Created
March 30, 2018 02:51
-
-
Save inakagawa/0049ad7a656d200645c68bd410331868 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
import glob | |
import os | |
import re,datetime | |
import dateutil.parser | |
SRCDIR = 'src' | |
OUTDIR = 'out' | |
def get_dispinfo(dat): | |
keys_base = ['title', 'url'] | |
keys_user = ['id'] | |
out_list = [] | |
[out_list.append('{0}: {1}'.format(k, dat[k])) for k in keys_base] | |
[out_list.append('{0}: {1}'.format(k, dat['user'][k])) for k in keys_user] | |
return out_list | |
def normalize_datestr(s): | |
l = s.split("+") | |
return l[0] | |
def str_to_datetime(s): | |
return dateutil.parser.parse(s) | |
# return datetime.datetime.strptime(s, '%Y-%m-%dT%H:%M:%S') | |
def datetime_to_str(dt): | |
return dt.strftime("%Y%m%d_%H%M%S") | |
def format_doc(doc_info, doc_body): | |
doc_info.insert(0,'```') | |
doc_info.append('```\n') | |
doc_info.append(doc_body) | |
return '\n'.join(doc_info) | |
def get_src_filenames(): | |
globpat = os.path.join(SRCDIR, '*.json') | |
files = glob.glob(globpat) | |
return files | |
def generate_out_filename(json_dict, src_filename): | |
# src: ORIGFILENAME.json | |
# out: OUTDIR/username/YYYYMMDD_hhmmss_ORIGFILENAME.md | |
username = json_dict['user']['id'] | |
created_at = str_to_datetime(normalize_datestr(json_dict['created_at'])) | |
datestr = datetime_to_str(created_at) | |
src_base = (os.path.splitext(os.path.basename(src_filename)))[0] # path,ext を除去 | |
out_basename = '{0}_{1}.md'.format(datestr, src_base) | |
# outdir: OUTDIR/username | |
dir_by_user = os.path.join(OUTDIR, username) | |
if not os.path.isdir(dir_by_user): os.makedirs(dir_by_user) | |
# | |
out_file = os.path.join(dir_by_user, out_basename) | |
return out_file | |
def main(): | |
src_files = get_src_filenames() | |
for src_file in src_files: | |
with open(src_file,'r') as sf: | |
json_dict = json.load(sf) | |
doc_info = get_dispinfo(json_dict) | |
doc_body = json_dict['body'] | |
out_content = format_doc(doc_info, doc_body) | |
out_file = generate_out_filename(json_dict, src_file) | |
with open(out_file, 'w') as of: | |
of.write(out_content) | |
if __name__ == "__main__": | |
main() | |
""" | |
qiitaの出力ファイルのデータ構造例 | |
"rendered_body": 本文html | |
"body": 本文ソース | |
"coediting": false, | |
"comments_count": 0, | |
"created_at": "2016-11-07T16:25:08+09:00", | |
"group": null, | |
"id": "0e28e1ca3a17da7e666a", | |
"likes_count": 0, | |
"private": false, | |
"reactions_count": 0, | |
# .... | |
"title": "WP プラグイン 利用例(パンくず・ナビ)", | |
"updated_at": "2016-11-07T16:32:49+09:00", | |
"url": "https://srk-sd.qiita.com/inakagawa/items/0e28e1ca3a17da7e666a", | |
"user": { | |
"id": "inakagawa", | |
"permanent_id": 17730, | |
"profile_image_url": "https://qiita-image-store.s3.amazonaws.com/0/17730/profile-images/1473682242" | |
}, | |
""" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment