I will show you how to use ** Python ** and ** Qiita API v2 ** to output the number of VIEWs, likes, and stocks of your articles posted to Qiita to CSV.
It is a program that uses Qiita API v2
from Python
to get the title, the number of VIEWs, the number of likes, and the number of stocks, and displays them in CSV format on the standard output.
Replace the xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
part of the program source below with your own access token.
The access token can be obtained from "Settings"-"Applications"-"Personal Access Token" on the Qiita page.
getview.py
# -*- coding: utf-8 -*-
"""
Created on Wed Dec 11 23:44:00 2019
@author: yasushi-jp
"""
import requests
import json
url = 'https://qiita.com/api/v2/authenticated_user/items'
params = { "page" : "1", "per_page" : "100"}
headers = {"content-type" : "application/json", "Authorization" : "Bearer xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"}
res = requests.get(url, headers=headers, params=params)
list = res.json()
total_views_cnt = 0
total_likes_cnt = 0
total_stocks_cnt = 0
print("title,View count,Like count,Stock count")
for item in list:
item_id = item['id']
title = item['title']
likes_cnt = item['likes_count']
total_likes_cnt += likes_cnt
url = 'https://qiita.com/api/v2/items/' + item_id
res = requests.get(url, headers=headers)
json = res.json()
views_cnt = json['page_views_count']
total_views_cnt += views_cnt
url = 'https://qiita.com/api/v2/items/' + item_id + '/stockers'
res = requests.get(url, headers=headers)
users = res.json()
stocks_cnt = len(users)
total_stocks_cnt += stocks_cnt
print(title + ", " + str(views_cnt) + ", " + str(likes_cnt) + ", " + str(stocks_cnt))
print("total, " + str(total_views_cnt) + ", " + str(total_likes_cnt) + ", " + str(total_stocks_cnt))
I'm passing the path through the installed Python and calling the getview.py
I just created to redirect the results to a file.
Replace "C: \ DK \ Anaconda3 \ envs \ tf_env" with the directory where you installed Python.
getview.bat
@echo off
set PATH=%PATH%;C:\DK\Anaconda3\envs\tf_env
python getview.py > viewcount.csv
pause
When you execute (double-click) getview.bat
created above, viewcount.csv
will be created.
Recommended Posts