Get the number of articles accessed and likes with Qiita API + Python

Get the number of articles accessed and likes with Qiita API + Python

Introduction

`Although it is an article on Mac environment, the procedure is the same for Windows environment. Please read and try the environment-dependent part. ``

Purpose

Use the Qiita API to get the number of articles accessed and the number of LGTMs.

After reading this article to the end, you will be able to:

No. Overview keyword
1 Qiita API
2 REST API requests

Complete image

command.sh


page_views_count=11130, likes_count=21, title=Get Salesforce data using REST API, url=https://qiita.com/nsuhara/items/19cf8ec89b88fb3deb39
page_views_count=04778, likes_count=11, title=Alexa skills in Python/Implement with Lambda, url=https://qiita.com/nsuhara/items/5b19cfb5ffb897bd4cfd
page_views_count=04482, likes_count=06, title=Limit Salesforce attachments, url=https://qiita.com/nsuhara/items/bd41c9ad946b8b832207
page_views_count=04008, likes_count=11, title=Unit tests for Swift(Unit Test)how to make, url=https://qiita.com/nsuhara/items/bc06c07ff30a5b78696d
page_views_count=03543, likes_count=03, title=IOS with Kony App Platform/Create an Android app, url=https://qiita.com/nsuhara/items/c28d838492512850520c
page_views_count=02642, likes_count=07, title=Flask-SQLAlchemy +Create a web service in PostgreSQL, url=https://qiita.com/nsuhara/items/fa5998c0b2f4fcefbed4
page_views_count=02579, likes_count=15, title=Heroku + Selenium +Automate WEB process with Chrome, url=https://qiita.com/nsuhara/items/76ae132734b7e2b352dd
page_views_count=02127, likes_count=04, title=Periodically run Python on Heroku Scheduler, url=https://qiita.com/nsuhara/items/fac20adb6b0a122a3709
page_views_count=02102, likes_count=07, title=Docker +Create a web service in Flask, url=https://qiita.com/nsuhara/items/c7eff7fae3801f85b5cd
page_views_count=01869, likes_count=08, title=Messaging API + LIFF + Heroku + Flask +Create LINE BOT with Framework extension, url=https://qiita.com/nsuhara/items/0c431913165e4af0f8f5
page_views_count=01597, likes_count=04, title=Salesforce Lightning Data Service(LDS)learn about, url=https://qiita.com/nsuhara/items/ecd77def7aa1f985efcc
page_views_count=01507, likes_count=01, title=IOS created with Kony App Platform/Learn about coding Android apps, url=https://qiita.com/nsuhara/items/bf0e8884a7efc3c55176
page_views_count=01490, likes_count=05, title=Create a RESTful web service in Flask, url=https://qiita.com/nsuhara/items/449835bc94f0fb3bbcbd
page_views_count=01477, likes_count=02, title=From JSON to Word using Python(docx)To create, url=https://qiita.com/nsuhara/items/3ba2fa7db38acd04f448
page_views_count=01377, likes_count=02, title=AWS-Lambda +AWS CSV data in Python-Write to S3, url=https://qiita.com/nsuhara/items/b2bd1d2623bca0f767f8
page_views_count=01208, likes_count=03, title=AWS-Lambda +AWS with Python-RDS/Read PostgreSQL table, url=https://qiita.com/nsuhara/items/dd780c2622258d10f961
page_views_count=01194, likes_count=02, title=Django + SQLAlchemy + SQLite3 /Create a web app in PostgreSQL, url=https://qiita.com/nsuhara/items/4ab5366273082ee0aa73
page_views_count=01159, likes_count=04, title=AWS-Lambda + Python +Perform web scraping regularly with Cron, url=https://qiita.com/nsuhara/items/0d36600511fc162827f6
page_views_count=01071, likes_count=01, title=IOS created with Kony App Platform/Data link between Android app and Salesforce, url=https://qiita.com/nsuhara/items/756120f1bddc6f8fe78b
page_views_count=00887, likes_count=00, title=IOS created with Kony App Platform/Learn about Auto Layout for Android apps, url=https://qiita.com/nsuhara/items/a52abd9861c51823ecec
page_views_count=00647, likes_count=02, title=AWS with Python-S3 signed(With a time limit)Generate URL, url=https://qiita.com/nsuhara/items/20160b080c2b30d57729
page_views_count=00597, likes_count=03, title=Kong API Gateway GUI/Build Konga, url=https://qiita.com/nsuhara/items/a0de75e6767f98cc8fec
page_views_count=00500, likes_count=01, title=Build Kong API Gateway, url=https://qiita.com/nsuhara/items/ad1d8fa1faad7940b5c1
page_views_count=00471, likes_count=00, title=From setting up Raspberry Pi to installing Python environment, url=https://qiita.com/nsuhara/items/05a2b41d94ced1f54316
page_views_count=00437, likes_count=02, title=Create a remote control car with Raspberry Pi and Python, url=https://qiita.com/nsuhara/items/7970b5dfe95ea76c93d6
page_views_count=00364, likes_count=00, title=Flask-How to implement UPSERT in SQLAlchemy, url=https://qiita.com/nsuhara/items/86570f789093222252b1
page_views_count=00307, likes_count=00, title=LCD with Raspberry Pi and Python(16x2)Create a game, url=https://qiita.com/nsuhara/items/57105fd232feffbcd05c
page_views_count=00140, likes_count=00, title=How to insert CSV data into PostgreSQL at once, url=https://qiita.com/nsuhara/items/a1b75e0557ed134c5302

items_count=28, total_page_views_count=55690, total_likes_count=125

Execution environment

environment Ver.
macOS Catalina 10.15.6
Python 3.7.3
requests 2.24.0

Source code

I think that understanding will deepen if you read while actually following the implementation contents and source code. Please use it by all means.

GitHub

Related articles

-Qiita API v2 documentation

Precautions (points of struggle)

Article access count (page_views_count)

It seems that the number of accesses (page_views_count) will be null when articles are acquired in a batch or articles of others are acquired due to API restrictions. As a countermeasure, it was realized by a two-step procedure of Step1: Obtaining the article IDs in a batch, and Step2: Obtaining the number of access to each article using the ID. (Because there are API usage restrictions, if the number of articles increases (= the number of requests increases), you may get caught in the limit)

API usage restrictions

Requests are accepted up to 1000 times per hour for each user in the authenticated state, and up to 60 times per hour for each IP address in the unauthenticated state.

Access token

Personal access token settings

Settings> Application> Issue new token> Check read_qiita> Issue

`* Please save the issued personal access token. This string cannot be redisplayed. ``

Config

config.sh


export PYTHONPATH=app/
export QIITA_ACCESS_TOKEN={Access token registered above}
export QIITA_PAGE=1
export QIITA_PER_PAGE=100
export QIITA_URL_ITEM=qiita.com/api/v2/items
export QIITA_URL_LIST=qiita.com/api/v2/authenticated_user/items

Main

main.py


"""app/main.py
"""

import os

import requests


def get(url):
    """get
    """
    headers = {
        'Content-Type': 'application/json',
        'Charset': 'utf-8',
        'Authorization': 'Bearer {}'.format(os.getenv('QIITA_ACCESS_TOKEN'))
    }
    res = requests.get(url=url, headers=headers)
    print('{}, {}'.format(res.status_code, res.url))
    return res


def get_id_list():
    """get_list
    """
    res = get(url='https://{url}?page={page}&per_page={per_page}'.format(**{
        'url': os.getenv('QIITA_URL_LIST'),
        'page': os.getenv('QIITA_PAGE'),
        'per_page': os.getenv('QIITA_PER_PAGE')
    }))
    return [item.get('id') for item in res.json()]


def get_item(qiita_id):
    """get_item
    """
    res = get(url='https://{url}/{id}'.format(**{
        'url': os.getenv('QIITA_URL_ITEM'),
        'id': qiita_id
    }))
    item = res.json()
    return {
        'page_views_count': item.get('page_views_count'),
        'likes_count': item.get('likes_count'),
        'title': item.get('title'),
        'url': item.get('url')
    }


def output(items):
    """output
    """
    total_page_views_count = 0
    total_likes_count = 0

    print('-'*100)

    for item in items:
        total_page_views_count = total_page_views_count + \
            item.get('page_views_count')
        total_likes_count = total_likes_count+item.get('likes_count')

        print('page_views_count={}, likes_count={}, title={}, url={}'.format(
            str(item.get('page_views_count')).zfill(5),
            str(item.get('likes_count')).zfill(2),
            item.get('title'),
            item.get('url'))
        )

    print('\nitems_count={}, total_page_views_count={}, total_likes_count={}'.format(
        len(items), total_page_views_count, total_likes_count))

    print('-'*100)


def main():
    """main
    """
    items = list()

    for qiita_id in get_id_list():
        items.append(get_item(qiita_id=qiita_id))

    sorted_items = sorted(
        items, key=lambda x: x['page_views_count'], reverse=True)

    output(items=sorted_items)


if __name__ == '__main__':
    main()

Execution example

sample.sh


~$ git clone https://github.com/nsuhara/python-qiita-api.git -b master
~$ cd python-qiita-api

~$ python -m venv .venv
~$ source .venv/bin/activate
~$ pip install -r requirements.txt
~$ source config

~$ python app/main.py

Recommended Posts

Get the number of articles accessed and likes with Qiita API + Python
Get the number of PVs of Qiita articles you posted with API
I tried to get the authentication code of Qiita API with Python.
[First API] Try to get Qiita articles with Python
[Python] Get user information and article information with Qiita API
[Python] Get the number of views of all posted articles
Get a list of articles posted by users with Python 3 Qiita API v2
[Python] Automatically totals the total number of articles posted by Qiita using the API
I tried to get the number of days of the month holidays (Saturdays, Sundays, and holidays) with python
After hitting the Qiita API with Python to get a list of articles for beginners, we will visit the god articles
Get the number of searches with a regular expression. SeleniumBasic VBA Python
Calculate the total number of combinations with python
Get Gmail subject and body with Python and Gmail API
I tried to get the movie information of TMDb API with Python
Get and estimate the shape of the head using Dlib and OpenCV with python
Get the stock price of a Japanese company with Python and make a graph
How to get the number of digits in Python
Visualize the range of interpolation and extrapolation with python
Get the size (number of elements) of UnionFind in Python
Get comments and subscribers with the YouTube Data API
Get the operation status of JR West with Python
Get the number of digits
Call the API with python3.
Try hitting the Twitter API quickly and easily with Python
Get the number of specific elements in a python list
Let's touch the API of Netatmo Weather Station with Python. #Python #Netatmo
Get rid of dirty data with Python and regular expressions
[Homology] Count the number of holes in data with Python
Sample of HTTP GET and JSON parsing with python of pepper
Play with the password mechanism of GitHub Webhook and Python
Get the source of the page to load infinitely with python.
Call the API of Hatena Blog from Python and save your blog articles individually on your PC
Get reviews with python googlemap api
I built an application with Lambda that notifies LINE of "likes" using the Qiita API
Quine Post with Qiita API (Python)
Get the weather with Python requests
Hit the Etherpad-lite API with Python
Get Qiita trends with Python scraping
Get the number of Youtube subscribers
Coexistence of Python2 and 3 with CircleCI (1.0)
Get the trading price of virtual currency and create a chart with API of Zaif exchange
I compared the speed of Hash with Topaz, Ruby and Python
I tried scraping the ranking of Qiita Advent Calendar with Python
Count the number of Thai and Arabic characters well in Python
How to get followers and followers from python using the Mastodon API
[Python] How to get the first and last days of the month
Crawling with Python and Twitter API 2-Implementation of user search function
Get and set the value of the dropdown menu using Python and Selenium
Get the title and delivery date of Yahoo! News in Python
Get the number of readers of a treatise on Mendeley in Python
PhytoMine-I tried to get the genetic information of plants with Python
Check the existence of the file with python
[Python] Get the variable name with str
Get the minutes of the Diet via API
Get holidays with the Google Calendar API
[Python] Get the character code of the file
[Python] Get Python package information with PyPI API
Count the number of characters with echo
Get the width of the div on the server side with Selenium + PhantomJS + Python
I replaced the numerical calculation of Python with Rust and compared the speed
Calculate the shortest route of a graph with Dijkstra's algorithm and Python