Last active
August 29, 2015 14:19
-
-
Save ap-Codkelden/54d45271e22a114dcd9c to your computer and use it in GitHub Desktop.
Convert Juick.com JSON backup to plain text
This file contains hidden or 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
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
import json | |
from datetime import datetime | |
import locale | |
import sys | |
from sys import platform | |
import argparse | |
import os.path | |
def process(nj): | |
if not args.out_file: | |
if '.' in args.file: | |
out_name = '.'.join([''.join((args.file).split('.')[:-1]),'txt']) | |
else: | |
out_name = '.'.join([args.file,'txt']) | |
else: | |
out_name = args.out_file | |
try: | |
outfile = open(out_name, 'w', encoding='utf-8') | |
except: | |
raise | |
if platform.startswith('win'): | |
dt_locale='rus_rus' | |
elif platform.startswith('linux'): | |
dt_locale='ru_RU' | |
locale.setlocale(locale.LC_ALL, locale=dt_locale) | |
dt = lambda d: datetime.strftime(datetime.strptime(d,'%Y-%m-%d %H:%M:%S'), | |
'%d %b %Y') | |
def start(): | |
return ''.join(['#',str(post[0]['mid']).zfill(10),' ','='*65,'\n']) | |
def end(): | |
return ''.join([' '*12,'-'*65,'\n']) | |
for post in nj: | |
outfile.write(start()) | |
tags = ''.join([', '.join(post[0]['tags']), '\n']) if 'tags' in post[0] else None | |
link = post[0]["photo"]["medium"] if 'photo' in post[0] else None | |
if tags: | |
outfile.write(tags) | |
b = ''.join([ | |
post[0]['body'].replace('\n','\\n'), | |
''.join([' ', link]) if link else '', | |
'\t', | |
dt(post[0]['timestamp']), | |
'\n' | |
]) | |
outfile.write(b) | |
if len(post)>1: | |
for i in range(1,len(post)): | |
c = ''.join([ | |
'\t ', | |
''.join([ | |
str(post[i]['rid']), | |
'=>', | |
str(post[i]['replyto']) | |
]) if 'replyto' in post[i] else str(post[i]['rid']), | |
''.join([ | |
' ', | |
post[i]['body'].replace('\n','\\n'), | |
'\t', | |
dt(post[i]['timestamp']), | |
'\n' | |
])]) | |
outfile.write(c) | |
outfile.write(end()) | |
outfile.close() | |
if __name__ == "__main__": | |
parser = argparse.ArgumentParser() | |
parser.add_argument('file') | |
parser.add_argument('-o','--out',help='out file',action='store', | |
default='',dest='out_file') | |
parser.add_argument('-r','--reverse',help='reversed output', | |
action='store_true',dest='reverse',default=False) | |
args = parser.parse_args() | |
print(args) | |
if os.path.isfile(args.file): | |
try: | |
with open(args.file, encoding='utf-8') as f: | |
nj = json.loads(f.read()) | |
if args.reverse: | |
nj = list(reversed(nj)) | |
process(nj) | |
except KeyboardInterrupt: | |
sys.exit() | |
except: | |
raise | |
else: | |
print("No file found.") | |
sys.exit() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment