Export Qiita's own article and push it to Github

Introduction

It is a code that exports the articles posted on Qiita to one file per article and pushes it to Github. I had the wisdom of my ancestors, so I used it as a reference. For reference, I wrote a Python script that exports all my posts using Qiita API v2 was the execution environment Python 2.7. So I modified the code a bit so that it works with 3.7.7 I'm using.

Execution environment

OS:macOS Python:3.7.7

Outline of processing

  1. Create one article for each article using the Qiita API in Python in the local environment.
  2. Push the created file to Github.

code

# -*- coding: utf-8 -*-

import json
import os
import sys
import requests
import subprocess

def abort(msg):
    print('Error!: {0}'.format(msg))
    sys.exit(1)

def get(url, params, headers):
    r = requests.get(url, params=params, proxies=proxies, headers=headers)
    return r

def post(url, data_dict, headers_dict):
    r = requests.post(url, data=json.dumps(data_dict),
                      proxies=proxies, headers=headers_dict)
    return r

def print_response(r, title=''):
    c = r.status_code
    h = r.headers
    print('{0} Response={1}, Detail={2}'.format(title, c, h))

def assert_response(r, title=''):
    c = r.status_code
    h = r.headers
    if c<200 or c>299:
        abort('{0} Response={1}, Detail={2}'.format(title, c, h))

class Article:
    def __init__(self, d):
        self._title      = d['title']
        self._html_body  = d['rendered_body']
        self._md_body    = d['body']
        self._tags       = d['tags']
        self._created_at = d['created_at']
        self._updated_at = d['updated_at']
        self._url        = d['url']

        user = d['user']
        self._userid   = user['id']
        self._username = user['name']

    def save_as_markdown(self):

        title = self._title
        body  = self._md_body.encode('utf8')

        filename = '{0}.md'.format(title)   
        fullpath = os.path.join(MYDIR, filename)
        #Change to binary mode
        #Reference) https://go-journey.club/archives/7113
        with open(fullpath, 'wb') as f:
            f.write(body)

#Specify the file output destination. Please specify the directory you want to output.
MYDIR = os.path.abspath("/Users/shin/github/Qiita")

proxies = {
    "http": os.getenv('HTTP_PROXY'),
    "https": os.getenv('HTTPS_PROXY'),
}

#Get Qiita access token
token = os.getenv('QIITA_ACCESS_TOKEN')
#If not specified in the environment variable, set as follows
# token = 'Access token'

headers = {
    'content-type'  : 'application/json',
    'charset'       : 'utf-8',
    'Authorization' : 'Bearer {0}'.format(token)
}

#List of posts by authenticated users
url = 'https://qiita.com/api/v2/authenticated_user/items'
params = {
    'page'     : 1,
    'per_page' : 100,
}
r = get(url, params, headers)
assert_response(r)
# print_response(r)

items = r.json()
print('{0} entries.'.format(len(items)))
for i,item in enumerate(items):
    print('[{0}/{1}] saving...'.format(i+1, len(items)))
    article = Article(item)
    article.save_as_markdown()

#Push to Github
#Reference) https://www.atmarkit.co.jp/ait/articles/2003/13/news031.html
subprocess.run(["cd", "/Users/shin/github/Qiita"])
#Add update contents to index (add to staging area) → Commit target
subprocess.run(["git", "add", "-A"])
#Commit to local repository
subprocess.run(["git", "commit", "-a", "-m", "AutomaticUpdate"])
#Reflect the contents of the local repository in the remote repository
subprocess.run(["git", "push", "origin", "master"])

reference

Recommended Posts

Export Qiita's own article and push it to Github
Get a global IP and export it to Google Spreadsheets
Read Python csv and export to txt
mkseldir --command to mkdir and select it further
How to install Cascade detector and how to use it
A high school student created a real-time language conversion service and pushed it to Github.