Skip to content

Instantly share code, notes, and snippets.

@yuntan
Created June 1, 2014 17:10
Show Gist options
  • Save yuntan/f1a53b239daed7584d26 to your computer and use it in GitHub Desktop.
Save yuntan/f1a53b239daed7584d26 to your computer and use it in GitHub Desktop.
ニコ動からあらかじめ取ってきた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!")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment