-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjson2csv
executable file
·54 lines (48 loc) · 1.77 KB
/
json2csv
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#!/usr/bin/env python3
"""
Generate list of all free IMDB entries as CSV.
"""
import argparse
import codecs
import csv
import json
import sys
def main():
parser = argparse.ArgumentParser()
parser.add_argument('files', metavar='N', type=str, nargs='*',
default=['free-movies-manual.json'],
help='input file(s)')
args = parser.parse_args()
keys = ['status', 'year', 'idurl', 'title', 'freenessurl', 'archive',
'wp', 'wdurl', 'vodo', 'comment']
completeset = {}
writer = csv.DictWriter(sys.stdout, keys)
for file in args.files:
with open(file, 'rt', encoding='utf-8') as input:
j = json.load(input)
for id in j.keys():
if not id in completeset:
completeset[id] = j[id]
else:
#print(file,id)
if 'year' not in completeset[id] and 'year' in j[id]:
completeset[id]['year'] = j[id]['year']
if 'freenessurl' in completeset[id] \
and 'freenessurl' in j[id] \
and j[id]['freenessurl'] != completeset[id]['freenessurl']:
completeset[id]['archive'] = j[id]['freenessurl']
#writer.writerow(keys)
for id in sorted(completeset.keys()):
if -1 == id.find("/www.imdb.com/title/"):
continue
if 'status' not in completeset[id] or 'free' != completeset[id]['status']:
continue
completeset[id]['idurl'] = id
#print(j[e])
fields = list(completeset[id].keys())
for k in fields:
if k not in keys:
del completeset[id][k]
writer.writerow(completeset[id])
if __name__ == '__main__':
main()