Ticket #6219: imdb_script5.patch
File imdb_script5.patch, 12.2 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 = plot.split("::")[1] 330 #IMDbPY 3.9 doesn't order the content of plot like earlier version of IMDbPY. 331 # IMDB 3.6 is : plot_content::author 332 # IMDB 3.9 is : author::plot_content 333 #So we assume that should one side longer than the other, it's actually where the description is 334 splitvalue = plot.split("::") 335 if len(splitvalue[0]) > len(splitvalue[1]): 336 text = splitvalue[0] 337 else: 338 text = splitvalue[1] 331 339 if shortest_found == None or len(text) < len(shortest_found): 332 340 shortest_found = text 333 341 metadata.plot = shortest_found … … 397 405 398 406 if options.movie_search is not None: 399 407 results = title_search(options.movie_search.decode("utf8")) 400 for result in results: 401 print "%s:%s (%d)" % (result[0], result[1], result[2]) 408 if results: 409 for result in results: 410 print "%s:%s (%d)" % (result[0], result[1], result[2]) 402 411 elif options.poster_search is not None: 403 412 poster_search(options.poster_search) 404 413 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) + "]" … … 400 449 if genrestring is not None and len(genrestring) > 0: 401 450 genres = genrestring.split(",") 402 451 452 #Always set category to "Unknown", until we can identify what it really is 453 category = mythvideo.getGenreId("Unknown") 403 454 if len(genres) < 1: 404 455 print_verbose("No genres.") 405 category = mythvideo.getGenreId("Unknown")406 456 else: 407 # Only one genre supported? 408 category = mythvideo.getGenreId(genres[0]) 457 #Remove previous genres 458 mythvideo.cleanGenres(intid) 459 #Set all the genres 460 for genre in genres: 461 mythvideo.setGenres(genre.strip(), intid) 409 462 463 countrystring = parse_metadata('Countries', "", "") 464 countries = [] 465 if countrystring is not None and len(countrystring) > 0: 466 countries = countrystring.split(",") 467 468 #Always set category to "Unknown", until we can identify what it really is 469 if len(countries) < 1: 470 print_verbose("No countries.") 471 else: 472 #Remove previous genres 473 mythvideo.cleanCountry(intid) 474 #Set all the countries 475 for country in countries: 476 mythvideo.setCountry(country.strip(), intid) 477 478 caststring = parse_metadata('Cast', "", "") 479 cast = [] 480 if caststring is not None and len(caststring) > 0: 481 cast = caststring.split(",") 482 483 #Always set category to "Unknown", until we can identify what it really is 484 if len(cast) < 1: 485 print_verbose("No cast.") 486 else: 487 #Remove previous cast 488 mythvideo.cleanCast(intid) 489 #Set all the cast 490 for actor in cast: 491 mythvideo.setCast(actor.strip(), intid) 492 410 493 if coverfile == None: 411 494 coverfile = "No cover" 412 495 … … 427 510 otherwise returns the base name of the poster image file. 428 511 """ 429 512 global poster_dir,overwrite 513 global defaultretries 430 514 image_extensions = ["png", "jpg", "bmp"] 431 515 432 516 poster_files = [] … … 436 520 if len(poster_files) == 0 or overwrite: 437 521 # Try to fetch the poster image from the web. 438 522 posters = fetch_poster.find_best_posters(\ 439 title, count=1, accept_horizontal=True, imdb_id=imdb_id )523 title, count=1, accept_horizontal=True, imdb_id=imdb_id,retries=defaultretries) 440 524 441 525 if len(posters) == 0: 442 526 return None … … 528 612 529 613 print_verbose("Title search '%s'" % title) 530 614 531 candidates = imdbpy.title_search(title) 615 candidates = functionretry(imdbpy.title_search, title) 616 532 617 if candidates is None or len(candidates) == 0: 533 618 # TODO: Try with the dirname 534 619 pass … … 560 645 561 646 print_verbose("Querying IMDb for meta data for ID %s..." % imdb_id) 562 647 try: 563 meta = imdbpy.fetch_metadata(imdb_id)648 meta = functionretry(imdbpy.fetch_metadata,imdb_id) 564 649 if meta is not None: 565 650 if meta.series_episode: 566 651 title, season, episode = imdbpy.detect_series_title(title) … … 708 793 metadata = load_metadata_file(metadata_target) 709 794 710 795 if imdb_id is not None: 711 meta = imdbpy.fetch_metadata(imdb_id)796 meta = functionretry(imdbpy.fetch_metadata,imdb_id) 712 797 if meta.series_episode: 713 798 fileName = os.path.basename(pathName) 714 799 t, season, episode = imdbpy.detect_series_title(fileName) … … 734 819 return 735 820 736 821 if imdb_id is not None: 737 metadata = imdbpy.metadata_search(imdb_id) 822 metadata = functionretry(imdbpy.metadata_search,imdb_id) 823 738 824 if metadata is not None: 739 825 metadata += "IMDb:%s" % imdb_id + "\n" 740 826 save_metadata(dirName, dirName + "/video.metadata", metadata) … … 815 901 global verbose, overwrite, interactive, recursive, dbimport 816 902 global import_from_files, metafiles, poster_dir, poster_search 817 903 global aka_language 904 global defaultretries 818 905 819 906 usage = "usage: %prog [options] videopath1 [videopath2 videopath3...]" 820 907 … … 831 918 help="Traverse sub directories of the given directory recursively.") 832 919 833 920 p.add_option('--no_dbimport', '-n', action="store_true", default=False, 834 help="Do not import metadata directly to MythDB.")921 help="Do not import metadata directly to MythDB.") 835 922 p.add_option('--fromfiles', '-f', action="store_true", default=False, 836 923 help="Import data to MythDB from .metadata files if found. Requires -d.") 837 924 p.add_option('--metafiles', '-m', action="store_true", default=False, … … 848 935 default=None, 849 936 help="Add the title name in the given country (two letter code, e.g., 'fi') "\ 850 937 "to the movie title.") 938 p.add_option('--retry', '-t', action="store", type="int", dest="retries",default=3, 939 help="Number of retries, 0 means no retry [default 3]") 851 940 852 941 options, arguments = p.parse_args() 853 942 … … 865 954 prune = options.prune 866 955 poster_search = not options.skip_poster_search 867 956 aka_language = options.lang_code 957 958 defaultretries = options.retries 868 959 869 960 if not (metafiles or dbimport): 870 961 print "You must define writing to either MythDB import (-d) or metadata files (-m)."