2014年6月2日月曜日

niconicoからあらかじめ取ってきたjsonをパースして必要な情報を表示するだけのスクリプト

#!/usr/bin/env python3
"""nicovideo.py: parse json and show video info"""
__author__ = "Yuto Tokunaga"
import sys
import os
import glob
import json
def print_usage():
usage = "usage: nicovideo.py [option] [file/directory]\n" +\
"\n" +\
" options list: list files\n" +\
" show: show file desctiption\n" +\
"\n"
print(usage)
def list_files(dirname):
files = glob.glob(dirname + '/*.json')
for f in files:
jsonfile = open(f)
jsondata = json.load(jsonfile)
print("{0}: {1}".format(f.split('/')[-1].rstrip('.json'), jsondata['videoTitle']))
print("\t{0}".format(jsondata['thumbDescription']))
jsonfile.close()
def show_file(file):
filename = ""
if os.path.exists(file):
if file.split('.')[-1] != 'json':
print("file is not JSON file")
return
filename = file
elif os.path.exists(file + '.json'):
filename = file + '.json'
else:
print("file not exists!")
return
jsonfile = open(filename)
jsondata = json.load(jsonfile)
print("{0}: {1}".format(filename.split('/')[-1].rstrip('.json'), jsondata['videoTitle']))
print("\t{0}".format(jsondata['description']))
print("")
"""parse tag"""
tagstr = jsondata['tagList'].strip('/')
tagdatas = json.loads(tagstr)
taglist = []
for tag in tagdatas:
taglist.append(tag['tag'])
print(taglist)
if __name__ == '__main__':
if len(sys.argv) < 2:
print_usage()
elif sys.argv[1] == 'list':
if len(sys.argv) < 3:
list_files('.')
else:
list_files(sys.argv[2])
elif sys.argv[1] == 'show':
if len(sys.argv) < 3:
print("invalid format!")
else:
show_file(sys.argv[2])
else:
print("invalid format!")
view raw nicovideo.py hosted with ❤ by GitHub



Python楽しい✌('ω'✌ )三✌('ω')✌三( ✌'ω')✌

0 件のコメント:

コメントを投稿