[Python] Automatically totals the total number of articles posted by Qiita using the API

I want to know the total number of views of my Qiita article! !!

Only the contributor can check the number of views of Qiita posted articles for each article. So, ** To know the total number of views of your post, go to all articles and calculate with a calculator OK **!

that's all.


I would have calculated it exactly one year ago.

However, it's been a year since I became an engineer. Such a method like a primitive man is not allowed.

I knew that I could get it using the API, so I looked it up.

Many articles have appeared, I couldn't find any articles about total views.

Finally, like the link below I would like to use GAS to perform automatic aggregation on a regular basis to complete oleore analytics.

[Qiita API] Like! Automatic counting of views ← Amazing article

code


20190208 postscript If it is exactly 100 articles, it will come out without passing except I added.


import requests
import json
import math
from msvcrt import getch

USER_ID = 'Your Qiita ID'
PER_PAGE = 20
allViews = 0

headers = {"content-type": "application/json",
           'Authorization': 'Bearer your access token'}

#Send a request and insert a json that includes the total number of posts
url = 'https://qiita.com/api/v2/users/' + USER_ID
res = requests.get(url, headers=headers)
json_qiita_info = res.json()

#Pull out the number of posts from json
items_count = json_qiita_info['items_count']

#Calculate the number of pages
page = math.ceil(items_count / PER_PAGE)

print('|Article title|Number of views|Like count|')

#Get all posted articles
for i in range(page):

    #Send a request and insert a json containing the information of each article
    url = 'https://qiita.com/api/v2/authenticated_user/items' + \
        '?page=' + str(i + 1)
    res = requests.get(url, headers=headers)
    json_qiita_info = res.json()

    for j in range(PER_PAGE):
        try:
            #Pull ID out of json
            item_id = json_qiita_info[j]['id']

            #Send a request and insert a json containing the number of views of the article with the specified ID
            url = 'https://qiita.com/api/v2/items/' + str(item_id)
            res = requests.get(url, headers=headers)
            json_view = res.json()

            #Pull out the number of views from json
            page_view = json_view['page_views_count']

            #Addition and substitution to make the total number of views
            allViews += page_view

            #Display in order of title, number of likes, number of views
            print('| ' + json_qiita_info[j]['title'] + ' | ' +
                  str(json_qiita_info[j]['likes_count']) + ' |' +
                  str(page_view) + ' |')

        except IndexError:
            print('View total:' + str(allViews))
            print('Output complete')
            getch()
            break

#If it is exactly 100 articles, it will end without passing except
print('View total:' + str(allViews))
print('Output complete')
getch()

Execution result

QiitaView.gif

The title, number of likes, and number of views of each article are displayed, and finally the total number of views is don! is.

Clogged part

The number of views cannot be obtained only by GET request

When I was investigating, ** "I was able to get the article information only with a GET request without an access token!" ** Because there was information that ** I thought, "The number of views can be completed only by a GET request that does not use an access token." **.

Also, in the official document of Qiita API v2 (GET / api / v2 / items), the value that exists in the field There was a " page_views_count ", so I got it and finished. .. .. I thought, but it didn't work.

None will be returned as shown in the image. QiitaView.PNG

If you think about it carefully, the number of views of each article can only be confirmed by the person who wrote the article. It was natural to say that it was not possible to obtain it with just a GET request.

How to get the correct number of views ** Use the request body including the access token in the GET request ** I understood it with the recognition that it was. (* The recognition of this area is ambiguous, so please point out any mistakes.)


The following is quoted from the official document of Qiita API v2

Parameters To request API v2, use 5 types of HTTP methods: GET, POST, PUT, PATCH, and DELETE. Requests to many APIs can include parameters, but if you want to include parameters in a GET request, use a URI query, otherwise use a request body. There are two types of parameters, one that is passed arbitrarily, such as pagination, and the other that is essential, such as the text at the time of posting. The API documentation describes the parameters that can be sent for each API.

paging

Until I used the API, I was in a state of "what is paging?" I spent some time.

As the word paging says ** Turn over the book and look up the information on that page ** It means that it is necessary to implement ** book flipping ** ... in the process.

   #Send a request and insert a json containing the information of each article
    url = 'https://qiita.com/api/v2/authenticated_user/items' + '?page=' + str(i + 1)
    res = requests.get(url, headers=headers)
    json_qiita_info = res.json()

The parts corresponding to paging are shown separately for easy understanding. This is where '? Page ='+ str (i + 1). It's just sending a request to the page with the specified index.

Specify by character string

I've spent the most time on this. You may not notice that it contains half-width blank spaces, The \ is missing at the end of the string. .. .. It was awkward to get an error. Furthermore, since the character string is out of the correction range of the auto indent function It took me a long time to notice the mistake first. .. ..

Links that I used as a reference

Try using Qiita API from Python Get a list of posts using Qiita API v2 [Python] Get list of posts using Qiita API + Review of 2018

Recommended Posts

[Python] Automatically totals the total number of articles posted by Qiita using the API
Get the number of PVs of Qiita articles you posted with API
Get the number of articles accessed and likes with Qiita API + Python
[Python] Get the number of views of all posted articles
How to output the number of VIEWs, likes, and stocks of articles posted on Qiita to CSV (created with "Python + Qiita API v2")
Judge the authenticity of posted articles by machine learning (Google Prediction API).
Calculate the total number of combinations with python
[Python] I tried collecting data using the API of wikipedia
Get the number of views of Qiita
Try using the Kraken API in Python
Tweet using the Twitter API in Python
I tried to get the authentication code of Qiita API with Python.
Pandas of the beginner, by the beginner, for the beginner [Python]
Divides the character string by the specified number of characters. In Ruby and Python.
To automatically send an email with an attachment using the Gmail API in Python
Output the number of CPU cores in Python
Stock number ranking by Qiita tag with python
Try using the BitFlyer Ligntning API in Python
I tried using the Datetime module by Python
Minimize the number of polishings by combinatorial optimization
Recent ranking creation using Qiita API with Python
Try using the collections module (ChainMap) of python3
Anonymous upload of images using Imgur API (using Python)
Find the geometric mean of n! Using Python
Determine the number of classes using the Starges formula
Try using ChatWork API and Qiita API in Python
Try using the DropBox Core API in Python
After hitting the Qiita API with Python to get a list of articles for beginners, we will visit the god articles
python beginners tried to predict the number of criminals
[Python] A program that counts the number of valleys
[First API] Try to get Qiita articles with Python
Explanation of the concept of regression analysis using python Part 2
How to get the number of digits in Python
[Python] Get the text of the law from the e-GOV Law API
Summary of Python articles by pharmaceutical company researcher Yukiya
Initial settings when using the foursquare API in python
[Question] About API conversion of chat bot using Python
The pain of gRPC using Python. November 2019. (Personal memo)
Send and receive Gmail via the Gmail API using Python
Get the size (number of elements) of UnionFind in Python
Let's use the Python version of the Confluence API module.
[Python] Using Line API [1st Creation of Beauty Bot]
Explanation of the concept of regression analysis using Python Part 1
I tried using the API of the salmon data project
Explanation of the concept of regression analysis using Python Extra 1
Using the National Diet Library Search API in Python
Study from the beginning of Python Hour8: Using packages
Speech file recognition by Google Speech API v2 using Python
[Python] Use the Face API of Microsoft Cognitive Services
Play music by hitting the unofficial API of Google Play Music
A little bit from Python using the Jenkins API
Clustering G-means that automatically determines the number of clusters
Call the API of Hatena Blog from Python and save your blog articles individually on your PC
I built an application with Lambda that notifies LINE of "likes" using the Qiita API
I wrote a Python script that exports all my posts using the Qiita API v2
How to know the number of GPUs from python ~ Notes on using multiprocessing with pytorch ~
Read English sentences by hitting Google Translate API with Python without using the distributed module
(Move / Repost) Extraction of articles containing specific words using e-Gov Law API and XML Python
the zen of Python
Reuse the behavior of the @property method by using a descriptor [16/100]
Get the number of specific elements in a python list