How to get country place details from Twitter usin

2019-08-03 05: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 + ",");
        }

2条回答
够拽才男人
2楼-- · 2019-08-03 05:39

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)

查看更多
▲ chillily
3楼-- · 2019-08-03 05:46

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();
}
查看更多
登录 后发表回答