/
imdbcons.py
executable file
·369 lines (330 loc) · 11.9 KB
/
imdbcons.py
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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
#!/usr/bin/env python
try:
from imdbpie import Imdb, Imdb
from imdbpie.exceptions import HTTPError
from PIL import Image
except ImportError:
print '\nError: Modules "imdb-pie" and "Pillow" must be Installed\n'
quit()
from urllib import urlretrieve
import os
import shutil
import sys
reload(sys)
sys.setdefaultencoding('utf8')
TEMP_DIR = 'temp_imdbcon'
TEMP_JPG = os.path.join(TEMP_DIR, 'temp_image.jpg')
TEMP_PNG = os.path.join(TEMP_DIR, 'temp_image.png')
EMPTY_PNG = os.path.join(TEMP_DIR, 'temp_empty_square_png.png')
MAGIC_SCRIPT = os.path.join(TEMP_DIR, 'set_icon_magic_temp.py')
ACCEPTED_EXTENSIONS = (
'webm', 'mkv', 'flv', 'avi', 'wmv', 'mp4',
'mpg', 'mp2', 'mpeg', 'mpe', 'mpv', 'm4v'
)
MOVIE_DICT = {
'path': '',
'title': '',
'imdb_id': None,
'imdb_obj': None,
'duplicates': []
}
MAGIC_SCRIPT_STRING = """
import Cocoa
import sys
Cocoa.NSWorkspace.sharedWorkspace().setIcon_forFile_options_(
Cocoa.NSImage.alloc().initWithContentsOfFile_(
sys.argv[1].decode('utf-8')
),
sys.argv[2].decode('utf-8'), 0
) or sys.exit("Unable to set file icon")
"""
PROCESSES = {
'download': 'Downloading cover %s from IMDB.',
'resize': 'Resizing cover to %s.',
'square': 'Pasting cover to square PNG.',
'set_icon': 'Setting icon to directory.',
'temp': 'Creating temporary files.',
'clean': 'Cleaning temporary files.',
'search': 'Searching IMDB for title.',
'complete': 'All processes are complete.',
}
ARGV_EXAMPLES = {
'-m': {
'use': 'Set icons for all sub-folders within main movie folder',
'ex': 'path/to/Movies',
},
'-a': {
'use': 'Set icons for ALL files and folders under main movie folder',
'ex': 'path/to/Movies',
},
'-s': {
'use': 'Set icon for single folder/file',
'ex': 'path/to/Inception',
},
'-id': {
'use': 'Set icon for folder/file with a vague title based on IMDB id',
'ex': 'tt0060153 path/to/Batman',
}
}
class Parser:
def __init__(self):
self.tag = ''
self.arg1 = ''
self.arg2 = ''
self.valid = True
self.get_args()
self.parsed = self.tag, self.arg1, self.arg2
def show_examples(self):
self.valid = False
print ''
for key in ARGV_EXAMPLES:
use = ARGV_EXAMPLES[key]['use']
ex = ARGV_EXAMPLES[key]['ex']
print '\t%s :\t%s\n\t\t\t%s %s\n' % (key, use, key, ex)
def get_args(self):
try:
self.tag = sys.argv[1]
except IndexError:
self.show_examples()
return
if self.tag not in ARGV_EXAMPLES:
self.show_examples()
return
try:
self.arg1 = sys.argv[2]
except IndexError:
self.show_examples()
return
if self.tag == '-id':
try:
self.arg2 = sys.argv[3]
except IndexError:
self.show_examples()
return
class Display:
def __init__(self):
self.bar_width = .1 # multiplied by 100
self.total_processes = 0 # int for number of processes
self.completed_processes = 0
self.current_title = ''
self.progress_bar = ''
self.errors_caught = []
def update_progress_bar(self):
"""Update self.progress_bar string based on percentage"""
if not self.total_processes:
return
percent = (100 * self.completed_processes) / self.total_processes
filled = int(percent * self.bar_width)
unfilled = int((100 * self.bar_width) - filled)
self.progress_bar = ' [%s%s] - %s%%' % (filled * '#', unfilled * '_', percent)
def update_current_process(self, process):
"""Print self.current_process and self.progress_bar"""
self.update_progress_bar()
t = self.current_title if len(self.current_title) < 20 \
else self.current_title[:17] + '...'
p = process if len(process) < 30 \
else process[:27] + '...'
message = '%s || %s: %s\r' % (self.progress_bar, t, p)
print ' ' * 78 + '\r',
print message,
sys.stdout.flush()
class IMDBcon:
def __init__(self):
self.parser = Parser()
self.directory = ''
self.imdb = Imdb()
self.cover_size = 214, 317
self.square_size = 317, 317
self.current = MOVIE_DICT
self.all_files = []
self.display = Display()
def update_display(self, process, args=None):
"""Send process to self.display to print to screen"""
if args:
process = PROCESSES[process] % args
else:
process = PROCESSES[process]
self.display.update_current_process(process)
def make_empty_square(self):
"""Make transparent .png image"""
image = Image.new('RGBA', self.square_size, (0, 0, 0, 0))
image.save(EMPTY_PNG, 'PNG')
def make_magic_script(self):
"""Make temporary magic 'set_icon.py' script"""
with open(MAGIC_SCRIPT, 'w') as script:
script.write(MAGIC_SCRIPT_STRING)
def make_temp_files(self):
"""Make temporary files"""
if os.path.isdir(TEMP_DIR):
shutil.rmtree(TEMP_DIR)
os.mkdir(TEMP_DIR)
self.make_empty_square()
self.make_magic_script()
def remove_temp_dir(self):
"""Remove temporary directory"""
self.display.current_title = ''
self.update_display('clean')
shutil.rmtree(TEMP_DIR)
def set_current(self, dict_item=None, path=''):
"""Set self.current 'title' and 'path'"""
if dict_item:
self.current = dict_item
elif path:
self.current['path'] = path
self.current['title'] = os.path.splitext(os.path.basename(path))[0]
self.display.current_title = self.current['title']
def set_id(self, imdb_id):
if os.path.isdir(self.current['path']):
id_path = os.path.join(self.current['path'], '.imdb_id')
with open(id_path, 'w') as id_file:
id_file.write(imdb_id)
self.current['imdb_id'] = imdb_id
def get_current_title(self):
"""Set self.current.imdb to Imdb Title object"""
self.update_display('search')
imdb_id = os.path.join(self.current['path'], '.imdb_id')
# User can use preset imdb_id for full accuracy
if self.current['imdb_id']:
try:
self.current['imdb_obj'] = self.imdb.get_title_by_id(self.current['imdb_id'])
except HTTPError:
error = 'Bad IMDB id for "%s" (%s)' % (
self.current['title'], self.current['imdb_id'])
self.display.errors_caught.append(error)
return False
elif os.path.isfile(imdb_id):
try:
with open(imdb_id) as id_file:
self.current['imdb_obj'] = self.imdb.get_title_by_id(
''.join(id_file.read().split()))
except HTTPError:
error = 'Bad IMDB id for "%s"' % self.current['title']
self.display.errors_caught.append(error)
return False
else:
try:
titles = self.imdb.search_for_title(self.current['title'])
temp = titles[0] # Not an Imdb Title object
self.current['imdb_obj'] = self.imdb.get_title_by_id(temp['imdb_id'])
if os.path.isdir(self.current['path']):
with open(imdb_id, 'w') as id_file:
id_file.write(temp['imdb_id'])
except IndexError:
error = 'No Titles Found for "%s"' % self.current['title']
self.display.errors_caught.append(error)
return False
if self.current['imdb_obj'].cover_url:
return True
else:
error = 'No Cover Image Found for "%s"' % self.current['title']
self.display.errors_caught.append(error)
return False
def retrieve_cover(self):
"""Download .jpg cover file from IMDB"""
url = self.current['imdb_obj'].cover_url
self.update_display('download', url)
urlretrieve(url, TEMP_JPG)
def resize_icon(self):
"""Set .jpg cover to self.cover_size"""
self.update_display('resize', str(self.cover_size))
image = Image.open(TEMP_JPG)
resized = image.resize(self.cover_size, Image.ANTIALIAS)
resized.save(TEMP_JPG)
def square_icon(self):
"""Convert .jpg cover to .png squared cover"""
self.update_display('square')
background = Image.open(EMPTY_PNG)
cover = Image.open(TEMP_JPG)
offset = (50, 0)
background.paste(cover, offset)
background.save(TEMP_PNG)
os.remove(TEMP_JPG)
def set_icon_magic(self):
"""Run 'set_icon.py' script"""
self.update_display('set_icon')
os.system('python2.6 %s "%s" "%s"' % (MAGIC_SCRIPT, TEMP_PNG, self.current['path']))
def set_icon(self):
"""Set directory icon to matching IMDB cover image"""
if not self.get_current_title():
return
self.retrieve_cover()
self.resize_icon()
self.square_icon()
self.set_icon_magic()
for item in self.current['duplicates']:
self.set_current(dict_item=item)
self.set_icon_magic()
os.remove(TEMP_PNG)
def exit_message(self):
"""Display exit message along with any errors"""
self.display.update_current_process('')
print(PROCESSES['complete'])
if self.display.errors_caught:
for error in self.display.errors_caught:
print(error)
else:
print('No Errors.')
print
def is_duplicate(self, item):
for existing in self.all_files:
if item['title'] == existing['title']:
existing['duplicates'].append(item)
return True
return False
def find_all(self):
"""Get list of all subdirectories and their files in directory"""
for root, dirs, files in os.walk(self.directory):
for directory in dirs:
item = {
'path': os.path.join(root, directory),
'title': directory,
'imdb_id': None,
'imdb_obj': None,
'duplicates': []
}
if not self.is_duplicate(item):
self.all_files.append(item)
if not self.parser.tag == '-a':
continue
for filename in files:
split = os.path.splitext(filename)
title, ext = split
if ext[1:] not in ACCEPTED_EXTENSIONS:
continue
item = {
'path': os.path.join(root, filename),
'title': title,
'imdb_id': None,
'duplicates': []
}
if not self.is_duplicate(item):
self.all_files.append(item)
self.display.total_processes = len(self.all_files)
def set_icons(self):
"""Set icons for all sub-directories in directory"""
self.find_all()
for item in self.all_files:
self.set_current(dict_item=item)
self.set_icon()
self.display.completed_processes += 1
def run(self):
if not self.parser.valid:
return
tag, arg1, arg2 = self.parser.parsed
self.make_temp_files()
print ''
if tag in ('-m', '-a'):
self.directory = arg1
self.set_icons()
if tag == '-s':
self.set_current(path=arg1)
self.set_icon()
if tag == '-id':
self.set_current(path=arg2)
self.set_id(arg1)
self.set_icon()
self.remove_temp_dir()
self.exit_message()
if __name__ == '__main__':
imdbcon = IMDBcon()
imdbcon.run()