Ticket #2734: metadata_scripts_prune.patch

File metadata_scripts_prune.patch, 4.6 KB (added by visit0r, 17 years ago)

Add '-p' which prunes old metadata from MythDB. Includes the previous patch also.

  • mythvideo/scripts/fetch_poster.py

     
    165165                urls = []
    166166                for div in divs:
    167167                        links = div.findAll('a')
     168                       
    168169                        if len(links) > 0:
    169                                 link = links[0]
    170                                 title = link['title'].lower()
    171                                 if title.endswith("poster"):
    172                                         title = title[0:-len(" poster")]
    173                                 if title == search:
    174                                         urls.append(link['href'])
     170                                for link in links:
     171                                        # Skip the mailto links.
     172                                        spl = link['href'].split(":")
     173                                        if len(spl) > 1:
     174                                                if spl[0].lower() == "mailto":
     175                                                        continue
     176                                        title = link['title'].lower()
     177                                        if title.endswith("poster"):
     178                                                title = title[0:-len(" poster")]
     179                                        if title == search:
     180                                                urls.append(link['href'])
    175181                return urls
    176182                               
    177183               
  • mythvideo/scripts/find_meta.py

     
    130130                db="mythconverg")
    131131        print_verbose("Database connection successful.")
    132132        return True
    133        
     133               
    134134def find_imdb_id_from_text_file(textFile):
    135135       
    136136        if os.access(textFile, os.R_OK):
     
    164164               
    165165def cleanup_title(title):
    166166        title = title.replace("_", " ").replace(".", " ")
    167         cut_point_strings = ["hdtv", "xvid", "dvd", "proper", "720p"]
     167        cut_point_strings = ["hdtv", "xvid", "dvd", "proper", "720p", "limited"]
    168168        lowest_cutpoint = len(title)
    169169        for string in cut_point_strings:
    170170                pos = title.lower().rfind(string)
     
    287287        else:
    288288                return disc
    289289       
    290 
    291290def save_metadata_to_mythdb(videopath, metadata):
    292291        """
    293292        Updates the given metadata for the given video path.
    294293       
    295294        Detects if the given title is a dvd-rip dir with multiple videos and
    296         adds metadata for all the videos separately, and chains the videos
     295        adds metadata for all the videos separately and chains the videos
    297296        together.
    298297        """
    299298        files_str = parse_meta("Files", "", "", metadata)
     
    314313                        return
    315314                       
    316315        return save_video_metadata_to_mythdb(videopath, metadata)
     316               
     317def prune_mythdb_metadata():
     318        global db
     319        c = db.cursor()
     320        c.execute("""
     321                SELECT intid, filename
     322                FROM videometadata""")
    317323       
    318        
     324        row = c.fetchone()
     325        while row is not None:
     326                intid = row[0]
     327                filename = row[1]
     328                if not os.path.exists(filename):
     329                        print_verbose("%s not exist, removing metadata..." % filename)
     330                        c2 = db.cursor()
     331                        c2.execute("""DELETE FROM videometadata WHERE intid = %s""", (intid,))
     332                        c2.close()
     333                row = c.fetchone()
     334        c.close()
     335               
    319336def mythvideo_metadata_id(videopath):
    320337        """
    321338        Finds the MythVideo metadata id for the given video path from the MythDB, if any.
     
    357374                return row[0]
    358375        else:
    359376                return None
    360        
    361        
     377               
    362378def save_video_metadata_to_mythdb(videopath, metadata, child=-1, disc=None):
    363379        """
    364380        Updates the given metadata for the given video file.
     
    515531                print_verbose("Found existing cover image.")
    516532                return poster_files[0]
    517533        return None
    518                
    519                
     534                               
    520535def save_metadata_to_file(fileName, metadata):
    521536        global overwrite
    522537               
     
    775790        if metadata is not None:
    776791                save_metadata(pathName, metadata_target, metadata)
    777792
    778 
    779793def scan_directory(dirName, imdb_id = None):
    780794        global videoExtensions
    781795        dirName = dirName.replace("[", "?").replace("]", "?")
     
    890904        p.add_option('--answer', '-a', action="store", type="string", dest="imdb_id",
    891905                help="Fetch metadata with the given IMDb ID for the path (must be a single path).")
    892906               
     907        p.add_option('--prune', '-p', action="store_true", default=False,
     908                help="Prune metadata of deleted files from MythDB.")
    893909               
     910               
    894911        options, arguments = p.parse_args()
    895912       
    896913        if options.version:
     
    904921        dbimport = not options.no_dbimport
    905922        import_from_files = options.fromfiles and dbimport
    906923        metafiles = options.metafiles
     924        prune = options.prune
    907925       
    908926        if not (metafiles or dbimport):
    909927                print "You must define writing to either MythDB import (-d) or metadata files (-m)."
    910928                sys.exit(1)
    911929                       
    912         if len(arguments) < 1:
     930        if not prune and len(arguments) < 1:
    913931                print "Please give the paths to be scanned as argument."
    914932                sys.exit(1)
    915933        paths = arguments       
     
    923941                        sys.exit(1)                     
    924942                print_verbose("IMDb ID %s given manually." % options.imdb_id)           
    925943                       
    926         if dbimport:
     944        if dbimport or prune:
    927945                if not db_support:
    928946                        print "You must install MySQLdb module to make direct DB importing to work"
    929947                        sys.exit(1)
     
    933951                poster_dir = mythtv_setting("VideoArtworkDir", socket.gethostname())
    934952               
    935953       
     954        if prune:
     955                prune_mythdb_metadata()
     956       
    936957        for path in paths:
    937958       
    938959                if not os.path.exists(path):