Skip to content

Instantly share code, notes, and snippets.

@pandanote-info
Last active March 24, 2019 07:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pandanote-info/309a0555d3d0603fa515b33fe0335824 to your computer and use it in GitHub Desktop.
Save pandanote-info/309a0555d3d0603fa515b33fe0335824 to your computer and use it in GitHub Desktop.
GitHub APIのアクセストークンを使ってGistにアップロードしたファイルについての情報を読み出し、そのリストをmarkdown形式で生成するためのPython3のプログラム。Gistの貼り付け先となるWordpressの記事の情報はMariaDBより取得している。
#!/usr/bin/env python3
import os
import re
import json
from requests_oauthlib import OAuth2Session
import mysql.connector
github_client_key_file = 'github_client_key.json'
client_id = ''
client_secret = ''
access_token = ''
with open(github_client_key_file) as infile:
text = infile.read()
keys = json.loads(text)
access_token = keys["access_token"]
db_config_file = 'db_config.json'
dbconfig = {}
with open(db_config_file) as dbfile:
t = dbfile.read()
dbconfig = json.loads(t)
github = OAuth2Session()
github.headers['Authorization'] = 'token '+access_token;
r = github.get('https://api.github.com/users/pandanote-info')
if r.status_code != 200:
sys.exit(1)
rr = r.json()
count = rr['public_gists']+rr['private_gists']
pages = int((count+99)/100)
conn = mysql.connector.connect(user=dbconfig["user"],password=dbconfig["password"],host=dbconfig["host"],database=dbconfig["dbname"])
pattern = re.compile('.*/([0-9a-f]+)$')
title_pattern = re.compile('^([^_]+_[^_]+_[^_]+_[^_]+_)')
for i in range(pages):
r = github.get('https://api.github.com/users/pandanote-info/gists?page='+str(i+1)+'&per_page=100')
rr = r.json()
for j in range(len(rr)):
for k in rr[j]["files"].keys():
cur = conn.cursor()
print("## "+title_pattern.sub('\\1<br/>',k))
print("* Created at: "+rr[j]["created_at"])
if rr[j]["created_at"] != rr[j]["updated_at"]:
print("* Updated at: "+rr[j]["updated_at"])
print("* Descrption: "+rr[j]["description"])
if rr[j]["public"]:
print("* [Link to Gist]("+rr[j]["html_url"]+")")
m = pattern.match(rr[j]["url"])
#print("* "+m.group(1))
cur.execute("select id,post_title from wp_posts where post_content like '%/"+m.group(1)+"%' and post_status='publish'")
reflist = {}
for (id,post_title) in cur:
reflist[id] = post_title
if len(reflist)>0:
print("* Referenced from: ")
for k,v in reflist.items():
print(" * ["+v+"](https://pandanote.info/?p="+str(k)+")")
print()
cur.close
conn.close
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment