开发者

How to get a count of followers from Twitter API and trendline

开发者 https://www.devze.com 2023-01-23 10:56 出处:网络
I am in the process of writing some reports for the number of followers over time for Twitter, however after substantial searches and trial and error, I have not being able to get the number of follow

I am in the process of writing some reports for the number of followers over time for Twitter, however after substantial searches and trial and error, I have not being able to get the number of followers over time - particularly past number of followers.

I know there is an API to get the individual userIds for the followers, but thats an overkill for what I need and I would have to call it everyday. Ideally it would be great if I can pass a date and it could return the number of followers.

Does anyone have any experience with this and what the API might be!

T开发者_如何学JAVAhanks


While there is no direct API to get the trendline, getting the followers count is fairly easy, access via the URL:

http://api.twitter.com/1/users/show.json?user_id=12345

The documentation has it all @ https://dev.twitter.com/docs/api/1/get/users/show

To get the trendline, seems like I will need to query it on a daily basis!

Updated to Twitter API v1.1

https://api.twitter.com/1.1/users/show.json?user_id=12345

Documentation at https://dev.twitter.com/docs/api/1.1/get/users/show

Updated 31-May-2018

The new API end point is at

https://developer.twitter.com/en/docs/accounts-and-users/follow-search-get-users/api-reference/get-users-show


Here is a simple PHP exemple using CURL, with no library involved, to get the followers_count of a selected profile (here @TwitterFrance) using v2 API and the bearer token (bearer token is some kind of simplified method to access public data through an OAuth 2.0 API)

$authorization = "Authorization: Bearer YOUREXTRALONGBEARERYOUREXTRALONGBEARERYOUREXTRALONGBEARERYOUREXTRALONGBEARERYOUREXTRALONGBEARERYOUREXTRALONGBEAR";
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', $authorization));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, "https://api.twitter.com/2/users/by/username/TwitterFrance?user.fields=public_metrics");
$result = curl_exec($ch);
curl_close($ch);
if (is_string($result)) {
    echo (json_decode($result)->data->public_metrics->followers_count);
    die();
}


I know this is an old question but I want to give an answer for those who still looking for an alternative way to get Twitter followers count. After I spent some time on documentation, I found that you’ll need to apply for Elevated access via the Developer Portal, in order to get more than Essential information. You need to apply for additional access within the developer portal for this access level. You can check Access Levels from here.

https://developer.twitter.com/en/docs/twitter-api/getting-started/about-twitter-api#v2-access-level

After some googling, I found this. No authentication is needed but I can't guarantee that it will not be taken down in the future. Just provide the screen_name of any account you want. E.g.

https://cdn.syndication.twimg.com/widgets/followbutton/info.json?screen_names=binance

...which will give you something like this:

[
    {
        "following": false,
        "id": "877807935493033984",
        "screen_name": "binance",
        "name": "Binance",
        "protected": false,
        "followers_count": 6959348,
        "formatted_followers_count": "6.96M followers",
        "age_gated": false
    }
]


In Swift 4.2 and Xcode 10.1 to get twitter followers_count

Here you need to integrate twitter SDK into you app and follow integration details https://github.com/twitter/twitter-kit-ios

//This is complete url 
https://api.twitter.com/1.1/users/show.json?screen_name=screenName

func getStatusesUserTimeline(accessToken:String) {

    let userId = "109*************6"
    let twitterClient = TWTRAPIClient(userID: userId)
    twitterClient.loadUser(withID: userId) { (user, error) in
        print(userId)
        print(user ?? "Empty user")
        if user != nil {
            var request = URLRequest(url: URL(string: "https://api.twitter.com/1.1/users/show.json?screen_name=screenName")!)

            request.httpMethod = "GET"
            request.setValue("Bearer "+accessToken, forHTTPHeaderField: "Authorization")
            print(request)

            let task = URLSession.shared.dataTask(with: request) { data, response, error in guard let data = data, error == nil else { // check for fundamental networking error
                print("error=\(String(describing: error))")
                return
                }

                if let httpStatus = response as? HTTPURLResponse, httpStatus.statusCode != 200 { // check for http errors
                    print("statusCode should be 200, but is \(httpStatus.statusCode)")
                    print("response = \(String(describing: response))")
                }

                do {
                    let response = try JSONSerialization.jsonObject(with: data, options: []) as! Dictionary<String,Any>
                    print(response)
                    // print((response["statuses"] as! Array<Any>).count)

                } catch let error as NSError {
                    print(error)
                }
            }

            task.resume()

        } else {
            print(error?.localizedDescription as Any)
        }
    }

}


Using Python:

import tweepy
twitter = tweepy.Client(bearer_token="your bearer token")

Single user:

print(twitter.get_user(username="twitter username", user_fields=["public_metrics"]).data.public_metrics['followers_count'])

Multiple users:

for i in twitter.get_users(usernames=twitterProfiles, user_fields=["public_metrics"]).data:
  print(i.public_metrics['followers_count'])
0

精彩评论

暂无评论...
验证码 换一张
取 消