-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlist-stats
executable file
·144 lines (132 loc) · 5.37 KB
/
list-stats
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#!/usr/bin/env python
"""
Calculate and report some statistics for each data source JSON file.
"""
import argparse
import csv
import gzip
import json
import re
import movielib
class IMDB(object):
titles = {}
def __init__(self):
path = 'title.basics.tsv.gz'
with gzip.open(path, 'rb') as f:
reader = csv.reader(f, delimiter="\t", quoting=csv.QUOTE_NONE)
for row in reader:
info = {
'title': row[3], # Original title
'year': row[5],
}
if '\\N' != row[7]:
info['duration'] = row[7] # minutes
self.titles[row[0]] = info
def __getitem__(self, key):
if key in self.titles:
return self.titles[key]
else:
return None
def main():
parser = argparse.ArgumentParser()
parser.add_argument('files', metavar='N', type=str, nargs='*',
default=['free-movies-manual.json'],
help='input file(s)')
parser.add_argument('--list-unique', action='store_true', default=False)
parser.add_argument('--list-all', action='store_true', default=False)
parser.add_argument('--create-complete', action='store_true', default=False)
args = parser.parse_args()
if args.create_complete:
ids = IMDB()
else:
ids = None
complete = {}
s = {}
i = {}
for file in args.files:
with open(file, 'rt') as input:
l = json.load(input)
imdb = 0
noimdb = 0
for e in l:
if not dict is type(l[e]): # Ignore comment entries
continue
if args.list_all or \
('status' in l[e] and "free" == l[e]['status']):
m = re.match('https?://www.imdb.com/title/(.+)/', e)
if m:
imdb = imdb + 1
imdbid = m.group(1)
if e not in complete:
complete[e] = {
'file': [],
'freenessurls': [],
'status': 'free',
}
if ids:
imdbinfo = ids[imdbid]
else:
imdbinfo = None
if imdbinfo:
complete[e].update(imdbinfo)
complete[e]['file'].append(file)
# Keep list sorted to get predictable output
complete[e]['file'] = \
sorted(complete[e]['file'])
for key in ('archive', 'archive1', 'archive2',
'archive3', 'archive4', 'archive5',
'freenessurl', 'freenessurl1', 'freenessurl2',
'freenessurl3', 'freenessurl4', 'freenessurl5',
'freenessurl6', 'freenessurl7', 'freenessurl8',
'freenessurl9', 'freenessurl10', 'freenessurl11',
'freenessurl12', 'freenessurl13', 'freenessurl14',
'freenessurl15', 'freenessurl16'):
if key in l[e] and 'n/a' != l[e][key]:
url = l[e][key]
if url not in complete[e]['freenessurls']:
complete[e]['freenessurls'].append(url)
# Keep list sorted to get predictable output
complete[e]['freenessurls'] = \
sorted(complete[e]['freenessurls'])
else:
noimdb = noimdb + 1
s[file] = {
'imdb' : imdb,
'noimdb' : noimdb,
}
i[e] = l[e]
usum = 0
xsum = 0
for file in sorted(s.keys()):
unique = 0
for imdburl in complete.keys():
if complete[imdburl]['file'] == [file]:
unique = unique + 1
print("%5d entries (%5d unique) with and %5d without IMDB title ID in %s"
% (s[file]['imdb'], unique, s[file]['noimdb'], file))
usum = usum + unique
xsum = xsum + s[file]['noimdb']
if args.list_unique:
def sortbyyear(imdburl):
if 'year' in i[imdburl]:
return int(i[imdburl]['year']), imdburl
else:
return imdburl
for imdburl in sorted(complete, key=sortbyyear):
if complete[imdburl]['file'] == [file]:
if 'year' in i[imdburl]:
y = i[imdburl]['year']
else:
y = ""
if 'title' in i[imdburl]:
t = i[imdburl]['title']
else:
t = ""
msg = " %s %4s %s" % (imdburl, y, t)
print(msg.encode('utf-8'))
print("%5d unique IMDB title IDs in total, %d only in one list, %d without IMDB title ID" %
(len(complete.keys()), usum, xsum))
if args.create_complete:
movielib.savelist(complete, 'free-complete.json')
if __name__ == '__main__':
main()