Ticket #6219: imdb_script6.patch
File imdb_script6.patch, 11.5 KB (added by , 15 years ago) |
---|
-
imdbpy.py
159 159 movies = [] 160 160 for m in sorted_movies: 161 161 try: 162 movies.append([imdb_access.get_imdbID(m), m['title'], int(m['year'])])162 item = [imdb_access.get_imdbID(m), m['title'], int(m['year'])] 163 163 except KeyError: 164 movies.append([imdb_access.get_imdbID(m), m['title'], 1901])165 movies.append( [imdb_access.get_imdbID(m), m['title'], int(m['year'])])164 item = [imdb_access.get_imdbID(m), m['title'], 1901] 165 movies.append(item) 166 166 return movies 167 167 168 168 def find_poster_url(imdb_id): … … 327 327 shortest_found = None 328 328 #print "%d plots found" % len(plots) 329 329 for plot in plots: 330 text = None 331 #IMDbPY 3.9 and later use 'author::plot_content' 332 # earlier versions are 'plot_content::author' 333 if float(imdb.__version__) > 3.8: 334 text = plot.split("::")[1] 335 else: 336 text = plot.split("::")[0] 330 337 text = plot.split("::")[1] 331 338 if text.find('@') != -1 or len(text.split(' ')) < 10: # Skip plots of less than 5 words 332 339 continue … … 400 407 401 408 if options.movie_search is not None: 402 409 results = title_search(options.movie_search.decode("utf8")) 403 for result in results: 404 print "%s:%s (%d)" % (result[0], result[1], result[2]) 410 if results: 411 for result in results: 412 print "%s:%s (%d)" % (result[0], result[1], result[2]) 405 413 elif options.poster_search is not None: 406 414 poster_search(options.poster_search) 407 415 elif options.metadata_search is not None: -
fetch_poster.py
46 46 http://www.pythonware.com/products/pil/""" 47 47 imaging_library = False 48 48 49 #Number of default IMDb retry 50 import time 51 defaultretries=3 52 53 def functionretry(func, arg1, arg2=None, retries=None): 54 global defaultretries 55 56 if retries == None: 57 retries = defaultretries 58 59 attempts = 0 60 stop = False 61 while (not stop): 62 try: 63 if arg2: 64 result = func(arg1, arg2) 65 else: 66 result = func(arg1) 67 stop = True 68 except: 69 result = None 70 if not stop: 71 attempts += 1 72 if attempts > retries: 73 stop = True 74 if attempts <= retries: 75 print 'Failed to retrieve data, retry in 5s' 76 time.sleep(5) 77 78 if attempts > retries: 79 print 'Error retrieving data : No more attempts' 80 return result 81 49 82 class PosterImage: 50 83 """ 51 84 Holds a single poster image. … … 104 137 max_results = 4 105 138 images = [] 106 139 107 for url in poster_urls: 108 image_url = self.find_poster_image_url(url) 109 if image_url is not None: 110 images.append(self.download_image(image_url, ".jpg")) 111 results += 1 112 if results >= max_results: 113 break 140 if poster_urls: 141 for url in poster_urls: 142 image_url = self.find_poster_image_url(url) 143 if image_url is not None: 144 images.append(self.download_image(image_url, ".jpg")) 145 results += 1 146 if results >= max_results: 147 break 114 148 return images 115 149 116 150 def find_poster_image_url(self, poster_page_url): … … 196 230 return [self.download_image(poster_url, extension)] 197 231 return [] 198 232 199 def find_best_posters(title, count=1, accept_horizontal=False, imdb_id=None ):233 def find_best_posters(title, count=1, accept_horizontal=False, imdb_id=None, retries=None): 200 234 201 235 fetchers = [MoviePosterPosterFetcher(), IMDbPosterFetcher()] 202 236 #fetchers = [IMDbPosterFetcher()] … … 218 252 break 219 253 220 254 for fetcher in fetchers: 221 new_posters = fetcher.fetch(title, imdb_id) 222 for poster in new_posters: 223 if not accept_horizontal and not poster.is_vertical(): 224 os.remove(poster.file_name) 225 continue 226 posters.append(poster) 255 new_posters = functionretry(fetcher.fetch, title, arg2=imdb_id, retries=retries) 256 if new_posters: 257 for poster in new_posters: 258 if not accept_horizontal and not poster.is_vertical(): 259 os.remove(poster.file_name) 260 continue 261 posters.append(poster) 227 262 228 263 def size_cmp(a, b): 229 264 return cmp(a.pixels(), b.pixels()) … … 237 272 return posters[0:count] 238 273 239 274 def main(): 275 global defaultretries 240 276 241 242 277 p = optparse.OptionParser() 243 278 p.add_option('--number', '-n', action="store", type="int", default=1, 244 279 help="the count of biggest posters to get") … … 247 282 p.add_option('--poster_search', '-P', metavar='IMDB_ID', default=None, dest="imdb_id", 248 283 help="Displays a list of URL's to movie posters. The lines are "\ 249 284 "ranked by descending value. For MythVideo.") 285 p.add_option('--retry', '-t', action="store", type="int", dest="retries",default=3, 286 help="Number of retries, 0 means no retry [default 3]") 250 287 251 288 options, arguments = p.parse_args() 252 289 290 defaultretries = options.retries 291 253 292 title = "" 254 293 if len(arguments) != 1: 255 294 if options.imdb_id: 256 295 # TODO: Fetch the title from IMDb. 257 metadata = imdbpy.metadata_search(options.imdb_id) 258 title = imdbpy.parse_meta(metadata, "Title") 296 metadata = functionretry(imdbpy.metadata_search,options.imdb_id) 297 if metadata: 298 title = imdbpy.parse_meta(metadata, "Title") 299 else: 300 print "Error can't retrieve title from IMDb" 301 sys.exit(1) 259 302 else: 260 303 print "Please give a video title as argument." 261 304 sys.exit(1) … … 263 306 title = arguments[0] 264 307 265 308 posters = find_best_posters(title, options.number, options.all, 266 imdb_id=options.imdb_id )309 imdb_id=options.imdb_id, retries=defaultretries) 267 310 268 311 if options.imdb_id is not None: 269 312 for poster in posters: … … 276 319 277 320 if __name__ == '__main__': 278 321 main() 279 280 No newline at end of file 322 -
find_meta.py
102 102 # MythTV settings table). 103 103 poster_dir = "./" 104 104 105 #Number of default IMDb retry 106 import time 107 defaultretries=3 108 109 def functionretry(func, arg1, arg2=None, retries=None): 110 global defaultretries 111 112 if retries == None: 113 retries = defaultretries 114 115 attempts = 0 116 stop = False 117 while (not stop): 118 try: 119 if arg2: 120 result = func(arg1, arg2) 121 else: 122 result = func(arg1) 123 stop = True 124 except: 125 result = None 126 if not stop: 127 attempts += 1 128 if attempts > retries: 129 stop = True 130 if attempts <= retries: 131 print_verbose ('Failed to retrieve data, retry in 5s') 132 time.sleep(5) 133 134 if attempts > retries: 135 print_verbose ('Error retrieving data : No more attempts') 136 return result 137 105 138 def print_verbose(string): 106 139 global verbose 107 140 if verbose: … … 153 186 lowest_cutpoint = pos 154 187 155 188 title = title[0:lowest_cutpoint] 189 # Remove trailing year in the form of "name [year]" or "name (year)" if any 190 m = re.compile(r"(.*)([\(\[]([0-9]+)[\]\)])$") 191 found = m.match(title.strip()) 192 if found: 193 title = found.group(1) 156 194 return title.strip() 157 195 158 196 def parse_meta(variable, oldvalue, emptyvalue="", meta=""): … … 347 385 title = aka_title + " (" + title + ")" 348 386 print_verbose("Found AKA: %s" % title) 349 387 break 388 # For IMDbPY >= 3.9 389 # Grill Point::(International: English title) 390 # Catastrofi d'amore::(Italy) [it] 391 akaRegexp = ".+::\(.+\) \[%s\].*" % aka_language 392 m = re.match(akaRegexp, aka) 393 if m is not None: 394 aka_title = aka.split("::")[0] 395 if aka_title != title: 396 title = aka_title + " (" + title + ")" 397 print_verbose("Found AKA: %s" % title) 398 break 350 399 351 400 if disc is not None: 352 401 title += " [disc" + unicode(disc) + "]" … … 395 444 396 445 filename = videopath 397 446 447 ## Process genres 398 448 genrestring = parse_metadata('Genres', "", "") 399 449 genres = [] 400 450 if genrestring is not None and len(genrestring) > 0: 401 451 genres = genrestring.split(",") 402 452 453 #Always set category to "Unknown", until we can identify what it really is 454 category = mythvideo.getGenreId("Unknown") 403 455 if len(genres) < 1: 404 456 print_verbose("No genres.") 405 category = mythvideo.getGenreId("Unknown")406 457 else: 407 # Only one genre supported? 408 category = mythvideo.getGenreId(genres[0]) 458 #Remove previous genres 459 mythvideo.cleanGenres(intid) 460 #Set all the genres 461 for genre in genres: 462 mythvideo.setGenres(genre.strip(), intid) 409 463 464 ## Process countries 465 countrystring = parse_metadata('Countries', "", "") 466 countries = [] 467 if countrystring is not None and len(countrystring) > 0: 468 countries = countrystring.split(",") 469 470 if len(countries) < 1: 471 print_verbose("No countries.") 472 else: 473 #Remove previous countries 474 mythvideo.cleanCountry(intid) 475 #Set all the countries 476 for country in countries: 477 mythvideo.setCountry(country.strip(), intid) 478 479 ## Process cast 480 caststring = parse_metadata('Cast', "", "") 481 cast = [] 482 if caststring is not None and len(caststring) > 0: 483 cast = caststring.split(",") 484 485 if len(cast) < 1: 486 print_verbose("No cast.") 487 else: 488 #Remove previous cast 489 mythvideo.cleanCast(intid) 490 #Set all the cast 491 for actor in cast: 492 mythvideo.setCast(actor.strip(), intid) 493 410 494 if coverfile == None: 411 495 coverfile = "No cover" 412 496 … … 427 511 otherwise returns the base name of the poster image file. 428 512 """ 429 513 global poster_dir,overwrite 514 global defaultretries 430 515 image_extensions = ["png", "jpg", "bmp"] 431 516 432 517 poster_files = [] … … 436 521 if len(poster_files) == 0 or overwrite: 437 522 # Try to fetch the poster image from the web. 438 523 posters = fetch_poster.find_best_posters(\ 439 title, count=1, accept_horizontal=True, imdb_id=imdb_id )524 title, count=1, accept_horizontal=True, imdb_id=imdb_id,retries=defaultretries) 440 525 441 526 if len(posters) == 0: 442 527 return None … … 528 613 529 614 print_verbose("Title search '%s'" % title) 530 615 531 candidates = imdbpy.title_search(title) 616 candidates = functionretry(imdbpy.title_search, title) 617 532 618 if candidates is None or len(candidates) == 0: 533 619 # TODO: Try with the dirname 534 620 pass … … 560 646 561 647 print_verbose("Querying IMDb for meta data for ID %s..." % imdb_id) 562 648 try: 563 meta = imdbpy.fetch_metadata(imdb_id)649 meta = functionretry(imdbpy.fetch_metadata,imdb_id) 564 650 if meta is not None: 565 651 if meta.series_episode: 566 652 title, season, episode = imdbpy.detect_series_title(title) … … 708 794 metadata = load_metadata_file(metadata_target) 709 795 710 796 if imdb_id is not None: 711 meta = imdbpy.fetch_metadata(imdb_id)797 meta = functionretry(imdbpy.fetch_metadata,imdb_id) 712 798 if meta.series_episode: 713 799 fileName = os.path.basename(pathName) 714 800 t, season, episode = imdbpy.detect_series_title(fileName) … … 734 820 return 735 821 736 822 if imdb_id is not None: 737 metadata = imdbpy.metadata_search(imdb_id) 823 metadata = functionretry(imdbpy.metadata_search,imdb_id) 824 738 825 if metadata is not None: 739 826 metadata += "IMDb:%s" % imdb_id + "\n" 740 827 save_metadata(dirName, dirName + "/video.metadata", metadata) … … 815 902 global verbose, overwrite, interactive, recursive, dbimport 816 903 global import_from_files, metafiles, poster_dir, poster_search 817 904 global aka_language 905 global defaultretries 818 906 819 907 usage = "usage: %prog [options] videopath1 [videopath2 videopath3...]" 820 908 … … 848 936 default=None, 849 937 help="Add the title name in the given country (two letter code, e.g., 'fi') "\ 850 938 "to the movie title.") 939 p.add_option('--retry', '-t', action="store", type="int", dest="retries",default=3, 940 help="Number of retries, 0 means no retry [default 3]") 851 941 852 942 options, arguments = p.parse_args() 853 943 … … 865 955 prune = options.prune 866 956 poster_search = not options.skip_poster_search 867 957 aka_language = options.lang_code 958 959 defaultretries = options.retries 868 960 869 961 if not (metafiles or dbimport): 870 962 print "You must define writing to either MythDB import (-d) or metadata files (-m)."