This repository has been archived by the owner on Dec 20, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtvshow_parser.py
239 lines (201 loc) · 7.5 KB
/
tvshow_parser.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
import sys
import getopt
import yaml
import os
import signal
from modules import file, serie, movie
__app_name__ = "tvshow_parser"
__version__ = 0.5
shortArgs = {
"h:": "help",
"c:": "config file",
"d:": "debug level",
}
longArgs = {
"type=": "type of media - TV/Movie - Default: TV",
"meta-module=": "metadata module to use - Default trakt",
"add-module=": "add module to use - Default: itunes",
"sub-module=": "subtitle module to use - Default: subliminal",
"sub=": "subtitle file to use - Default srt/sub with same filename as input",
"notification-module=": "notification module to use - Default: growl",
"season=": "Set season # - Default: Parse from filename",
"episode=": "Set episode # - Default: Parse from filename",
"name=": "Set name - Default: Parse from filename",
"hd=": "Set HD quality: True/False - Default: Parse from filename",
"filename=": "Use this string for parsing info instead of actual filename",
"no-sub": "Don't add subtitle",
"no-convert": "Don't convert",
"no-metadata": "Don't add metadata"
}
file_handler = {}
def parseArgs(argv, config):
try:
opts, args = getopt.getopt(argv, str(shortArgs.keys()), longArgs.keys())
except getopt.GetoptError:
usage()
sys.exit(2)
config['file'] = {'metadata': {}}
try:
for opt, arg in opts:
if opt in ("-h", "--help"):
usage()
sys.exit()
elif opt == "-c":
config = loadConfig(arg)
if not config:
print "Couldn't read config file " + str(arg)
sys.exit(2)
elif opt == "--type":
config['type'] = arg.upper()
elif opt == "--meta-module":
config['modules']["metadata"] = arg
elif opt == "--add-module":
config['modules']["addTo"] = arg
elif opt == "--sub-module":
config['modules']['subtitle'] = arg
elif opt == "--sub":
config['sub'] = arg
elif opt == "sendNotification":
config['modules']['notification'] = arg
elif opt == "--season":
config['file']['metadata']['season'] = arg
elif opt == "--episode":
config['file']['metadata']['episode'] = arg
elif opt == "--name":
config['file']['metadata']['name'] = arg
elif opt == "--hd":
config['file']['metadata']['hd'] = arg
elif opt == "--filename":
config['file']['metadata']['filename'] = arg
elif opt == "--no-sub":
config['actions']['sub'] = False
elif opt == "--no-convert":
config['actions']['convert'] = False
elif opt == "--no-metadata":
config['actions']['metadata'] = False
except:
print "something wrong with config..."
sys.exit(2)
theFile = "".join(args)
config['file']['path'] = os.path.dirname(theFile)
config['file']['name'] = os.path.basename(theFile)
if config['file']['path'] == '.' or config['file']['path'] == '':
config['file']['path'] = os.getcwd()
if not theFile:
print "No input..."
sys.exit(2)
return config
def loadConfig(file):
global config
try:
f = open(file)
config = yaml.load(f)
f.close()
except IOError:
return False
return config
def usage():
print "tvshow-parser"
for arg in shortArgs:
print "{0:25} {1:15s}".format("-" + arg.replace(":", ""), shortArgs[arg])
for arg in longArgs:
print "{0:25} {1:15s}".format("--" + arg, longArgs[arg])
def loadModules(config):
res = {}
for module in config['modules']:
path = module.replace("_", ".")
try:
exec("from modules." + path + " import " + config['modules'][module])
m = sys.modules['modules.' + path + '.' + config['modules'][module]]
res[module] = getattr(m, config['modules'][module].capitalize())(config)
except Exception as e:
print e
sys.exit(1)
return res
def cleanup(signum, frame):
print "Cleaning up..."
file_handler.removeTemp()
sys.exit(0)
def convert(config, modules):
global file_handler
# setup signal
signal.signal(signal.SIGINT, cleanup)
# init file_handler
file_handler = file.File(config)
print config['file']
# If file isn't a video, try to find one (or more)
if not os.path.isfile(os.path.join(config['file']['path'], config['file']['name'])):
print "Not a file? Exiting..."
sys.exit(1)
if not file_handler.isVideo():
files = file_handler.findVideo()
for f in files['files']:
config['file']['path'] = files['path']
config['file']['name'] = os.path.basename(f)
# Call convert again with new file
convert(config, modules)
else:
# Okay, we have a file
# Init media_handler for the type
if config['type'] == "TV":
media_handler = serie.Serie(config)
elif config['type'] == "MOVIE":
media_handler = movie.Movie(config)
print "Not implemented yet!"
sys.exit(1)
# LETS GO!
media_handler.parseFilename()
# Move file to temp folder to work with
file_handler.moveToTemp()
file_handler.cdToTemp()
if config['actions']['metadata']:
try:
metadata = modules['metadata_fetch'].getInfo({
"name": media_handler.name,
"season": media_handler.season,
"episode": media_handler.episode,
"hd": media_handler.hd
})
print metadata
modules['metadata_fetch'].getArtwork(media_handler.name, media_handler.season)
except:
print "Couldn't fetch metadata... exiting..."
file_handler.removeTemp()
sys.exit(1)
if config['actions']['sub']:
try:
modules['subtitle'].getSubtitle()
except:
print "Something wrong with fetching subtitle."
if config['actions']['convert']:
try:
modules['convert'].convert()
except:
print "Oh Oh. Something wrong!"
file_handler.removeTemp()
sys.exit(1)
if config['actions']['metadata']:
try:
modules['metadata_add'].addMetadata(metadata)
except:
print "Couldn't add metdata. Exiting..."
file_hander.removeTemp()
sys.exit(1)
# convert()
# addTags()
# optimize()
if modules['add'].add(os.path.join(config['temp']['path'], config['temp']['name'])):
file_handler.removeTemp()
modules['notification'].sendNotification(description=config['file']['name'])
def main(argv):
# Load config file
root = os.path.dirname(argv[0])
configFile = os.path.join(root, "config.yaml")
config = loadConfig(configFile)
# Parse arguments
parseArgs(argv[1:], config)
# Load modules for metadata, addTo and subtitle
modules = loadModules(config)
convert(config, modules)
if __name__ == "__main__":
main(sys.argv)