-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathOld Sports Scanner.py
430 lines (383 loc) · 12.1 KB
/
Old Sports Scanner.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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
#
# Copyright (c) 2010 Plex Development Team. All rights reserved.
#
import re, os, sys, titlecase
import Media, VideoFiles
import ConfigParser
class RegexHandler(object):
def handle(self, match, filename):
pass
def getRegexs(self):
return []
class F1Handler(RegexHandler):
def getRegexs(self):
return [
'^(?P<year>[12][0-9][0-9][0-9]?)(?P<month>[0-9][0-9]?)(?P<day>[0-9][0-9]?)_F1(_R(?P<race>[0-9][0-9]*?))?_(?P<session>.*?)_?(?P<channel>Sky|BBC?)[^_]*_(?P<location>.*?)(\.pt(?P<part>[1-9][0-9]*?))?.*',
'^(?P<year>[12][0-9][0-9][0-9]?)(?P<month>[0-9][0-9]?)(?P<day>[0-9][0-9]?)_F1 ([12][0-9][0-9][0-9])?[- _.]+Race (?P<race>[0-9][0-9]*?)[- _.]+(?P<location>.*?)[- _.]+(?P<session>.*?)[- _.]+(?P<channel>Sky|BBC?)(HD)?(\.pt(?P<part>[1-9][0-9]*?))?.*',
]
SESSIONS = {
"practice1" : 0,
"practice 1" : 0,
"p1" : 0,
"practice2" : 1,
"practice 2" : 1,
"p2" : 1,
"practice3" : 2,
"practice 3" : 2,
"p3" : 2,
"qualifying" : 3,
"qual" : 3,
"" : 4,
}
RACES = {
2012: {
"Australia": "1",
"Malaysia": "2",
"China": "3",
"Bahrain": "4",
"Spain": "5",
"Monaco": "6",
"Canada": "7",
"Europe": "8",
"Great Britain": "9",
"German": "10",
"Hungary": "11",
"Belgium": "12",
"Italy": "13",
"Singapore": "14",
"Japan": "15",
"Korea": "16",
"India": "17",
"Abu Dhabi": "18",
"USA": "19",
"Brasil": "20",
}
}
def handle(self, match, filename):
channel = match.group('channel')
if channel == "SKY":
channel = "Sky"
showName = "Formula 1 " + channel
year = int(match.group('year'))
location = match.group('location')
race = match.group('race')
if race is None:
race = self.RACES[year][location]
episode = int(race) * 10
session = match.group('session')
if session is not None:
episode += self.SESSIONS[session.lower()]
else:
episode += 4
episode *= 10
title = "Race %s %s %s" % (race, location, session)
part = match.group('part')
if part is not None:
episode += int(part) - 1
title += " #" + part
episode += 1
return Media.Episode(showName, year, episode, title, year)
class F1Handler1(RegexHandler):
LOCATIONS = {
2013: [
"Australia",
"Malaysia",
"China",
"Bahrain",
"Spain",
"Monaco",
"Canada",
"Europe",
"Great Britain",
"German",
"Hungary",
"Belgium",
"Italy",
"Singapore",
"Japan",
"Korea",
"India",
"Abu Dhabi",
"USA",
"Brasil",
]
}
def getRegexs(self):
return [
'^(?P<year>[12][0-9][0-9][0-9]?)(?P<month>[0-9][0-9]?)[0-9]?(?P<day>[0-9][0-9]?)_F1.*',
]
def handle(self, match, filename):
if filename.lower().find("bbc") >= 0:
channel = "BBC"
else:
channel = "SKY"
showName = "Formula 1 " + channel
year = int(match.group('year'))
# location = match.group('location')
match = re.search("F1_R(?P<race>[0-9][0-9]*?)", filename);
race = int(match.group("race"))
episode = int(race) * 10
if filename.find("_P1") >= 0:
sessionNum = 0
sessionName = "P1"
elif filename.find("_P2") >= 0:
sessionNum = 1
sessionName = "P2"
elif filename.find("_P3") >= 0:
sessionNum = 2
sessionName = "P3"
elif filename.find("Q1") >= 0:
sessionNum = 3
sessionName = "Q1"
elif filename.find("Q2") >= 0:
sessionNum = 4
sessionName = "Q2"
elif filename.find("Q3") >= 0:
sessionNum = 5
sessionName = "Q3"
elif filename.find("QF1") >= 0:
sessionNum = 3
sessionName = "Q1"
elif filename.find("QF2") >= 0:
sessionNum = 4
sessionName = "Q2"
elif filename.find("QF3") >= 0:
sessionNum = 5
sessionName = "Q3"
elif filename.find("_QF_Q2Q3") >= 0:
sessionNum = 4
sessionName = "Q23"
elif filename.find("QF") >= 0:
sessionNum = 3
sessionName = "Q"
else:
sessionNum = 7
sessionName = "Race"
episode += sessionNum
episode *= 10
location = self.LOCATIONS[year][int(race) - 1]
title = "Race %s %s %s" % (race, location, sessionName)
p1 = filename.find(".pt")
if p1 >= 0:
p2 = filename.find(".", p1 + 1)
part = filename[p1 + 3:p2]
else:
part = None
if part is not None:
episode += int(part) - 1
title += " #" + part
episode += 1
return Media.Episode(showName, year, episode, title, year)
class GP2Handler(RegexHandler):
# 20120421_GP2_Round2_Race 1_SkySD_Bahrain.pt1.thebox.e45.avi
SESSIONS = {
"practice" : 0,
"qualifying" : 1,
"race1" : 2,
"race 1" : 2,
"race2" : 3,
"race 2" : 3,
}
def getRegexs(self):
return ['^(?P<year>[12][0-9][0-9][0-9]?)[0-9][0-9][0-9][0-9]_GP2_Round[_ ]?(?P<round>[0-9][0-9]*?)_(?P<session>.*?)_?(Sky|BBC)[^_]*_(?P<location>.*?)(\.pt(?P<part>[1-9][0-9]*?))?\.thebox.*']
def handle(self, match, filename):
showName = "GP2"
year = int(match.group('year'))
round = int(match.group('round'))
session = match.group('session')
location = match.group('location')
episode = round * 100 + self.SESSIONS[session.lower()] * 10
title = "Round %d %s %s" % (round, location, session)
part = match.group('part')
if part is not None:
episode += int(part) - 1
title += " #" + part
episode += 1
return Media.Episode(showName, year, episode, title, year)
class UFCHandler1(RegexHandler):
def getRegexs(self):
return [
'^ufc(?P<year>[12][0-9][0-9][0-9]?)(?P<month>[0-9][0-9]?)[0-9]?(?P<day>[0-9][0-9]?)_F1.*',
]
def handle(self, match, filename):
if filename.lower().find("bbc") >= 0:
channel = "BBC"
else:
channel = "SKY"
showName = "Formula 1 " + channel
year = int(match.group('year'))
# location = match.group('location')
match = re.search("F1_R(?P<race>[0-9][0-9]*?)", filename);
race = int(match.group("race"))
episode = int(race) * 10
if filename.find("_P1") >= 0:
sessionNum = 0
sessionName = "P1"
elif filename.find("_P2") >= 0:
sessionNum = 1
sessionName = "P2"
elif filename.find("_P3") >= 0:
sessionNum = 2
sessionName = "P3"
elif filename.find("_QF_Q1") >= 0:
sessionNum = 3
sessionName = "Q1"
elif filename.find("Q1") >= 0:
sessionNum = 3
sessionName = "Q1"
elif filename.find("Q2") >= 0:
sessionNum = 4
sessionName = "Q2"
elif filename.find("Q3") >= 0:
sessionNum = 5
sessionName = "Q3"
elif filename.find("_QF_Q2Q3") >= 0:
sessionNum = 4
sessionName = "Q23"
elif filename.find("_QF") >= 0:
sessionNum = 3
sessionName = "Q"
else:
sessionNum = 6
sessionName = "Race"
episode += sessionNum
episode *= 10
location = self.LOCATIONS[year][int(race) - 1]
title = "Race %s %s %s" % (race, location, sessionName)
p1 = filename.find(".pt")
if p1 >= 0:
p2 = filename.find(".", p1 + 1)
part = filename[p1 + 3:p2]
else:
part = None
if part is not None:
episode += int(part) - 1
title += " #" + part
episode += 1
return Media.Episode(showName, year, episode, title, year)
class RegexMatcher:
def __init__(self,
name,
regex,
show="%s", showArgs="show",
season="%s", seasonArgs="season",
episode="%s", episodeArgs="episode",
title="Episode %s", titleArgs = "episode",
year="%s", yearArgs="year"):
self.name = name
self.regex = regex
self.show = show
self.showArgs = showArgs
self.season = season
self.seasonArgs = seasonArgs
self.episode = episode
self.episodeArgs = episodeArgs
self.title = title
self.titleArgs = titleArgs
self.year = year
self.yearArgs = yearArgs
class GenericScanner():
def __init__(self):
config = ConfigParser.ConfigParser()
configFilename = os.path.expanduser("~/.mySportsScanner.cfg")
config.read(configFilename)
matcherMap = {}
for section in config.sections():
matcher = RegexMatcher(section, config.get(section, "regex"))
order = int(config.get(section, "order"))
matcherMap[order] = matcher
if config.has_option(section, "show"):
matcher.show = config.get(section, "show")
if config.has_option(section, "showArgs"):
matcher.showArgs = config.get(section, "showArgs")
if config.has_option(section, "season"):
matcher.season = config.get(section, "season")
if config.has_option(section, "seasonArgs"):
matcher.seasonArgs = config.get(section, "seasonArgs")
if config.has_option(section, "episode"):
matcher.episode = config.get(section, "episode")
if config.has_option(section, "episodeArgs"):
matcher.episodeArgs = config.get(section, "episodeArgs")
if config.has_option(section, "title"):
matcher.title = config.get(section, "title")
if config.has_option(section, "titleArgs"):
matcher.titleArgs = config.get(section, "titleArgs")
if config.has_option(section, "year"):
matcher.year = config.get(section, "year")
if config.has_option(section, "yearArgs"):
matcher.yearArgs = config.get(section, "yearArgs")
self.matcherList = []
for order in sorted(matcherMap.iterkeys()):
matcher = matcherMap[order]
self.matcherList.append(matcher)
def getString(self, match, format, argsString):
args = ()
for group in argsString.split(","):
if group != "":
arg = match.group(group)
if group == "show":
arg = arg.replace(".", " ").replace("-", " ").replace("_", " ")
elif group == "episode" or group == "year":
if arg is None and group == "year":
arg = 2012
arg = int(arg)
args = args + (arg,)
value = format % args
if value == "":
value = None
return value
def scan(self, filename):
for matcher in self.matcherList:
match = re.search(matcher.regex, filename, re.IGNORECASE)
if match:
showName = titlecase.titlecase(self.getString(match, matcher.show, matcher.showArgs).replace(".", " ").replace("-", " ").replace("_", " "))
season = int(self.getString(match, matcher.season, matcher.seasonArgs))
episode = int(self.getString(match, matcher.episode, matcher.episodeArgs))
year = self.getString(match, matcher.year, matcher.yearArgs)
if year is not None:
year = int(year)
title = self.getString(match, matcher.title, matcher.titleArgs)
partMatch = re.search("p(ar)?t[ _-](?P<part>[0-9]+)", filename, re.IGNORECASE)
if partMatch:
part = int(partMatch.group("part"))
episode = episode * 10 + part - 1
title = title + " Part " + str(part)
return Media.Episode(showName, season, episode, title, year)
REGEX_HANDLERS = [
#F1Handler(),
F1Handler1(),
#GP2Handler(),
]
def customScan(filename):
for handler in REGEX_HANDLERS:
for regex in handler.getRegexs():
match = re.search(regex, filename, re.IGNORECASE)
if match:
return handler.handle(match, filename)
def Scan(path, files, mediaList, subdirs):
# Scan for video files.
print files
VideoFiles.Scan(path, files, mediaList, subdirs)
scanner = GenericScanner()
# Run the select regexps we allow at the top level.
for file in files:
filename, ext = os.path.splitext(os.path.basename(file))
show = customScan(filename)
if show is None:
show = scanner.scan(filename)
if show is None:
show = scanner.scan(file)
if show is not None:
print(filename)
print(" Show: %s\n Season: %d\n episode: %d\n title: %s\n Year: %s" % (show.show, show.season, show.episode, show.name, show.year))
show.parts.append(file)
mediaList.append(show)
if __name__ == '__main__':
if len(sys.argv) < 2:
path = "files"
else:
path = sys.argv[1]
files = [os.path.join(path, file) for file in os.listdir(path)]
media = []
Scan(path[1:], files, media, [])
print "Total: %d" % len(media)