Ticket #3313: mythvideo_mythtv.diff
File mythvideo_mythtv.diff, 10.9 KB (added by , 17 years ago) |
---|
-
mythvideo/mythvideo/scripts/imdbpy.py
9 9 10 10 This wrapper script is written by 11 11 Pekka JÀÀskelÀinen (gmail: pekka.jaaskelainen). 12 """ 12 """ 13 13 14 14 import sys 15 15 import optparse 16 16 import re 17 import socket 17 18 18 19 try: 19 20 import imdb … … 22 23 "from (http://imdbpy.sourceforge.net/?page=download)" 23 24 sys.exit(1) 24 25 26 try: 27 from mythtv import MythTV 28 mythtv = MythTV() 29 except: 30 mythtv = None 25 31 26 27 32 def detect_series_title(search_string): 28 33 """ 29 34 Detects a series episode title. … … 50 55 continue 51 56 52 57 # Found a regexp that matches the title string. 53 return (m.group('title'), m.group('season'), m.group('episode')) 58 return (m.group('title'), m.group('season'), m.group('episode')) 54 59 55 60 return (None, None, None) 56 61 … … 90 95 meta.toMetadataString(), year]) 91 96 return matches 92 97 else: 93 matches.append([imdb_access.get_imdbID(serie), 98 matches.append([imdb_access.get_imdbID(serie), 94 99 serie['title'], int(serie['year'])]) 95 100 return matches 96 101 … … 109 114 110 115 imdb_access = imdb.IMDb() 111 116 #print "Search:",search_string 112 movies = imdb_access.search_movie(search_string.encode("ascii", 'ignore')) 117 movies = imdb_access.search_movie(search_string.encode("ascii", 'ignore')) 113 118 114 119 if movies is None or len(movies) == 0: 115 120 return None … … 192 197 countries = None 193 198 akas = None 194 199 195 episode_title_format = '%(series_title)s S%(season)02d E%(episode)02d %(episode_title)s'196 197 200 def __init__(self): 198 pass 201 self.episode_title_format = mythtv.getSetting('VideoEpisodeTitleFormat', socket.gethostname()) 202 if self.episode_title_format = None: 203 self.episode_title_format = '%(series_title)s S%(season)02d E%(episode)02d %(episode_title)s' 199 204 200 205 def toMetadataString(self): 201 206 … … 243 248 def metadataFromField(key, default=None, m=movie): 244 249 245 250 searchKey = key.lower() 246 if searchKey not in m.keys(): 251 if searchKey not in m.keys(): 247 252 return default 248 253 value = unicode(m[searchKey]) 249 254 try: … … 258 263 def metadataFromFirst(key, default=None, m=movie): 259 264 260 265 searchKey = key.lower() 261 if searchKey not in m.keys(): 266 if searchKey not in m.keys(): 262 267 return default 263 268 264 269 value = m[searchKey] … … 271 276 return default 272 277 273 278 if movie['kind'] == 'episode': 274 # print "TV Series episode detected" 279 # print "TV Series episode detected" 275 280 metadata.series_episode = True 276 281 if 'series title' in movie.keys(): 277 282 metadata.series_title = movie['series title'] … … 294 299 if 'director' in movie.keys(): 295 300 directors = movie['director'] 296 301 if directors is not None: 297 metadata.directors = directors 302 metadata.directors = directors 298 303 299 304 plots = [] 300 305 if 'plot' in movie.keys(): … … 313 318 metadata.plot = shortest_found 314 319 315 320 metadata.rating = metadataFromField('rating', metadata.rating) 316 metadata.mpaa_rating = metadataFromField('mpaa', metadata.mpaa_rating) 321 metadata.mpaa_rating = metadataFromField('mpaa', metadata.mpaa_rating) 317 322 metadata.runtime = metadataFromFirst('runtimes', metadata.runtime) 318 323 metadata.genres = metadataFromFirst('genres', metadata.genres) 319 324 metadata.countries = metadataFromFirst('countries', metadata.countries) … … 359 364 360 365 if options.info: 361 366 print """ 362 Uses the IMDbPy package to fetch the data, thus externalizes the actual 363 parsing of IMDb data to another project, hopefully reducing the maintenance 364 burden in the future, in addition supports fetching data for TV-series 367 Uses the IMDbPy package to fetch the data, thus externalizes the actual 368 parsing of IMDb data to another project, hopefully reducing the maintenance 369 burden in the future, in addition supports fetching data for TV-series 365 370 episodes.""" 366 371 sys.exit(0) 367 372 -
mythvideo/mythvideo/scripts/find_meta.py
35 35 0 * * * * find_meta.py -r /videos 36 36 37 37 In which /videos is the root of your MythVideo files. 38 """ 38 """ 39 39 40 40 import sys 41 41 import optparse … … 52 52 53 53 try: 54 54 # If found, we can insert data directly to MythDB 55 import MySQLdb 56 db_support = True 55 from mythtv import MythTV, MythVideo 56 import mythtv 57 mythtv = MythTV() 58 mythvideo = MythVideo() 57 59 except: 58 print "MySQLdb (python-mysqldb) not installed, MythDB importing disabled." 59 db_support = False 60 print "MythTV module not found, MythDB importing disabled." 61 mythtv = None 62 mythvideo = None 60 63 61 64 from stat import * 62 65 … … 180 183 title = title[0:lowest_cutpoint] 181 184 return title.strip() 182 185 183 def get_genre_id(genre_name):184 """185 Find the id of the given genre from MythDB.186 187 If the genre does not exist, insert it and return its id.188 """189 global db190 c = db.cursor()191 c.execute("SELECT intid FROM videocategory WHERE lower(category) = %s", (genre_name,))192 row = c.fetchone()193 c.close()194 195 if row is not None:196 return row[0]197 198 # Insert a new genre.199 c = db.cursor()200 c.execute("INSERT INTO videocategory(category) VALUES (%s)", (genre_name.capitalize(),))201 newid = c.lastrowid202 c.close()203 204 return newid205 206 186 def parse_meta(variable, oldvalue, emptyvalue="", meta=""): 207 187 """ 208 188 Parses a single metadata from a metadata string (returned by the imdbpy.py, etc.). … … 320 300 321 301 return save_video_metadata_to_mythdb(videopath, metadata) 322 302 323 def prune_mythdb_metadata():324 global db325 c = db.cursor()326 c.execute("""327 SELECT intid, filename328 FROM videometadata""")329 330 row = c.fetchone()331 while row is not None:332 intid = row[0]333 filename = row[1]334 if not os.path.exists(filename):335 print_verbose("%s not exist, removing metadata..." % filename)336 c2 = db.cursor()337 c2.execute("""DELETE FROM videometadata WHERE intid = %s""", (intid,))338 c2.close()339 row = c.fetchone()340 c.close()341 342 def mythvideo_metadata_id(videopath):343 """344 Finds the MythVideo metadata id for the given video path from the MythDB, if any.345 346 Returns None if no metadata was found.347 """348 global db349 c = db.cursor()350 c.execute("""351 SELECT intid352 FROM videometadata353 WHERE filename = %s""", (videopath,))354 row = c.fetchone()355 c.close()356 357 if row is not None:358 return row[0]359 else:360 return None361 362 def mythtv_setting(value, hostname = '%'):363 """364 Returns the value for the given MythTV setting.365 366 Returns None if the settings was not found. If multiple rows are367 found (multiple hostnames), returns the value of the first one.368 """369 global db370 c = db.cursor()371 c.execute("""372 SELECT data373 FROM settings374 WHERE value LIKE(%s) AND hostname LIKE(%s) LIMIT 1""",375 (value, hostname))376 row = c.fetchone()377 c.close()378 379 if row is not None:380 return row[0]381 else:382 return None383 384 303 def save_video_metadata_to_mythdb(videopath, metadata, child=-1, disc=None): 385 304 """ 386 305 Updates the given metadata for the given video file. … … 406 325 (None, None, 0, None, None, None, None, 407 326 0.0, None, 0, None, None, child, "") 408 327 409 intid = mythvideo _metadata_id(videopath)328 intid = mythvideo.getMetadataId(videopath) 410 329 if intid is not None: 411 if not overwrite: 330 if not overwrite: 412 331 print_verbose("Metadata already exist in MythDB, not overwriting it.") 413 return None 332 return None 414 333 else: 415 334 print_verbose("No metadata in MythDB, creating a new one.") 416 335 # Create a new empty entry at this point so we can use the common UPDATE code 417 336 # to actually insert the data. 418 c = db.cursor() 419 c.execute("""INSERT INTO videometadata(filename) VALUES(%s)""", (videopath,)) 420 intid = c.lastrowid 421 c.close() 337 intid = mythvideo.setMetadata({'filename': videopath}) 422 338 423 424 339 def parse_metadata(variable, oldvalue, emptyvalue="", meta=metadata): 425 340 return parse_meta(variable, oldvalue, emptyvalue, meta) 426 341 427 342 if title is None: 428 343 title = parse_metadata('Title', title) 429 344 … … 437 352 438 353 if poster_search: 439 354 print_verbose("Fetching a poster image...") 440 coverfile = find_poster_image(title, inetref) 355 coverfile = find_poster_image(title, inetref) 441 356 if coverfile is not None: 442 357 print_verbose("Found a poster.") 443 358 else: … … 452 367 m = re.match(akaRegexp, aka) 453 368 if m is not None: 454 369 title = m.group(1) + " (" + title + ")" 455 print_verbose("Found AKA: %s" % title) 370 print_verbose("Found AKA: %s" % title) 456 371 break 457 372 458 373 if disc is not None: … … 505 420 506 421 if len(genres) < 1: 507 422 print_verbose("No genres.") 508 category = get_genre_id("Unknown")423 category = mythvideo.getGenreId("Unknown") 509 424 else: 510 425 # Only one genre supported? 511 category = get_genre_id(genres[0])426 category = mythvideo.getGenreId(genres[0]) 512 427 513 428 if coverfile == None: 514 429 coverfile = "No cover" 515 430 516 c = db.cursor() 517 518 c.execute(u"""UPDATE videometadata 519 SET showlevel = 1, browse = 1, childid = %s, playcommand = %s, title = %s, 520 director = %s, plot = %s, rating = %s, inetref = %s, category = %s, 521 year = %s, userrating = %s, length = %s, filename = %s, coverfile = %s 522 WHERE intid = %s""", 523 (childid, playcommand, title.encode("utf8"), 524 director.encode("utf8"), plot.encode("utf8"), rating, 525 inetref, category, year, userrating, length, filename, 526 coverfile, intid)) 527 c.close() 431 mythvideo.setMetadata({'showlevel': 1, 'browse': 1, 'childid': childid, 432 'playcommand': playcommand, 'title': title.encode('utf8'), 433 'director': director.encode('utf8'), 'plot': plot.encode('utf8'), 434 'rating': rating, 'inetref': inetref, 'category': category, 435 'year': year, 'userrating': userrating, 'length': length, 436 'filename': filename, 'coverfile': coverfile}, intid) 528 437 return intid 529 438 530 439 def find_poster_image(title, imdb_id): … … 883 792 # Check if we are not in overwrite mode and there is existing data 884 793 # for the wanted targets (metadata files and/or MythDB). 885 794 if not overwrite: 886 need_mythdb_data = dbimport and mythvideo _metadata_id(path) is None795 need_mythdb_data = dbimport and mythvideo.getMetadataId(path) is None 887 796 need_metadata_file = metafiles 888 797 if metafiles and meta_file is not None: 889 798 need_metadata_file = not os.path.exists(meta_file) … … 991 900 print_verbose("IMDb ID %s given manually." % options.imdb_id) 992 901 993 902 if dbimport or prune: 994 if not db_support:995 print "You must install MySQLdbmodule to make direct DB importing to work"903 if not mythtv: 904 print "You must have the MythTV module to make direct DB importing to work" 996 905 sys.exit(1) 997 if not init_db(): 998 print "Database connection failed." 999 sys.exit(1) 1000 poster_dir = mythtv_setting("VideoArtworkDir", socket.gethostname()) 906 poster_dir = mythtv.getSetting("VideoArtworkDir", socket.gethostname()) 1001 907 1002 908 1003 909 if prune: 1004 prune_mythdb_metadata()910 mythvideo.pruneMetadata() 1005 911 1006 912 for path in paths: 1007 913