Twitter API link 解析器

Twitter API link parser

我遇到了问题,并尝试就此采取一切措施!!即使 HttpUtility.ParseQueryString 也无济于事!

我正在尝试以 http://t.co/oEVQbihMWu 的形式解析来自 API 的 Twitter 链接。我需要完全解决 URL.

我的代码:

richTextBox1.Clear();
            richTextBox1.Visible = true;
            SearchOptions SO = new SearchOptions();
            SO.GeoCode = richTextBox3.Text + "," + richTextBox2.Text + "mi";
            TwitterResponse<TwitterSearchResultCollection> TweetSearchResult = TwitterSearch.Search(tokens, "#blogger", SO);

            if (TweetSearchResult.Result != RequestResult.Success) richTextBox1.Text = "connection Error";
            else
            {
                string a = null;

                foreach (var tweet in TweetSearchResult.ResponseObject)
                {
                    string b = tweet.User.Location.Contains(",") ? tweet.User.Location.Replace(",", "-") : tweet.User.Location;
                    a += string.Format("{0},{1},{2},{3},{4},{5},{6},{7}", tweet.CreatedDate, b, tweet.User.Id,
                        tweet.User.ScreenName, tweet.User.Name, tweet.User.NumberOfFollowers, tweet.User.Website, Environment.NewLine);

                    richTextBox1.AppendText(" " + tweet.CreatedDate + "\n" + tweet.User.Location + "\n" + tweet.User.Id + "\n" + tweet.User.ScreenName + "\n" + tweet.User.Name + "\n" + tweet.User.NumberOfFollowers +
                    "\n" + tweet.User.Website + "\n" + tweet.Text + "\n\n\n");



}

链接由 tweet.user.website 表示。

有什么帮助吗? :)

在 API 响应中,有 entities.urls,其中包含 urlexpanded_url 映射的数组。检查您的图书馆的文档以获取等效内容。

或者,如果您检查 t.co 个链接的响应,您会发现:

<noscript><META http-equiv="refresh" content="0;URL=http://www.fitnessbydanielle.com"></noscript><title>http://www.fitnessbydanielle.com</title><script>window.opener = null; location.replace("http:\/\/www.fitnessbydanielle.com")</script>

解析它得到url。

我成功破解了。 我做了什么:

foreach (var tweet in TweetSearchResult.ResponseObject)
                {
                    if(tweet.User.Website != null)
                    {
                        HttpWebRequest req = (HttpWebRequest)WebRequest.Create(tweet.User.Website);
                        req.AllowAutoRedirect = false;
                        var resp = req.GetResponse();
                        string realUrl = resp.Headers["Location"];
                        string b = tweet.User.Location.Contains(",") ? tweet.User.Location.Replace(",", "-") : tweet.User.Location;
                        a += string.Format("{0},{1},{2},{3},{4},{5},{6},{7}", tweet.CreatedDate, b, tweet.User.Id,
                            tweet.User.ScreenName, tweet.User.Name, tweet.User.NumberOfFollowers, realUrl, Environment.NewLine);

                        richTextBox1.AppendText(" " + tweet.CreatedDate + "\n" + tweet.User.Location + "\n" + tweet.User.Id + "\n" + tweet.User.ScreenName + "\n" + tweet.User.Name + "\n" + tweet.User.NumberOfFollowers +
                        "\n" + realUrl + "\n" + tweet.Text + "\n\n\n");
                    }
                }

                File.AppendAllText(@".\BloggerTable.csv", a, Encoding.UTF8); 

}

将它包装在一个条件中,这样没有网站的用户就不会显示并使用网络请求来获取 link。将每条推文的位置存储在 httprequest header 中。