How to get country place details from Twitter using Tweetsharp?

99封情书 提交于 2019-12-24 15:27:08

问题


Can anyone help me to get country details from Twitter using the Tweetsharp API? I have written a snippet from a program to get the details of the user, but I am unable to get the country details of the user.

        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 + ",");
        }

回答1:


As far as I can tell, you are getting list of followers from a specified user and then you try to get follower countries. You need to call GetUserProfile for each follower in order to get country information (if one is provided)




回答2:


Your code is getting the country from a user's status. However a users "Place" within the status (i.e. location) is obtained from a users geo coordinates, and many users choose not to share this information.

Instead of trying to determine a user's location from their status, I'd refer to a Users "Location" and "Timezone". However, as with a users "Place", the "Location" and "Timezone" are also optional.

Here's an example where follower's username, location and timezone are outputted, along with a list of users URLs (which are again optional!)

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();
}


来源:https://stackoverflow.com/questions/22040626/how-to-get-country-place-details-from-twitter-using-tweetsharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!