Ticket #3313: episode_title_db.diff

File episode_title_db.diff, 3.2 KB (added by hads, 17 years ago)
  • mythvideo/mythvideo/scripts/imdbpy.py

     
    99
    1010This wrapper script is written by
    1111Pekka JÀÀskelÀinen (gmail: pekka.jaaskelainen).
    12 """ 
     12"""
    1313
     14import os
    1415import sys
    1516import optparse
    1617import re
     18import socket
     19import shlex
    1720
    1821try:
    1922        import imdb
     
    2225                "from (http://imdbpy.sourceforge.net/?page=download)"
    2326        sys.exit(1)
    2427
     28try:
     29        import MySQLdb
     30        db_support = True
     31except:
     32        db_support = False
    2533
     34db = None
     35episode_title_format = '%(series_title)s S%(season)02d E%(episode)02d %(episode_title)s'
    2636
     37def init_db():
     38        global db
     39        try:
     40                config = shlex.shlex(open(os.path.expanduser('~/.mythtv/mysql.txt')))
     41        except:
     42                print "Error opening ~/.mythtv/mysql.txt"
     43                return False
     44               
     45       
     46        token = config.get_token()
     47        db_host = db_user = db_password = None
     48        while  token != config.eof and (db_host == None or db_user == None or db_password == None):
     49                if token == "DBHostName":
     50                        if config.get_token() == "=":
     51                                db_host = config.get_token()
     52                elif token == "DBUserName":
     53                        if config.get_token() == "=":
     54                                db_user = config.get_token()
     55                elif token == "DBPassword":
     56                        if config.get_token() == "=":
     57                                db_password = config.get_token()
     58                                                               
     59                token = config.get_token()
     60        db = MySQLdb.connect(user=db_user, host=db_host, passwd=db_password,
     61                db="mythconverg")
     62        return True
     63
     64def mythtv_setting(value, hostname = '%'):
     65        """
     66        Returns the value for the given MythTV setting.
     67       
     68        Returns None if the settings was not found. If multiple rows are
     69        found (multiple hostnames), returns the value of the first one.
     70        """
     71        global db
     72        c = db.cursor()
     73        c.execute("""
     74                SELECT data
     75                FROM settings
     76                WHERE value LIKE(%s) AND hostname LIKE(%s) LIMIT 1""",
     77                (value, hostname))
     78        row = c.fetchone()
     79        c.close()
     80       
     81        if row is not None:
     82                return row[0]
     83        else:
     84                return None
     85
    2786def detect_series_title(search_string):
    2887        """
    2988        Detects a series episode title.
     
    175234        genres = None
    176235        countries = None
    177236       
    178         episode_title_format = '%(series_title)s S%(season)02d E%(episode)02d %(episode_title)s'
    179        
    180237        def __init__(self):
    181238                pass
    182239       
     
    190247                metadata = ""
    191248                if self.series_episode == True and self.season is not None and \
    192249                        self.episode is not None:
    193                         metadata += 'Title:' + self.episode_title_format % \
     250                        metadata += 'Title:' + episode_title_format % \
    194251                                {
    195252                                        'series_title': self.series_title,
    196253                                        'season': int(self.season),
     
    316373        return None
    317374       
    318375def main():
     376        global episode_title_format
     377       
    319378        p = optparse.OptionParser()
    320379        p.add_option('--version', '-v', action="store_true", default=False,
    321380                help="display 1-line describing name, version, author etc")
     
    345404episodes."""
    346405                sys.exit(0)
    347406
     407        if db_support:
     408                if init_db():
     409                        episode_title_format_setting = mythtv_setting('VideoEpisodeTitleFormat', socket.gethostname())
     410                        if episode_title_format_setting:
     411                                episode_title_format = episode_title_format_setting
     412
    348413        if options.movie_search is not None:
    349414                results = title_search(options.movie_search.decode("utf8"))
    350415                for result in results: