I will introduce how to use the API that I made a mistake when developing the Mastodon management tool "MASTMAN".
Docs How to use the API feels relatively simple as follows (it should have been ...)
GET /api/v1/accounts/:id/followers
Query parameters: max_id : Get a list of followers with ID less than this value since_id : Get a list of followers with ID greater than this value limit : Maximum number of followers to get (Default 40, Max 80)
Reference: Getting an account's followers
41 cases!
The code below gets the first 10 follower IDs, gets the smallest ID, and then takes the 10 smaller IDs and repeats.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import urllib2
import urllib
import json
import re
access_token = 'xxxxx'
def get_followers(user_id, max_id=None):
#The default limit is 40, but this time it's 10 because it's easy to see failure cases!
limit_num = 10
url = "https://mstdn.jp/api/v1/accounts/%d/followers" %(user_id)
param = []
param.append(('limit', limit_num))
if max_id:
param.append(('max_id', max_id))
url += "?" + urllib.urlencode( param )
request = urllib2.Request(url)
auth_header = 'Bearer %s' %(access_token)
request.add_header('Authorization', auth_header)
res = urllib2.urlopen(request)
res_list = json.loads(res.read())
#The result is limit_num(If it is 10 cases this time, get the next 10 cases...
if len(res_list) == limit_num:
next_max_id = min(map(lambda x: x.get('id'), res_list))
res_list.extend(get_followers(user_id, max_id=next_max_id))
return res_list
def main():
#toitech ID
my_user_id = 96368
res = get_followers(my_user_id)
#Result number of items
print len(res)
#Number of unique IDs in the result
print len(set(map(lambda x: x.get('id'), res)))
if __name__ == '__main__':
main()
When I do this, I get only 10! !! !!
$ python get_follower.py
10
10
If you look at Docs again, you will see the following note ...
Note: max_id and since_id for next and previous pages are provided in the Link header. It is not possible to use the id of the returned objects to construct your own URLs, because the results are sorted by an internal key.
It seems that it is different to specify the ID of the returned result as since_id or max_id. Look at the header.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import urllib2
import urllib
import json
import re
access_token = 'xxxxx'
def get_followers(user_id, max_id=None):
#The default limit is 40, but this time it's 10 because it's easy to see failure cases!
limit_num = 10
url = "https://mstdn.jp/api/v1/accounts/%d/followers" %(user_id)
param = []
param.append(('limit', limit_num))
if max_id:
param.append(('max_id', max_id))
url += "?" + urllib.urlencode( param )
request = urllib2.Request(url)
auth_header = 'Bearer %s' %(access_token)
request.add_header('Authorization', auth_header)
res = urllib2.urlopen(request)
res_list = json.loads(res.read())
#The result is limit_num(If it is 10 cases this time, get the next 10 cases...
if len(res_list) == limit_num:
link_str = res.info().getheaders("link")[0]
next_max_id = re.search("max_id=(\d+)>", link_str).group(1)
res_list.extend(get_followers(user_id, max_id=next_max_id))
return res_list
def main():
#toitech ID
my_user_id = 96368
res = get_followers(my_user_id)
#Result number of items
print len(res)
#Number of unique IDs in the result
print len(set(map(lambda x: x.get('id'), res)))
if __name__ == '__main__':
main()
When I do this ... it's 41! I'm happy.
$ python get_follower.py
41
41
Read the document properly even if you are busy
You can get the following status (toot) in the same way as the former (getting bad followers)! The newer the toot, the higher the toot ID, but that's probably because followers don't always have as big a user ID as those who have been followed recently.
GET /api/v1/accounts/:id/statuses
Mastodon's management tool "MASTMAN" is under development and everyone should try it!
Recommended Posts