public this repo is viewable by everyone
Description: Simple to use TheTVDB.com API in Python
Clone URL: git://github.com/dbr/tvdb_api.git
Added (semi-)automated TV episode namer. Works, but needs clean-up and 
more testing
dbr (author)
4 days ago
commit  e01ec1b6726ef201a7e1bbed958538f049a58c52
tree    28f5addc2730642c1330c1d23ce2ab30b5cef975
parent  1ad79d5b64df3abdd297928d73acc3f4c823d5e5
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
...
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
0
@@ -0,0 +1,222 @@
0
+#!/usr/bin/env python
0
+#encoding:utf-8
0
+
0
+#
0
+# TVNamer.py
0
+# Automatic TV episode namer.
0
+# Uses data from www.thetvdb.com via tvdb_api
0
+#
0
+__author__ = "dbr/Ben"
0
+__version__ = "0.1"
0
+
0
+import os,sys,re
0
+from optparse import OptionParser
0
+from tvdb_api import tvdb
0
+
0
+config={}
0
+
0
+config['with_ep_name'] = '%(showname)s - [%(seasno)02dx%(epno)02d] - %(epname)s.%(ext)s'
0
+config['without_ep_name'] = '%(showname)s - [%(seasno)02dx%(epno)02d].%(ext)s'
0
+
0
+config['name_parse'] = [
0
+ re.compile('''
0
+ ^(
0
+ [\w\. ]*
0
+ )
0
+ (?= \- |\.)?
0
+ s(\d+)e(\d+)\D
0
+ ''', re.IGNORECASE|re.VERBOSE ),
0
+
0
+ re.compile('''
0
+ ^(
0
+ [\w\. ]*
0
+ )
0
+ (?= \- |\.)?
0
+ .*?
0
+ (\d+)x(\d+)\D
0
+ ''', re.IGNORECASE|re.VERBOSE ),
0
+
0
+ re.compile('''
0
+ ^(
0
+ [\w\. ]*
0
+ )
0
+ (?= \- )?
0
+ \D+
0
+ (\d+?)(\d{2})
0
+ \D''', re.IGNORECASE|re.VERBOSE ),
0
+]
0
+
0
+config['valid_filename_chars'] = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!@£$%^&*()_+=-[]{}\"'.,<>`~? "
0
+
0
+def findFiles(args):
0
+ allfiles=[]
0
+ for x in args:
0
+ if os.path.isdir(x):
0
+ for sf in os.listdir(x):
0
+ newpath = os.path.join(x,sf)
0
+ if os.path.isfile(newpath):
0
+ allfiles.append(newpath)
0
+ #end if isfile
0
+ #end for sf
0
+ elif os.path.isfile(x):
0
+ allfiles.append(x)
0
+ #end if isdir
0
+ #end for x
0
+ return allfiles
0
+#end findFiles
0
+
0
+def processNames(names):
0
+ allEps=[]
0
+ for f in names:
0
+ filepath,filename = os.path.split( f )
0
+ filename,ext = os.path.splitext( filename )
0
+ for r in config['name_parse']:
0
+ match = r.match(filename)
0
+ if match:
0
+ showname,seasno,epno = match.groups()
0
+ showname = showname.replace("."," ").strip()
0
+ seasno,epno = int(seasno), int(epno)
0
+ allEps.append({ 'showname':showname,
0
+ 'seasno':seasno,
0
+ 'epno':epno,
0
+ 'filepath':filepath,
0
+ 'filename':filename,
0
+ 'ext':ext
0
+ })
0
+ break
0
+ else:
0
+ print "Invalid name %s"%(f)
0
+ #end for r
0
+ #end for f
0
+
0
+ return allEps
0
+#end processNames
0
+
0
+def formatName(cfile):
0
+ orig_ext = cfile['ext']
0
+ cfile['ext'] = cfile['ext'].replace(".","",1)
0
+ if cfile['epname']:
0
+ n = config['with_ep_name'] % (cfile)
0
+ else:
0
+ n = config['without_ep_name'] % (cfile)
0
+ #end if epname
0
+ cfile['ext'] = orig_ext
0
+ return n
0
+#end formatName
0
+
0
+def cleanName(name):
0
+ return ''.join( [c for c in name if c in config['valid_filename_chars']] )
0
+
0
+
0
+def renameFile(oldfile,newfile,force=False):
0
+ new_exists = os.access(newfile,os.F_OK)
0
+ if new_exists:
0
+ sys.stderr.write("New filename already exists.. ")
0
+ if force:
0
+ sys.stderr.write("overwriting\n")
0
+ os.rename(oldfile,newfile)
0
+ else:
0
+ sys.stderr.write("skipping\n")
0
+ return False
0
+ #end if force
0
+ else:
0
+ os.rename(oldfile,newfile)
0
+ return True
0
+ #end if new_exists
0
+
0
+
0
+def main():
0
+ parser = OptionParser()
0
+
0
+ parser.add_option("-d","--debug",action="store_true",default=False,dest="debug", help="Show debugging info")
0
+ parser.add_option("-b","--batch",action="store_false",dest="interactive",help="Selects first search result, requires no human intervention once launched",default=False)
0
+ parser.add_option("-i","--interactive",action="store_true",dest="interactive",help="Interactivly prompt for show",default=True)
0
+ parser.add_option("-f","--force",action="store_true",default=False,dest="force",help="Skips checking if new filename already exists before renaming")
0
+
0
+ opts,args = parser.parse_args()
0
+ if len(args) == 0:
0
+ parser.error("No filenames or directories supplied")
0
+ #end if len(args)
0
+
0
+ allFiles = findFiles(args)
0
+ validFiles = processNames(allFiles)
0
+
0
+ if len(validFiles) == 0:
0
+ sys.stderr.write("No valid files found")
0
+ sys.exit(1)
0
+
0
+ print "Starting tvdb"
0
+ t = tvdb(debug = opts.debug, interactive = opts.interactive)
0
+ print "Done"
0
+ print "Starting processing files"
0
+ always=False
0
+ for cfile in validFiles:
0
+ epname = t[ cfile['showname'] ][ cfile['seasno'] ][ cfile['epno'] ]['name']
0
+ if epname:
0
+ cfile['epname'] = epname
0
+ else:
0
+ cfile['epname'] = None
0
+ sys.stderr.write("Episode name not found for %s" % ( cfile['fullpath']) )
0
+ #end if epname
0
+
0
+ newname = formatName( cfile )
0
+ newname = cleanName(newname)
0
+ oldfile = os.path.join( cfile['filepath'],cfile['filename'] + cfile['ext'] )
0
+ newfile = os.path.join( cfile['filepath'],newname )
0
+ print "*"*20
0
+ print "Old name: %s" % ( cfile['filename'] + cfile['ext'] )
0
+ print "New name: %s" % ( newname )
0
+
0
+ if always:
0
+ rename_result = renameFile(oldfile,newname,force=opts.force)
0
+ if rename_result:
0
+ print "..auto-renaming"
0
+ else:
0
+ print "..not renamed"
0
+ continue # next filename!
0
+ #end if always
0
+
0
+ print "Rename?"
0
+ print "(y/n/a/q)",
0
+ ans=raw_input()
0
+ if ans[0] == "a":
0
+ always=True
0
+ rename_result = renameFile(oldfile,newname,force=opts.force)
0
+ elif ans[0] == "q":
0
+ print "Aborting"
0
+ break
0
+ elif ans[0] == "y":
0
+ rename_result = renameFile(oldfile,newname,force=opts.force)
0
+ elif ans[0] == "n":
0
+ print "Skipping"
0
+ continue
0
+ #end if ans
0
+ if rename_result:
0
+ print "..renamed"
0
+ else:
0
+ print "..not renamed"
0
+ #end if rename_result
0
+ #end for cfile
0
+#end main
0
+
0
+if __name__ == "__main__":
0
+ main()
0
+
0
+def prompts():
0
+ if always:
0
+ print "Processing",x
0
+ else:
0
+ ans=raw_input()
0
+ if ans=="a":
0
+ always=True
0
+ print "processing",x
0
+ elif ans=="q":
0
+ pass#break
0
+ elif ans=="n":
0
+ print "Skipping",x
0
+ pass#continue
0
+ elif ans=="y":
0
+ print "processing",x
0
+ #end if ans
0
+ #end if always
0
+

Comments

    No one has commented yet.