Play with Mastodon's archive in Python 2 Count replies and favourites

Introduction

For the Mastodon archive, it is recommended to refer to The above [Python] Post date and time scatter plot in the Mastodon archive .... Especially, I will omit the part to read from the json file and store the toot contents in objects this time.

Count reply destinations

Collect reply destination IDs

at = []
for object in objects:
    if '@' in object:
        at.append(object)
name = []
others = []
for a in at:
    while '@' in a:
        if ' ' in a:
            name.append(a[1:a.index(' ')])
            a=a[a.index(' ')+1:]
        else:
            others.append(a)
            break
len(name)

I have stripped the tag from the toot content, but since @onekodate remains at the beginning, I will first search for the one that contains @ from the toot content. Next, fetch from the second to the blank after the ID. Note the toots without this space and the toots with multiple destinations. Toots with no blanks are collected by others, so it's a good idea to check them. If there are any mentions, they are usually empty, so I will omit them. Also, repeat the operation with the while statement as long as there is @ in the rest so as not to miss multiple destinations. This will collect the destination IDs.

Count each frequency

import pandas as pd
name = pd.Series(name)
namerank = name.value_counts()
print(namerank[0:29])

Since value.counts () of pandas.Series is convenient, I purposely converted it to use it, and it is designed to display the top 30 people.

Count the destinations of the fabo

Load likes.json

import json
import numpy as np
file = 'likes.json'
with open(file, 'r', encoding='utf-8') as fi:
    novels = json.load(fi)  
for novel in novels:
    likes = np.array(novels[novel])
len(likes)

Load likes.json containing the destination URL of the favourite in exactly the same way as you loaded outbox.json at the beginning of the last time.

Collect fabo destinations

favuser = []
others = []
for like in likes:
    if 'users' in like and 'statuses' in like:
        favuser.append(like[like.index('users')+6:like.index('statuses')-1])
    else:
        others.append(like)
len(favuser)

In likes, the URL of the destination of the favorite such as https://mstdn.jp/users/onekodate/statuses/99502681496689830 is written, and the URL of Mastodon contains the ID information, so please take it out. You can tell who you are, but there are two problems here. ・ Misskey and Pleroma URLs do not include ID information -Occasionally, the mysterious character string'tag: pawoo.net, 2017-09-15: objectId = 41135378: objectType = Status'is included in the URL. There is nothing I can do about these two, so I'll set them aside for others. Therefore, the URL contains'users' and'statuses', and the character string between them is fetched as the ID.

Count each frequency

import pandas as pd
fav = pd.Series(favuser)
favrank = fav.value_counts()
print(favrank[0:29])

This is exactly the same as when replying. You don't have to import pandas every time, though.

Graph the transition of the number of favourites

follower = np.zeros((10,len(favuser)))
for i in range(len(favuser)):
    for j in range(10):
        if favuser[i]==favrank.index[j]:
            follower[j][i:len(favuser)] = follower[j][i] + 1

I will graph the transition of the number of fabs for the 10 prestigious people who have a lot of fabs from me. There is no time data in likes.json, but it is probably arranged in chronological order, so let's assume that the order can be regarded as time. Create a two-dimensional array using numpy and count by force. If you just want to draw a graph, I feel that you should write it every time without creating a two-dimensional array.

import matplotlib.pyplot as plt
fig = plt.figure(figsize=(10, 5), dpi=200)
for j in range(10):
    plt.plot(range(len(favuser)),follower[j],label=favrank.index[j])
    plt.legend(bbox_to_anchor=(1, 1), loc='upper left', borderaxespad=0, fontsize=18)
fig.savefig('favtime.png')

favtime.png Here is the one with the legend removed. The number one person was overwhelming, but it seems that it has been sluggish recently, certainly I have not seen it recently. The light blue person in 10th place seems to have been sluggish recently, because he has reincarnated.

in conclusion

I thought it would be interesting to see the relationship with my followers, so I looked at the replies. You can probably boost in the same way. If you can see this person, it will be unrequited love or both feelings and it will be fun, but we are waiting for everyone's use.

Recommended Posts

Play with Mastodon's archive in Python 2 Count replies and favourites
Search and play YouTube videos in Python
Fractal to make and play with Python
Dealing with "years and months" in Python
Fill the string with zeros in python and count some characters from the string
[REAPER] How to play with Reascript in Python
Play with Google Spread Sheets in python (OAuth)
Archive and compress the entire directory with python
Play youtube in python
Calculate Pose and Transform differences in Python with ROS
Hit Mastodon's API in Python
Programming with Python and Tkinter
Script to count and stop up to 5 seconds in Python in Blender
Design and test Verilog in Python only with Veriloggen and cocotb.
Encryption and decryption with Python
Word Count with Apache Spark and python (Mac OS X)
Scraping with selenium in Python
Python and hardware-Using RS232C with Python-
Working with LibreOffice in Python
Scraping with chromedriver in python
Debugging with pdb in Python
[Python] How to play with class variables with decorator and metaclass
Working with sounds in Python
Scraping with Selenium in Python
Tweet with image in Python
[Python] Play with Discord's Webhook.
[Homology] Count the number of holes in data with Python
[Let's play with Python] Image processing to monochrome and dots
Combined with permutations in Python
Stack and Queue in Python
python with pyenv and venv
Unittest and CI in Python
Install CaboCha in Ubuntu environment and call it with Python.
Play with the password mechanism of GitHub Webhook and Python
How to log in to AtCoder with Python and submit automatically
Works with Python and R
Count the number of Thai and Arabic characters well in Python
I want to solve APG4b with Python (only 4.01 and 4.04 in Chapter 4)
Draw a watercolor illusion with edge detection in Python3 and openCV3
[Python3] Save the mean and covariance matrix in json with pandas
Throw something to Kinesis with python and make sure it's in
Install pip in Serverless Framework and AWS Lambda with Python environment
Firebase Authentication token issuance in Python and token verification with Fast API
Play with Statistical Modeling: Quantify J-League Team Strength with Stan and Python
Communicate with FX-5204PS with Python and PyUSB
Number recognition in images with Python
MIDI packages in Python midi and pretty_midi
Robot running with Arduino and python
Testing with random numbers in Python
Install Python 2.7.9 and Python 3.4.x with pip.
Neural network with OpenCV 3 and Python 3
AM modulation and demodulation with python
Difference between == and is in python
[Python] font family and font with matplotlib
Scraping with Node, Ruby and Python
View photos in Python and html
GOTO in Python with Sublime Text 3
Sorting algorithm and implementation in Python
Scraping with Selenium in Python (Basic)
CSS parsing with cssutils in Python
Scraping with Python, Selenium and Chromedriver