public
Description: Simple to use TVDB (thetvdb.com) API in Python, and automatic TV episode namer
Homepage: http://dbr.lighthouseapp.com/projects/13342-tvdb_api/tickets
Clone URL: git://github.com/dbr/tvdb_api.git
Search Repo:
Improved user-input handling. Deals with 'q' quit command and interupts 
(^c) much cleaner
dbr (author)
Tue Jun 24 22:42:17 -0700 2008
commit  4f8a41a614a1cb669deb5345de9fb5cd7a46631b
tree    15ed8ba97f87d170e0f6f32343dc5e494a2ae839
parent  c934530d85b47c10b9c2821b44838cf0e0f2fa2d
...
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
...
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
0
@@ -184,32 +184,45 @@ class tvdb:
0
         
0
         if len(allSeries) == 0:
0
             self.log.debug('Series result returned zero')
0
- raise tvdb_shownotfound("Show-name search returned zero results")
0
+ raise tvdb_shownotfound("Show-name search returned zero results (cannot find show on TVDB)")
0
         
0
- if self.config['interactive']:
0
+ if not self.config['interactive']:
0
+ self.log.debug('Auto-selecting first search result')
0
+ return allSeries[0]
0
+ else:
0
             self.log.debug('Interactivily selecting show')
0
- for i in range(len(allSeries[:6])):
0
+ print "TVDB Search Results:"
0
+ for i in range(len(allSeries[:6])): # list first 6 search results
0
                 i_show = i + 1 # Start at more human readable number 1 (not zero)
0
                 self.log.debug( 'Showing allSeries[%s] = %s)' % (i_show,allSeries[i]) )
0
                 print "%s -> %s (tvdb id: %s)" % (i_show,allSeries[i]['name'].encode("UTF-8","ignore"),allSeries[i]['sid'].encode("UTF-8","ignore"))
0
- print "Enter choice (first number):"
0
- ans=raw_input()
0
- self.log.debug( 'Got choice of: %s' % (ans))
0
- try:
0
- selected_id = int(ans) - 1 # The human entered 1 as first result, not zero
0
- self.log.debug( 'Trying to return ID: %d' % (selected_id))
0
- return allSeries[ selected_id ]
0
- except ValueError: # Input was not number
0
- if ans == "q":
0
- self.log.debug('Got quit command (q)')
0
- raise tvdb_userabort("User aborted")
0
- else:
0
- self.log.debug('Unknown keypress %s' % (ans))
0
- raise tvdb_userabort("Invalid keypress") # TODO: Better UI
0
- #end for k,v
0
- else:
0
- self.log.debug('Auto-selecting first search result')
0
- return allSeries[0]
0
+
0
+ valid_input = False
0
+ while not valid_input:
0
+ try:
0
+ print "Enter choice (first number, ? for help):"
0
+ ans=raw_input()
0
+ except KeyboardInterrupt:
0
+ raise tvdb_userabort("User aborted ('^c' keyboard interupt)")
0
+
0
+ self.log.debug( 'Got choice of: %s' % (ans))
0
+ try:
0
+ selected_id = int(ans) - 1 # The human entered 1 as first result, not zero
0
+ self.log.debug( 'Trying to return ID: %d' % (selected_id))
0
+ return allSeries[ selected_id ]
0
+ except ValueError: # Input was not number
0
+ if ans == "q":
0
+ self.log.debug('Got quit command (q)')
0
+ raise tvdb_userabort("User aborted ('q' quit command)")
0
+ elif ans == "?":
0
+ print "## Help"
0
+ print "# Enter the number that corresponds to the correct show."
0
+ print "# ? - this help"
0
+ print "# q - abort tvnamer"
0
+ else:
0
+ self.log.debug('Unknown keypress %s' % (ans))
0
+ #end try
0
+ #end while not valid_input
0
     #end _getSeries
0
 
0
     def _getEps(self,sid):
...
16
17
18
19
 
20
21
22
...
228
229
230
231
232
 
233
 
234
235
236
237
238
 
 
 
 
239
240
241
...
279
280
281
 
 
282
283
284
285
286
287
288
 
 
 
 
 
 
 
 
 
 
 
 
 
289
290
291
...
297
298
299
300
 
301
302
303
...
16
17
18
 
19
20
21
22
...
228
229
230
 
 
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
...
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
...
313
314
315
 
316
317
318
319
0
@@ -16,7 +16,7 @@ __version__ = "0.1"
0
 import os,sys,re
0
 from optparse import OptionParser
0
 
0
-from tvdb_api import tvdb_shownotfound, tvdb_epnamenotfound
0
+from tvdb_api import tvdb_shownotfound, tvdb_epnamenotfound, tvdb_userabort
0
 from tvdb_api import tvdb
0
 
0
 config={}
0
@@ -228,14 +228,18 @@ def main():
0
     print "#"*20
0
     
0
     for cfile in validFiles:
0
- print "Processing %(file_showname)s (season: %(seasno)d, episode %(epno)d)" % (cfile)
0
- # Ask for episode name from tvdb_api
0
+ print "# Processing %(file_showname)s (season: %(seasno)d, episode %(epno)d)" % (cfile)
0
         try:
0
+ # Ask for episode name from tvdb_api
0
             epname = t[ cfile['file_showname'] ][ cfile['seasno'] ][ cfile['epno'] ]['name']
0
         except (tvdb_shownotfound,tvdb_epnamenotfound):
0
             # No such show
0
             epname = None
0
             showname = None
0
+ except (tvdb_userabort), errormsg:
0
+ # User aborted selection (q or ^c)
0
+ print "\n", errormsg
0
+ os._exit(1)
0
         finally:
0
             showname = t[ cfile['file_showname'] ]['showname'] # get the corrected showname
0
         
0
@@ -279,13 +283,25 @@ def main():
0
                 print "..auto-renaming"
0
             else:
0
                 print "..not renamed"
0
+ #end if rename_result
0
+
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
+ print "([y]/n/a/q)",
0
+ try:
0
+ ans=raw_input()
0
+ ans = ans.strip()
0
+ except KeyboardInterrupt, errormsg:
0
+ print "User aborted (^c)"
0
+ break
0
+ #end try
0
+
0
+ if len(ans) == 0:
0
+ print "Renaming (default)"
0
+ rename_result = renameFile(oldfile,newfile,force=opts.force)
0
+ elif ans[0] == "a":
0
             opts.always=True
0
             rename_result = renameFile(oldfile,newfile,force=opts.force)
0
         elif ans[0] == "q":
0
@@ -297,7 +313,7 @@ def main():
0
             print "Skipping"
0
             continue
0
         else:
0
- print "Invalid input"
0
+ print "Invalid input, skipping"
0
         #end if ans
0
         if rename_result:
0
             print "..renamed"

Comments

    No one has commented yet.