使用 C# 的 Twitter API Atom 聚合器

发布于 2024-12-10 22:45:43 字数 5504 浏览 0 评论 0原文

我的问题是,我继承了一个网站的启动,该网站将 Twitter 提要代码作为绝对页脚放置在页面上。政府已决定他们不希望提要中包含任何转发或@提及。我可以看到提要是通过 Atom RSS 聚合的。我试图只更改搜索词和 C# 代码中的 twitter URL,然后整个网站崩溃了。

不幸的是,我对 C# 完全不熟悉,所以我修改这段代码的能力充其量是有限的。

该网站的构建包含页眉、页脚、twitter 栏等。

此外,该网站预计将于今晚在 COB 发布,因此从头开始起草另一个插件实际上并不是一个选择。

通过以下代码将股票代码调用到页面(代码缺少开始和结束注释括号,因此它会显示在此处):

com.omniupdate.div label="ticker" path="/z-omniupdate/fakes/bfeo/2012/ticker.html"      
bf2012:ticker runat="server"     
/com.omniupdate.div 

CSS 为:

    #ticker{
    width: 100%;
    height: 43px;
    border-top: 1px solid #939241;
    background: #bdcc2a url('/bfeo/2012/img/tweet-bg.gif') 0 0 repeat-x;
    background: -moz-linear-gradient(#bdcc2a, #a9b625);
    background: -webkit-gradient(linear, 0% 0%, 0% 100%, from(#a9b625), to(#e4c595));
    background: -webkit-linear-gradient(#bdcc2a, #a9b625); 
    background: -o-linear-gradient(#bdcc2a, #a9b625);
    position:fixed;
    z-index: 1000;
    bottom: 0;
}
    #ticker p{
        font-size: 15px;
        color: #fff;
        text-transform: uppercase;
        float: left;
        line-height: 43px;
        margin-right: 10px;
    }
.ticker-engage {
    list-style: none;
    padding: 0;
    margin: 8px 0 0 0;
    width: 108px;
    height: 28px;
    float: left;
}
    .ticker-engage li{
        display: inline;    
    }
    .ticker-engage li a{
        height: 28px;
        width: 28px;
        text-indent: -9999px;
        float: left;
        margin: 0 2px 0 0;
    }
    .ticker-engage  li a.twitter{
        background: url('/bfeo/2012/img/twitter-sm.png') 0 0 no-repeat;
    }
    .ticker-engage  li a.facebook{
        background: url('/bfeo/2012/img/facebook-sm.png') 0 0 no-repeat;
    }
    .ticker-engage  li a.youtube{
        background: url('/bfeo/2012/img/youtube-sm.png') 0 0 no-repeat;
    }
 #tweetlist{
    list-style: none;
    padding: 7px 20px 0 20px;
    height: 36px;
    width: 666px;
    float: left;
    background: url('/bfeo/2012/img/tweet-bg.gif') 0 0 repeat-x;
    background: -moz-linear-gradient(#aab726, #98a322);
    background: -webkit-gradient(linear, 0% 0%, 0% 100%, from(#aab726), to(#98a322));
    background: -webkit-linear-gradient(#aab726, #98a322); 
    background: -o-linear-gradient(#aab726, #98a322);
    font-size: 12px;
}
     #tweetlist li{
        display: inline;    
    }
     #tweetlist li a:link,  #tweetlist li a:visited{
        color: #fff;
        text-decoration: none;
        font-weight: normal;
    }
     #tweetlist li a:hover, #tweetlist li a:focus{
        text-decoration: underline;
    }
     #tweetlist li span{
            color: #ffe400; 
     }

Ticker.ascx:

    <%@ Control Language="C#" AutoEventWireup="true" CodeFile="ticker.ascx.cs" Inherits="bf_controls_ticker" %>
<div id="ticker">
    <div class="wrapper clearfix">
        <p>Engage with us</p>
        <ul class="ticker-engage">
            <li><a class="facebook" target="_blank" href="http://www.facebook.com/booththinking">
                Facebook</a></li>
            <li><a class="twitter" target="_blank" href="http://twitter.com/booththinking">Twitter</a></li>
            <li><a class="youtube" target="_blank" href="http://www.youtube.com/user/BoothThinking">
                Youtube</a></li>
        </ul>
        <form id="form1" runat="server">
        <div>
            <ul id="tweetlist">
                <asp:Literal ID="Literal1" runat="server"></asp:Literal>
            </ul>
        </div>
        </form>
    </div>
</div>

ticker.ascx.cs

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml;
using System.Text;

public partial class bf_controls_ticker : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        XmlDocument doc = new XmlDocument();
        doc.LoadXml(HttpGet("http://search.twitter.com/search.atom?q=booththinking"));
        XmlNamespaceManager ns = new XmlNamespaceManager(doc.NameTable);
        ns.AddNamespace("at", "http://www.w3.org/2005/Atom");
        ns.AddNamespace("twitter", "http://api.twitter.com/");
        XmlNodeList entries = doc.DocumentElement.SelectNodes("//at:entry", ns);
        //Response.Write(entries.Count);
        StringBuilder sb = new StringBuilder();
        foreach (XmlNode node in entries)
        {
            sb.Append(String.Format("<li><span>{2}:</span> <a href=\"{0}\">{1}</a></li>", node.ChildNodes[2].Attributes["href"].Value, node.ChildNodes[3].InnerText, node.LastChild.FirstChild.InnerText.Split(' ')[0]));
        }
        Literal1.Text = sb.ToString();
    }

    private string HttpGet(string URI)
    {
        System.Net.WebRequest req = System.Net.WebRequest.Create(URI);

        System.Net.WebResponse resp = req.GetResponse();
        if (resp == null) return null;

        System.IO.StreamReader sr = new System.IO.StreamReader(resp.GetResponseStream());
        string responsexml = sr.ReadToEnd().Trim();
        sr.Close();

        return responsexml;
    }
}

My issue is that I have inherited the launch of a site which has a Twitter feed ticker placed as an absolute footer on the page. The administration has decided they do not want any retweets or @mentions included in the feed. I can see the feed is being aggregated through Atom RSS. I have attempted to just change the search terms, and the twitter URL in the C# code, and the whole site crashes.

Unfortunately, I am completely unfamiliar in C#, so my ability to modify this code is limited, at best.

The web site is constructed with includes for the header, footer, twitter bar, etc.

Furthermore, the projected launch for the site is this evening at COB, so drafting another plug-in from scratch is not really an option.

The ticker is called to the page with this (The code is missing its opening and closing comment brackets so it would display here):

com.omniupdate.div label="ticker" path="/z-omniupdate/fakes/bfeo/2012/ticker.html"      
bf2012:ticker runat="server"     
/com.omniupdate.div 

The CSS is:

    #ticker{
    width: 100%;
    height: 43px;
    border-top: 1px solid #939241;
    background: #bdcc2a url('/bfeo/2012/img/tweet-bg.gif') 0 0 repeat-x;
    background: -moz-linear-gradient(#bdcc2a, #a9b625);
    background: -webkit-gradient(linear, 0% 0%, 0% 100%, from(#a9b625), to(#e4c595));
    background: -webkit-linear-gradient(#bdcc2a, #a9b625); 
    background: -o-linear-gradient(#bdcc2a, #a9b625);
    position:fixed;
    z-index: 1000;
    bottom: 0;
}
    #ticker p{
        font-size: 15px;
        color: #fff;
        text-transform: uppercase;
        float: left;
        line-height: 43px;
        margin-right: 10px;
    }
.ticker-engage {
    list-style: none;
    padding: 0;
    margin: 8px 0 0 0;
    width: 108px;
    height: 28px;
    float: left;
}
    .ticker-engage li{
        display: inline;    
    }
    .ticker-engage li a{
        height: 28px;
        width: 28px;
        text-indent: -9999px;
        float: left;
        margin: 0 2px 0 0;
    }
    .ticker-engage  li a.twitter{
        background: url('/bfeo/2012/img/twitter-sm.png') 0 0 no-repeat;
    }
    .ticker-engage  li a.facebook{
        background: url('/bfeo/2012/img/facebook-sm.png') 0 0 no-repeat;
    }
    .ticker-engage  li a.youtube{
        background: url('/bfeo/2012/img/youtube-sm.png') 0 0 no-repeat;
    }
 #tweetlist{
    list-style: none;
    padding: 7px 20px 0 20px;
    height: 36px;
    width: 666px;
    float: left;
    background: url('/bfeo/2012/img/tweet-bg.gif') 0 0 repeat-x;
    background: -moz-linear-gradient(#aab726, #98a322);
    background: -webkit-gradient(linear, 0% 0%, 0% 100%, from(#aab726), to(#98a322));
    background: -webkit-linear-gradient(#aab726, #98a322); 
    background: -o-linear-gradient(#aab726, #98a322);
    font-size: 12px;
}
     #tweetlist li{
        display: inline;    
    }
     #tweetlist li a:link,  #tweetlist li a:visited{
        color: #fff;
        text-decoration: none;
        font-weight: normal;
    }
     #tweetlist li a:hover, #tweetlist li a:focus{
        text-decoration: underline;
    }
     #tweetlist li span{
            color: #ffe400; 
     }

Ticker.ascx:

    <%@ Control Language="C#" AutoEventWireup="true" CodeFile="ticker.ascx.cs" Inherits="bf_controls_ticker" %>
<div id="ticker">
    <div class="wrapper clearfix">
        <p>Engage with us</p>
        <ul class="ticker-engage">
            <li><a class="facebook" target="_blank" href="http://www.facebook.com/booththinking">
                Facebook</a></li>
            <li><a class="twitter" target="_blank" href="http://twitter.com/booththinking">Twitter</a></li>
            <li><a class="youtube" target="_blank" href="http://www.youtube.com/user/BoothThinking">
                Youtube</a></li>
        </ul>
        <form id="form1" runat="server">
        <div>
            <ul id="tweetlist">
                <asp:Literal ID="Literal1" runat="server"></asp:Literal>
            </ul>
        </div>
        </form>
    </div>
</div>

ticker.ascx.cs

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml;
using System.Text;

public partial class bf_controls_ticker : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        XmlDocument doc = new XmlDocument();
        doc.LoadXml(HttpGet("http://search.twitter.com/search.atom?q=booththinking"));
        XmlNamespaceManager ns = new XmlNamespaceManager(doc.NameTable);
        ns.AddNamespace("at", "http://www.w3.org/2005/Atom");
        ns.AddNamespace("twitter", "http://api.twitter.com/");
        XmlNodeList entries = doc.DocumentElement.SelectNodes("//at:entry", ns);
        //Response.Write(entries.Count);
        StringBuilder sb = new StringBuilder();
        foreach (XmlNode node in entries)
        {
            sb.Append(String.Format("<li><span>{2}:</span> <a href=\"{0}\">{1}</a></li>", node.ChildNodes[2].Attributes["href"].Value, node.ChildNodes[3].InnerText, node.LastChild.FirstChild.InnerText.Split(' ')[0]));
        }
        Literal1.Text = sb.ToString();
    }

    private string HttpGet(string URI)
    {
        System.Net.WebRequest req = System.Net.WebRequest.Create(URI);

        System.Net.WebResponse resp = req.GetResponse();
        if (resp == null) return null;

        System.IO.StreamReader sr = new System.IO.StreamReader(resp.GetResponseStream());
        string responsexml = sr.ReadToEnd().Trim();
        sr.Close();

        return responsexml;
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

香橙ぽ 2024-12-17 22:45:43

如果我理解正确的话,您只需检查字符串中是否有您要跳过的模式。

在每个循环中尝试这样的事情:

foreach (XmlNode node in entries)
{
    var content = node.SelectSingleNode("at:content", ns).InnerText;

    if(!content.ToLower().Contains("@somename"))
    {
        sb.Append(String.Format("<li><span>{2}:</span> <a href=\"{0}\">{1}</a></li>", node.ChildNodes[2].Attributes["href"].Value, node.ChildNodes[3].InnerText, node.LastChild.FirstChild.InnerText.Split(' ')[0]));
    }
}

查看正则表达式以进行更复杂的模式匹配

If I understand you correctly you could just check the string for the pattern you are trying to skip.

In your for each loop try something like this:

foreach (XmlNode node in entries)
{
    var content = node.SelectSingleNode("at:content", ns).InnerText;

    if(!content.ToLower().Contains("@somename"))
    {
        sb.Append(String.Format("<li><span>{2}:</span> <a href=\"{0}\">{1}</a></li>", node.ChildNodes[2].Attributes["href"].Value, node.ChildNodes[3].InnerText, node.LastChild.FirstChild.InnerText.Split(' ')[0]));
    }
}

have a look at regular expressions for more complicated pattern matching

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文