-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmklist-retrofilmvault
executable file
·194 lines (180 loc) · 6.48 KB
/
mklist-retrofilmvault
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Fetch list of movies available from RetroFilmVault. All their movies
are claimed to be in the public domain.
Look up each title + year in IMDB and if only one title is returned
with the correct year, assume it is the correct IMDB title ID for
the movie.
Question: How does these two links add up?
http://www.motherjones.com/politics/2013/08/mlk-intellectual-property-problems/
https://retrofilmvault.com/listings/martin-luther-king-i-have-a-dream-1963/
"""
import argparse
import datetime
import json
import lxml.html
import movielib
import re
import urllib
import urllib2
import urlparse
from copy import deepcopy
def dumplist(list, name):
outlist = deepcopy(list)
# Rewrite freenessurl array to individual fields (freenessurl,
# freenessurl2, freenessurl3...) in predictable/sorted order
for i in outlist:
if 1 == len(outlist[i]['freenessurl']):
outlist[i]['freenessurl'] = outlist[i]['freenessurl'][0]
else:
sl = sorted(outlist[i]['freenessurl'])
outlist[i]['freenessurl'] = sl[0]
seq = 0
for u in sl[1:]:
field = "freenessurl%d" % (seq + 2)
outlist[i][field] = u
seq = seq + 1
movielib.savelist(outlist, name=name)
def fetch_movie_info(entryurl):
try:
root = lxml.html.fromstring(movielib.http_get_read(entryurl))
except urllib2.HTTPError as e:
return None
retval = {}
title = root.cssselect("h2[itemprop=name]")[0].text_content().strip()
title = title.replace(u"’", "'")\
.replace(u"‘", "'")\
.replace(u'\ufffd', "'")\
.replace(u'“', '"')\
.replace(u'”', '"')\
.replace(u"–", "-")\
.replace(u"…", "...")\
.replace(u"é", "e")\
.replace(u"ñ", "n")\
.replace(u"\xa0", " ") # nobreak space
retval['title'] = title
m = re.search("^(.+) (\d+)$", title)
if m:
y = int(m.group(2))
if 1875 < y and y < 3000:
retval['year'] = int(m.group(2))
retval['title'] = m.group(1)
y = root.cssselect("tr.listing_category_year td")
if y:
year = y[1].text_content()
try:
retval['year'] = int(year)
except ValueError:
# Workaround for
# 'https://retrofilmvault.com/listings/a-child-is-born-television/'
# listing 'Year: Television'
pass
cref = root.cssselect("tr.listing_category_public-domain-movie-category td")
if cref:
c = cref[1].text_content()
retval['category'] = c
print(retval)
return retval
def fetch_movie_list(args, l, url, name):
try:
root = lxml.html.fromstring(movielib.http_get_read(url))
except urllib2.HTTPError as e:
return None
lastref = root.cssselect("span.total_pages")
if lastref:
print(lastref[0].text)
last = int(lastref[0].text)
else:
last = 1
page = 1
while page <= last:
for a in root.cssselect("a.inventory[href]"):
entryurl = urlparse.urljoin(url, a.attrib['href'])
if is_in_list(l, entryurl):
continue
title = a.text_content().strip()
print entryurl
info = fetch_movie_info(entryurl)
if 'title' in info:
title = info['title']
if not info:
print("error: found broken link %s" % entryurl)
#print info
imdb = None
if info and 'year' in info:
year = info['year']
else:
year = None
entry = {
'status' : 'free',
'freenessurl' : [entryurl],
'title' : title,
'updated' : datetime.datetime.now().isoformat(),
}
if args.imdblookup:
print("IMDB search for %s %s" % (title, year))
imdb = movielib.imdb_find_one(title, year)
if imdb:
entry['imdblookup'] = '%s %d' % (title, year)
if not imdb:
imdb = entryurl
if year:
entry['year'] = year
if imdb not in l:
l[imdb] = entry
else:
if entryurl not in l[imdb]['freenessurl']:
l[imdb]['freenessurl'].append(entryurl)
for f in ['updated', 'imdblookup']:
if f in entry:
l[imdb][f] = entry[f]
print(imdb, l[imdb])
dumplist(l, name=name)
try:
pageurl = url + "&paged=%d" % page
root = lxml.html.fromstring(movielib.http_get_read(pageurl))
except urllib2.HTTPError as e:
return None
page += 1
return l
def is_in_list(list, url):
for e in list:
if url in list[e]['freenessurl']:
return True
return False
def loadlist(l, path):
try:
with open(path, 'rt') as input:
n = json.load(input)
for id in n.keys():
freenessurl = []
for field in ['freenessurl', 'freenessurl2', 'freenessurl3',
'freenessurl4', 'freenessurl5', 'freenessurl6',
'freenessurl7', 'freenessurl8', 'freenessurl9']:
if field in n[id] and n[id][field] not in freenessurl:
freenessurl.append(n[id][field])
del n[id][field]
n[id]['freenessurl'] = freenessurl
if not id in l:
l[id] = n[id]
else:
for url in n[id]['freenessurl']:
if url not in l[id]['freenessurl']:
l[id]['freenessurl'].append(url)
return l
except IOError as e:
return l
def main():
parser = argparse.ArgumentParser()
parser.add_argument('--imdblookup', action='store_true', default=False,
help='also find title IDs by searching for title/year in IMDB')
args = parser.parse_args()
url = "https://retrofilmvault.com/public-domain-movies/?listing_order=titles&listing_orderby=ASC"
path='free-movies-retrofilmvault.json'
outlist = {}
outlist = loadlist(outlist, path)
outlist = fetch_movie_list(args, outlist, url, name=path)
dumplist(outlist, name=path)
if __name__ == '__main__':
main()