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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
|
#! /bin/ruby
require 'json'
require 'open-uri'
# tmdb [movie_path] [db_path]
n = ARGV.size
mpath = n.positive? ? ARGV.shift : File.dirname(__FILE__)
dbpath = n > 1 ? ARGV.shift : File.join(mpath, 'db')
puts " movies : #{mpath}\n db : #{dbpath}"
API_KEY = JSON.parse(File.read(File.expand_path('~/.tmdb.json')))['apikey']
API_URL = 'https://api.themoviedb.org/3'.freeze
SEARCH_M = "#{API_URL}/search/movie?api_key=#{API_KEY}&language=en-US&page=1&include_adult=true&".freeze
SEARCH_T = SEARCH_M.sub(%r{/movie}, '/tv').freeze
FETCH_M = "#{API_URL}/movie/ID?api_key=#{API_KEY}".freeze
FETCH_T = FETCH_M.sub(%r{/movie}, '/tv').freeze
FETCH_CM = "#{API_URL}/movie/ID/credits?api_key=#{API_KEY}".freeze
FETCH_CT = FETCH_CM.sub(%r{/movie}, '/tv').freeze
URL_M = 'https://www.themoviedb.org/movie/'.freeze
URL_T = URL_M.sub(%r{/movie}, '/tv').freeze
URL_A = 'https://www.themoviedb.org/person/'.freeze
URL_I = 'https://www.themoviedb.org/t/p/original/'.freeze
ACTORS_N = 6
ASZ = 150
MSZ = 350
BLANK = 'blank.png'.freeze
DB = File.join(dbpath, 'db.json')
FIX = File.join(dbpath, 'fix.json')
FAIL = File.join(dbpath, 'failed.json')
DBA = File.join(dbpath, 'a')
DBM = File.join(dbpath, 'm')
HTML_I = File.join(dbpath, 'index.html')
HTML_M = File.join(dbpath, 'movies.html')
HTML_A = File.join(dbpath, 'actors.html')
HTML_F = File.join(dbpath, 'failed.html')
HTML_N = File.join(dbpath, 'new.html')
Dir.mkdir(dbpath) unless Dir.exist?(dbpath)
Dir.mkdir(DBA) unless Dir.exist?(DBA)
Dir.mkdir(DBM) unless Dir.exist?(DBM)
def write_db(next_db)
puts "write #{DB}"
File.open(DB, 'w') { |f| f << next_db.to_json }
end
def num
('1'..'9').inject('') { |r, i| "#{r}<div class=link><a class=dic href='##{i}'>#{i}</a></div>" }
end
def alpha
('A'..'Z').inject('') { |r, i| "#{r}<div class=link><a class=dic href='##{i}'>#{i}</a></div>" }
end
def menu(*links)
t = '<div id=menu>'
links.each { |link| t << "<a class=linkm href='#{link.downcase}.html'>#{link}</a>" }
t << '</div>'
end
def prelude(title)
f = "<html><head><title>#{title}</title><meta charset='utf-8' />\n"
f << '<link rel="stylesheet" href="db.css" />'
f << '<script src="lazy.js"></script>'
f << "</head><body>\n"
end
def write_index(next_db)
puts "write #{HTML_I}"
File.open(HTML_I, 'w') do |f|
f << prelude('Movies Index')
f << menu('Movies', 'Actors', 'New', 'Failed')
f << '<div id=toc>'
f << "<div id=adic class=dic>#{alpha}</div>\n"
f << "<div id=ndic class=dic>#{num}</div>\n"
letter = nil
next_db.each do |m|
l = m['title'][0].upcase
if l != letter
f << '</table></div>' unless letter.nil?
letter = l
f << "<div class=letter>\n <div name=#{letter} id=#{letter} class=alpha>#{letter}</div>\n<table class=index>"
end
f << "<tr class=entry><td class=movie><a class=link href='movies.html##{m['id']}'>#{m['title']}#{' - TV' if m['is_tv']}</a></td>\n"
f << " <td class=release>#{m['release_date'][0..3]}</td></tr>\n"
end
f << '</table></div></body></html>'
end
end
def write_actors(actors)
puts "write #{HTML_A}"
File.open(HTML_A, 'w') do |f|
f << prelude('Actors')
f << menu('Index', 'Movies', 'New', 'Failed')
f << '<div id=toc>'
f << "<div id=adic class=dic>#{alpha}</div>\n"
f << "<div id=ndic class=dic>#{num}</div>\n"
letter = nil
actors.keys.sort! { |a, b| a.downcase <=> b.downcase }.each do |aname|
l = aname[0].upcase
if l != letter
f << '</table></div>' unless letter.nil?
letter = l
f << "<div class=letter>\n <div name=#{letter} id=#{letter} class=alpha>#{letter}</div>\n<table class=index>"
end
d = actors[aname]
d['movies'].sort! { |a, b| b[2] <=> a[2] }
m = d['movies'].shift
f << "<tr class=entry><td class=actor><a class=link href='#{URL_A}#{d['id']}'>#{aname}</a></td>\n"
f << "<td class=movie><a class=link href='movies.html##{m[0]}'>#{m[1]}</a></td>"
f << "<td class=release>#{m[2][0..3]}</td></tr>"
d['movies'].each do |mov|
f << "<tr class=entry><td> </td><td class=link><a class=link href='movies.html##{m[0]}'>#{mov[1]}</a></td>"
f << "<td class=release>#{mov[2][0..3]}</td></tr>"
end
end
f << '</table></div></body></html>'
end
end
def __write_movies(fout, movies, actors = nil)
movies.each do |m|
url = m['is_tv'] ? URL_T : URL_M
img = (m['img'].nil? ? BLANK : "m/#{m['img']}")
fout << "<div class=movie id=#{m['id']}><div class=poster>"
fout << "<a href='#{url}#{m['id']}'><img class=lazy data-src=#{img} height=#{MSZ}px /></a></div>"
fout << '<div class=cont0><div class=info>'
fout << "<div class=title>#{m['title']}</div>"
fout << "<div class=original>(#{m['original_title']})</div>" if m['title'] != m['original_title']
if m['is_tv']
fout << "<div class=year>#{m['release_date'][0..3]}-#{m['last_air_date'][0..3]}</div>"
fout << "<div class=season>#{m['eps']}/#{m['number_of_episodes']} episodes -#{m['number_of_seasons']} seasons</div>"
else
fout << "<div class=year>#{m['release_date'][0..3]}</div>"
end
# fout << "<div class=fn>[#{m['fname']}]</div>"
fout << "</div><div class=cast>\n"
m['cast'].each do |a|
img = a['img']
img = (img.nil? ? BLANK : "a/#{img}")
fout << "<div class=actor><h2>#{a['name']}</h2>"
fout << "<a href='#{URL_A}#{a['id']}'><img class=lazy data-src=#{img} width=#{ASZ}px /></a></div>\n"
unless actors.nil?
actors[a['name']] ||= { 'id' => a['id'], 'movies' => [] }
actors[a['name']]['movies'] << [m['id'], m['title'], m['release_date']]
end
end
fout << "</div><div class=overview>#{m['overview']}</div>\n"
fout << "</div></div>\n"
end
end
def write_movies(movies)
puts "write #{HTML_M}"
actors = {}
File.open(HTML_M, 'w') do |f|
f << prelude('Movies')
f << menu('Index', 'Actors', 'New', 'Failed') << "\n"
__write_movies(f, movies, actors)
f << '</body></html>'
end
actors
end
def write_failed(failed)
puts "write #{HTML_F}"
File.open(HTML_F, 'w') do |f|
f << prelude('Failed')
f << menu('Index', 'Actors', 'New', 'Failed') << "\n"
failed.each do |fn|
f << "<div class=movie>#{fn}</div>\n"
end
f << '</body></html>'
end
end
def write_new(movies)
puts "write #{HTML_N}"
File.open(HTML_N, 'w') do |f|
f << prelude('New')
f << menu('Index', 'Movies', 'Actors', 'Failed') << "\n"
__write_movies(f, movies)
f << '</body></html>'
end
end
def download(id, path, base)
return nil if path.nil?
fn = id.to_s + File.extname(path)
dst = File.join(base, fn)
unless File.exist? dst
puts " get : #{dst}"
File.open(dst, 'wb') { |f| f.write URI(URL_I + path).open.read }
end
system("magick #{dst} -resize x#{MSZ} #{dst}") if base == DBM
system("magick #{dst} -resize #{ASZ}x #{dst}") if base == DBA
fn
end
def get_all(data)
id = data['id']
data['img'] = download(id, data['poster_path'], DBM)
data['cast'] = []
url = data['is_tv'] ? FETCH_CT : FETCH_CM
JSON.parse(URI(url.sub(/ID/, id.to_s)).open.read)['cast']
.sort { |a, b| a['order'] <=> b['order'] }.each_with_index do |a, i|
break if i == ACTORS_N
a['img'] = download(a['id'], a['profile_path'], DBA)
data['cast'] << a
end
data
end
def filter_results(res, data)
sel = res.select { |r| (r['release_date'] || 'nope')[0..3] == data[:year] }
return nil if sel.empty?
if sel.size > 1
puts " #{sel.map { |s| "#{s['title']} #{s['release_date'] || '?'}" }.join("\n ")}"
s = sel.select { |r| r['stitle'] =~ /#{data[:sequel]}/ } unless data[:sequel].nil?
sel = s unless s.nil? || s&.empty?
s = sel.select { |r| r['stitle'] == data[:name] }
sel = s unless s.empty?
return nil if sel.size != 1
end
puts " => : '#{sel[0]['title']}' #{sel[0]['id']}"
sel[0]['id']
end
def normalise_results(res)
res.each do |r|
r['release_date'] = r['first_air_date'] unless r.key? 'release_date'
r['title'] = r['name'] unless r.key? 'title'
r['original_title'] = r['original_name'] unless r.key? 'original_title'
r['stitle'] = r['title'].downcase.gsub(/[^ a-z0-9]/, '')
end
res
end
def fetch(data)
url = data['is_tv'] ? FETCH_T : FETCH_M
[] << JSON.parse(URI(url.sub(/ID/, data['id'].to_s)).open.read)
end
def search(data)
url = data['is_tv'] ? SEARCH_T : SEARCH_M
res = JSON.parse(URI.parse(url + URI.encode_www_form('query' => data[:name])).read)['results']
normalise_results(res)
filter_results(res, data)
end
def process_fname(data)
puts "#{data['fname']} :"
fname = data['fname'].downcase.tr('àáâäçèéêëìíîïòóôöùúûü', 'aaaaceeeeiiiioooouuuu').gsub('_', ' ')
fname = fname[..-5] unless data['is_tv']
name, *more = fname.split '-'
year = more[-1]
sequel = more[0] if more.size == 2
data.merge(name: name, year: year, sequel: sequel)
end
def process(data)
data = process_fname(data)
data['id'] = search(data) if data['id'].nil?
return nil if data['id'].nil?
data.merge!(normalise_results(fetch(data))[0])
get_all(data)
end
def in_prev_db?(fname, prev_db)
prev_db.find { |i| i['fname'] == fname } if @db_idx.include?(fname)
end
failed = []
fix_db = File.exist?(FIX) ? JSON.parse(File.read(FIX)) : {}
prev_db = File.exist?(DB) ? JSON.parse(File.read(DB)) : []
@db_idx = prev_db.collect { |m| m['fname'] }
next_db = []
def skip?(fname)
fname =~ /\.srt$/ || fname =~ /\.sub$/ || fname =~ /\.jpg$/ || fname =~ /^db/
end
def eps(path)
n = 0
Dir.glob(File.join(path, '*')) do |fn|
n += 1 unless skip?(fn)
end
n
end
# FIXME: list seasons -> x/N
Dir.glob(File.join(mpath, '*')) do |path|
fname = path.split('/')[-1]
next if skip?(fname)
data = in_prev_db?(fname, prev_db)
if data.nil?
is_tv = File.directory?(path)
mtime = File.mtime(path).to_i.to_s
eps = is_tv ? eps(path) : 0
data = process('id' => fix_db[fname], 'fname' => fname, 'is_tv' => is_tv, 'eps' => eps, 'mtime' => mtime)
end
failed << fname if data.nil?
next_db << data unless data.nil?
end
next_db.sort! { |a, b| a['title'].downcase <=> b['title'].downcase }
write_db(next_db)
write_index(next_db)
write_actors(write_movies(next_db))
write_failed(failed)
write_new(next_db.sort { |a, b| b['mtime'] <=> a['mtime'] }[..20])
puts 'FAILED :'
File.open(FAIL, 'w') { |f| f << failed.to_json }
failed.each { |fn| puts " -> #{fn}" }
# jq '.[] | select(.fname | test("Fargo"))' db.json
# curl --request GET --url 'https://api.themoviedb.org/3/tv/60622/season/2/credits?api_key=c4202eaa738af60ae7a784c349a0cc63' | jq '.cast'
# 'https://api.themoviedb.org/3/tv/series_id/season/season_number/credits?languag
|