cinematools/add_metadata.py

50 lines
1.6 KiB
Python
Raw Normal View History

#!/usr/bin/python
from optparse import OptionParser
import json
import codecs
import sys
import ox
def add_metadata(films, country):
api = ox.API('https://indiancine.ma/api/')
for info in films:
extra = api.getMetadata(id=info['imdbId'], keys=[
'language', 'productionCompany', 'director',
'runtime', 'alternativeTitles',
'color', 'sound',
'summary', 'country',
'isSeries',
'title',
'originalTitle', 'year'
])['data']
if 'isSeries' in extra or ('country' in extra and not country in extra['country']):
info['delete'] = True
print 'deleting', info['imdbId'], info.get('title')
continue
if 'originalTitle' in extra:
info['alternativeTitles'] = [[info['title'], '']]
info['title'] = extra.pop('originalTitle')
else:
info['title'] = extra['title']
for key in extra:
if key not in info:
info[key] = extra[key]
print info['imdbId'], info['title']
return filter(lambda f: not f.get('delete', False), films)
if __name__ == '__main__':
usage = "usage: %prog [options] country films.json"
parser = OptionParser(usage=usage)
(opts, args) = parser.parse_args()
if len(args) != 2:
parser.print_help()
sys.exit(1)
country, filename = args
with open(filename) as fd:
films = json.load(fd)
films = add_metadata(films, country)
with codecs.open(filename, 'w', encoding='utf-8') as fd:
json.dump(films, fd, indent=1, ensure_ascii=False)