-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathepisoderenamer.py
executable file
·328 lines (284 loc) · 12.5 KB
/
episoderenamer.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
#!/usr/bin/env python
# Episode renamer - Copyright 2008 Stavros Korokithakis
# Released under the GNU GPL.
# You can find the latest version at http://github.com/skorokithakis/
import urllib
import urllib2
import optparse
import re
import os
import os.path
import sys
import subprocess
import random
import htmlentitydefs
import md5
from HTMLParser import HTMLParser
try:
import simplejson as json
except ImportError:
import json
__version__ = "0.4.6"
SERIES_PARSER = [
re.compile("^.*?s *(?P<series>\d+) *e *(?P<episode>\d+).*\.(?P<extension>.*?)$", re.IGNORECASE),
re.compile("^.*?(?P<series>\d+)x(?P<episode>\d+).*\.(?P<extension>.*?)$", re.IGNORECASE),
re.compile("^(?:.*?\D|)(?P<series>\d{1,2})(?P<episode>\d{2})(?:\D.*|)\.(?P<extension>.*?)$", re.IGNORECASE),
]
class Show:
def __init__(self, title=""):
self.title = title
self.attributes = {}
self.episodes = {}
def get_page(page_url):
try:
return urllib2.urlopen(page_url).read()
except urllib2.HTTPError, error:
print "An HTTP error occurred, HTTP code %s." % error.code
sys.exit()
def search_show(name, site):
"""Search Google for the page best matching the given show name."""
google_url = "http://www.google.com/search?q=site%%3A%s+%s" % (site["domain"], urllib.quote(name))
google_url = "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=site%%3A%s+%s" % (
site["domain"], urllib.quote(name))
results = json.load(urllib.urlopen(google_url))
url = results['responseData']['results'][0]['url']
show_id = re.search(site["urlparser"], url).group(1)
return show_id
def parse_imdbapi(show_id, options):
"""Get the episodes from imdbapi."""
url = 'http://imdbapi.poromenos.org/json/?name=%s' % urllib.quote(show_id)
if options.year:
url += '&year=%s' % urllib.quote(options.year)
results = json.load(urllib2.urlopen(url))
if not results:
print "Show not found."
sys.exit()
elif 'shows' in results:
print "Found multiple shows titled '%s'; specify year with -y or --year." % show_id
for show in results['shows']:
print '%s (%d)' % (show['name'], show['year'])
sys.exit()
show = Show(results.keys()[0])
for episode in results[show.title]["episodes"]:
show.episodes[(episode["season"], episode["number"])] = {"title": episode["name"]}
return show
def parse_imdb(show_id, options):
"""Parse an IMDB page."""
site = {"url": "http://www.imdb.com/title/%s/epdate",
"domain": "imdb.com",
"urlparser": "imdb\.com\/title\/(.*?)\/",
}
if options.google:
show_id = search_show(show_id, site)
print "Found show ID \"%s\"..." % show_id
page = get_page(site["url"] % show_id)
from BeautifulSoup import BeautifulSoup, BeautifulStoneSoup
# Remove   (nbsp) entities.
page = page.replace(" ", "")
page = HTMLParser().unescape(page)
soup = BeautifulSoup(page, convertEntities=BeautifulStoneSoup.ALL_ENTITIES)
matches = re.search("(?:"|)(.*?)(?:"|) *?\((.*?)\)", soup.title.string)
title = matches.group(1)
try:
year = matches.group(2)
except IndexError:
year = None
show = Show(title)
if options.use_atomic_parsley:
show.attributes["artwork"] = urllib2.urlopen(soup.find("a", attrs={"name": "poster"}).findNext("img")["src"]).read()
show.attributes["year"] = year
table = soup.find("h4").findNext("table")
for tr in table.findChildren("tr")[1:]:
tds = tr.findChildren("td")
season, episode = tds[0].contents[0].split(".")
show.episodes[(int(season), int(episode))] = {"title": tds[1].a.contents[0]}
show.episodes[(int(season), int(episode))]["rating"] = tds[2].contents[0]
return show
def parse_epguides(show_id, options):
"""Parse an epguides page."""
site = {"url": "http://epguides.com/%s/",
"domain": "epguides.com",
"urlparser": "epguides.com\/(.*?)\/",
}
if options.google:
show_id = search_show(show_id, site)
print "Found show ID \"%s\"..." % show_id
page = get_page(site["url"] % show_id)
from BeautifulSoup import BeautifulSoup, BeautifulStoneSoup
page = HTMLParser().unescape(page)
soup = BeautifulSoup(page, convertEntities=BeautifulStoneSoup.ALL_ENTITIES)
page = unicode(soup).replace("\n", "")
show = Show()
try:
show.title = re.search("""<h1><a href="http://.*?">(.*?)</a></h1>""", page).groups()[0]
except AttributeError:
print "Could not find show title, cannot continue."
sys.exit()
episodes = re.findall("\d+. +(?P<season>\d+) *\- *(?P<episode>\d+).*?<a.*?>(?P<name>.*?)</a>", page)
for season, episode, name in episodes:
show.episodes[(int(season), int(episode))] = {"title": name}
return show
def parse_filename(show, filename, file_mask):
for parser in SERIES_PARSER:
matches = parser.search(filename)
try:
match_dict = matches.groupdict()
break
except AttributeError:
continue
else:
raise Exception("Filename not matched.")
series = int(match_dict["series"])
episode = int(match_dict["episode"])
extension = match_dict["extension"]
info_dictionary = {"show": show.title,
"series_num": series,
"episode_num": episode,
"extension": extension}
try:
info_dictionary.update(show.episodes[(series, episode)])
new_filename = file_mask % info_dictionary
except KeyError:
print 'Episode name for "%s" not found.' % filename
raise Exception
new_filename = re.sub("[\\\/\:\*\"\?\<\>\|]", "", new_filename)
return new_filename, info_dictionary
def rename_files(show, file_mask, preview=False, use_ap=False, use_filenames=False, base_dir=None, filenames=None):
if base_dir is None:
base_dir = os.getcwd()
if not use_filenames:
filenames = os.listdir(base_dir)
for filename in filenames:
try:
new_filename, info_dictionary = parse_filename(show, filename, file_mask)
except:
print 'Episode name for "%s" not found.' % filename
continue
filename = os.path.join(base_dir, filename)
new_filename = os.path.join(base_dir, new_filename)
print "Renaming \"%s\" to \"%s\"..." % (filename, new_filename.encode("ascii", "replace"))
if not preview:
if use_ap:
# The temp_filename shenanigans are necessary because AP sometimes
# chokes if it's set to overwrite the file.
temp_filename = filename + str(random.randint(10000, 99999))
arguments = ["AtomicParsley",
filename,
"-o", temp_filename,
"--TVShowName", info_dictionary["show"],
"--stik", "TV Show",
"--TVSeasonNum", str(info_dictionary["series_num"]),
"--TVEpisodeNum", str(info_dictionary["episode_num"]),
"--TVEpisode", show.episodes[(info_dictionary["series_num"], info_dictionary["episode_num"])]["title"],
"--title", show.episodes[(info_dictionary["series_num"], info_dictionary["episode_num"])]["title"]]
if "year" in show.episodes[(info_dictionary["series_num"], info_dictionary["episode_num"])]:
arguments.extend(["--year", show.episodes[(info_dictionary["series_num"], info_dictionary["episode_num"])]["year"]])
elif "year" in show.attributes:
arguments.extend(["--year", show.attributes["year"]])
artwork_file = None
if "artwork" in show.episodes[(info_dictionary["series_num"], info_dictionary["episode_num"])]:
artwork_filename = md5.md5(str(random.randint(10000, 100000))).hexdigest()
artwork_file = open(artwork_filename, "wb")
artwork_file.write(show.episodes[(info_dictionary["series_num"], info_dictionary["episode_num"])]["artwork"])
artwork_file.close()
arguments.extend(["--artwork", "REMOVE_ALL", "--artwork", artwork_filename])
elif "artwork" in show.attributes:
artwork_filename = md5.md5(str(random.randint(10000, 100000))).hexdigest()
artwork_file = open(artwork_filename, "wb")
artwork_file.write(show.attributes["artwork"])
artwork_file.close()
arguments.extend(["--artwork", "REMOVE_ALL", "--artwork", artwork_filename])
proc = subprocess.Popen(tuple(arguments))
proc.wait()
if proc.returncode == 0:
os.remove(filename)
try:
os.rename(temp_filename, new_filename)
except OSError:
print "There was an error while renaming the file."
if artwork_file:
os.remove(artwork_filename)
else:
try:
os.rename(filename, new_filename)
except OSError:
print "There was an error while renaming the file."
def main():
parser = optparse.OptionParser(usage="%prog [options] <show id from the URL> <show base directory>", version="Episode renamer %s\nThis program is released under the GNU GPL." % __version__)
parser.add_option("-a",
"--use-atomic-parsley",
dest="use_atomic_parsley",
action="store_true",
help="use AtomicParsley to fill in the files' tags")
parser.add_option("-d",
"--use-imdbapi",
dest="use_imdbapi",
action="store_true",
help="use imdbapi.poromenos.org")
parser.add_option("-e",
"--use-epguides",
dest="use_epguides",
action="store_true",
help="use epguides.com")
parser.add_option("-i",
"--use-imdb",
dest="use_imdb",
action="store_true",
help="use imdb.com")
parser.add_option("-m",
"--mask",
dest="mask",
default="%(show)s - S%(series_num)02dE%(episode_num)02d - %(title)s.%(extension)s",
metavar="MASK",
action="store",
type="string",
help="the filename mask to use when renaming (default: \"%default\")")
parser.add_option("-p",
"--preview",
dest="preview",
action="store_true",
help="don't actually rename anything")
parser.add_option("-g",
"--google",
dest="google",
action="store_true",
help="search Google for the episode list based on the show name")
parser.add_option("-f",
"--files",
dest="use_filenames",
action="store_true",
help="specify filenames to match starting in current dir (do NOT specify a base dir)")
parser.add_option("-y",
"--year",
dest="year",
action="store",
help="Specify the first year the series ran")
parser.set_defaults(preview=False)
(options, arguments)=parser.parse_args()
if len(arguments) < 1:
parser.print_help()
sys.exit(1)
if options.use_imdb:
parser = parse_imdb
elif options.use_epguides:
parser = parse_epguides
else:
parser = parse_imdbapi
show_id = arguments[0]
if options.use_filenames:
filenames = arguments[1:]
base_dir = None
if filenames == []:
print "If -f or --files is specified, you must give a list of files."
sys.exit(1)
else:
try:
filenames = None
base_dir = arguments[1]
except IndexError:
base_dir = None
show = parser(show_id, options)
rename_files(show, options.mask, options.preview, options.use_atomic_parsley, options.use_filenames, base_dir, filenames)
print "Done."
if __name__ == "__main__":
main()