如何使用Tweetsharp从Twitter获取国家/地区详细信息?

时间:2014-02-26 11:56:00

标签: c# twitter tweetsharp

有人可以帮助我使用Tweetsharp API从Twitter获取国家/地区详细信息吗?我已经从程序中编写了一个片段来获取用户的详细信息,但我无法获得该用户的国家/地区详细信息。

        string tweetText = string.Empty;
        int count;
        StringBuilder sbWebsite = new StringBuilder();
        StringBuilder sbCountry = new StringBuilder();
        TwitterTest obj = new TwitterTest();

        TwitterService twitterService = obj.GetAuthentication();

        TwitterUser objUser = twitterService.GetUserProfile(new GetUserProfileOptions { IncludeEntities = true, SkipStatus = false });
        ListFollowersOptions objFollowerOptions = new ListFollowersOptions();

        objFollowerOptions.UserId = objUser.Id;
        objFollowerOptions.ScreenName = objUser.ScreenName;
        objFollowerOptions.IncludeUserEntities = true;
        objFollowerOptions.SkipStatus = false;
        objFollowerOptions.Cursor = -1;

        TwitterCursorList<TwitterUser> followers = twitterService.ListFollowers(objFollowerOptions);

        for (int i = 0; i < followers.Count; i++)
        {
            if (followers[i].Status != null)
            {
                if (followers[i].Status.Place != null)
                {
                    if (followers[i].Status.Place.Country != null)
                        sbCountry.Append(followers[i].Status.Place.Country + ",");
                }

            }
            sbWebsite.Append(followers[i].Url + ",");
        }

2 个答案:

答案 0 :(得分:0)

据我所知,您正在获取指定用户的关注者列表,然后您尝试获取关注者国家/地区。您需要为每个关注者调用GetUserProfile以获取国家/地区信息(如果提供了一个)

答案 1 :(得分:0)

您的代码是从用户的状态获取国家/地区。但是,状态(即位置)内的用户“放置”是从用户geo coordinates获得的,并且许多用户选择不共享此信息。

我没有尝试从状态中确定用户的位置,而是引用Users“位置”和“时区”。但是,与用户“地点”一样,“位置”和“时区”也是可选的。

这是一个示例,其中输出了关注者的用户名,位置和时区,以及用户URL列表(这些也是可选的!)

static void Main(string[] args)
{
 var follwerUrls = new StringBuilder();
 var followerNameLocationAndTimeZones = new StringBuilder();

 var twitterService = new TwitterService("yourConsumerKey", "yourConsumerSecret");
 twitterService.AuthenticateWith("yourToken", "yourTokenSecret");

 var userProfile = twitterService.GetUserProfile(new GetUserProfileOptions { IncludeEntities = true, SkipStatus = false });

 var followerOptions = new ListFollowersOptions 
                        { 
                            IncludeUserEntities = true, 
                            ScreenName = userProfile.ScreenName, 
                            SkipStatus = false, 
                            UserId = userProfile.Id 
                        };

 var usersfollowers = twitterService.ListFollowers(followerOptions);

 foreach (var follower in usersfollowers)
 {
    followerNameLocationAndTimeZones.Append(follower.Name);
    followerNameLocationAndTimeZones.Append(", ");
    followerNameLocationAndTimeZones.Append(string.IsNullOrEmpty(follower.Location) ? "LOCATION NOT SPECIFIED" : follower.Location);
    followerNameLocationAndTimeZones.Append(", ");
    followerNameLocationAndTimeZones.Append(string.IsNullOrEmpty(follower.TimeZone) ? "TIMEZONE NOT SPECIFIED" : follower.TimeZone);
    followerNameLocationAndTimeZones.Append('\n');

    follwerUrls.Append(follower.Url ?? "URL NOT SPECIFIED");
    follwerUrls.Append('\n');
 }

 Console.WriteLine(followerNameLocationAndTimeZones.ToString());
 Console.WriteLine(follwerUrls.ToString());
 Console.ReadKey();
}