How to get followers and followers from python using the Mastodon API

Introduction

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

Number of my followers

da7e85c5357d395b78c6a383b770be3b.png

41 cases!

Get bad followers

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

Get the right followers

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

Summary

Read the document properly even if you are busy

Good reason

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

Promotion

Mastodon's management tool "MASTMAN" is under development and everyone should try it!

Recommended Posts

How to get followers and followers from python using the Mastodon API
How to get the Python version
From Python to using MeCab (and CaboCha)
API explanation to touch mastodon from python
[Python] How to get the first and last days of the month
[Python] How to read data from CIFAR-10 and CIFAR-100
How to get article data using Qiita API
How to get the files in the [Python] folder
[Python Kivy] How to get the file path by dragging and dropping
How to get the date and time difference in seconds with python
How to get a value from a parameter store in lambda (using python)
Sample code to get the Twitter API oauth_token and oauth_token_secret in Python 2.7
How to get a sample report from a hash value using VirusTotal's API
Get news from three major mobile companies using Django and the News API
[Rails] How to get location information using Geolocation API
How to get started with the 2020 Python project (windows wsl and mac standardization)
How to get the variable name itself in python
How to get the number of digits in Python
[Python] Get the text of the law from the e-GOV Law API
Push notifications from Python to Android using Google's API
Send and receive Gmail via the Gmail API using Python
[Introduction to Python] How to stop the loop using break?
How to post a ticket from the Shogun API
Get files from Linux using paramiko and scp [Python]
A little bit from Python using the Jenkins API
[For beginners] How to display maps and search boxes using the GoogleMap Javascript API
A story about a Python beginner trying to get Google search results using the API
Pass an array from PHP to PYTHON and do numpy processing to get the result
Get your heart rate from the fitbit API in Python!
How to connect to various DBs from Python (PEP 249) and SQLAlchemy
How to get the last (last) value in a list in Python
How to get all the keys and values in the dictionary
Predict gender from name using Gender API and Pykakasi in Python
How to get into the python development environment with Vagrant
Tweet Now Playing to Twitter using the Spotify API. [Python]
How to get temperature from switchBot thermo-hygrometer using raspberry Pi
I tried to get various information from the codeforces API
Get the weather using the API and let the Raspberry Pi speak!
[Introduction to Python] How to get data with the listdir function
How to install python using anaconda
How to get started with Python
[Python] How to import the library
How to use OpenPose's Python API
Use the Flickr API from Python
Get upcoming weather from python weather api
How to access wikipedia from python
Run Ansible from Python using API
[Python] How to use Typetalk API
[Python / Ruby] Understanding with code How to get data from online and write it to CSV
How to know the number of GPUs from python ~ Notes on using multiprocessing with pytorch ~
How to get only the data you need from a structured data set using a versatile method
How to get and set the NTP server name by DHCP
How to pass and study the Python 3 Engineer Certification Basic Exam
python / pandas / dataframe / How to get the simplest row / column / index / column
How to get a string from a command line argument in python
[Python] How to get & change rows / columns / values from a table.
Hit the New Relic API in Python to get the server status
[EC2] How to install and download chromedriver from the command line
Get and set the value of the dropdown menu using Python and Selenium
[Python] Conversation using OpenJTalk and Talk API (up to voice output)
Regularly upload files to Google Drive using the Google Drive API in Python