00001
00002
00003 import sys
00004 import os
00005 import socket
00006 import time
00007 import MySQLdb
00008 from optparse import OptionParser
00009 from mythvideo_test import HTTP_PORT
00010
00011 IMAGE_URL_BASE = 'http://%s:%d/images'
00012
00013 SETTINGS_BACKUP = '~/.mythvideo_search.rc'
00014
00015 URL_PORT=HTTP_PORT
00016 URL_HOST='localhost'
00017
00018 DB_USER = 'mythtv'
00019 DB_PASSWORD = 'mythtv'
00020 DB_HOST = 'localhost'
00021 DB_DATABASE = 'mythconverg'
00022
00023 class ItemInfo:
00024 def __init__(self, id, title, year, release_date, director, plot,
00025 user_rating, movie_rating, runtime, writers, cast, genres,
00026 countries, poster = None, akas = None):
00027 self.m_id = id
00028 self.m_title = title
00029 self.m_year = year
00030 self.m_release_date = release_date
00031 self.m_director = director
00032 self.m_plot = plot
00033 self.m_user_rating = user_rating
00034 self.m_movie_rating = movie_rating
00035 self.m_runtime = runtime
00036 self.m_writers = writers
00037 self.m_cast = cast
00038 self.m_genres = genres
00039 self.m_countries = countries
00040 self.m_poster = poster
00041 self.m_akas = akas
00042
00043 def __str__(self):
00044 return """Title:%(title)s
00045 Year:%(year)s
00046 ReleaseDate:%(releasedate)s
00047 Director:%(director)s
00048 Plot:%(plot)s
00049 UserRating:%(userrating)s
00050 MovieRating:%(movierating)s
00051 Runtime:%(runtime)s
00052 Writers:%(writers)s
00053 Cast:%(cast)s
00054 Genres:%(genres)s
00055 Countries:%(countries)s
00056 """ % {
00057 'title' : self.m_title,
00058 'year' : self.m_year,
00059 'releasedate' : self.m_release_date,
00060 'director' : self.m_director,
00061 'plot' : self.m_plot,
00062 'userrating' : self.m_user_rating,
00063 'movierating' : self.m_movie_rating,
00064 'runtime' : self.m_runtime,
00065 'writers' : self.m_writers,
00066 'cast' : self.m_cast,
00067 'genres' : self.m_genres,
00068 'countries' : self.m_countries
00069 }
00070
00071 items = [
00072 ItemInfo('1234567', 'Test Video 1', '2008', '2008-01-02', 'ADirector',
00073 'None.', '5.3', 'Rated PG-13', '103', 'Some Writter', 'Cast member',
00074 'Action', 'USA', 'poster1.png',
00075 ['Test Video', 'Test Video One', 'all']),
00076 ItemInfo('1234568', 'Test Video One', '2008', '2008-01-02', 'ADirector',
00077 'None.', '5.3', 'Rated PG-13', '103', 'Some Writter', 'Cast member',
00078 'Action', 'USA', 'poster2.png',
00079 ['Test Video', 'Test Video One', 'Test video 1', 'all']),
00080 ItemInfo('1234569', 'Test Video 2', '2008', '2008-01-02', 'ADirector',
00081 'None.', '5.3', 'Rated PG-13', '103', 'Some Writter', 'Cast member',
00082 'Action', 'USA', 'poster3.png', ['Test Video Two', 'all']),
00083 ItemInfo('1234570', 'Test Video 3', '2008', '2008-01-02', 'ADirector',
00084 'None.', '5.3', 'Rated PG-13', '103', 'Some Writter', 'Cast member',
00085 'Action', 'USA', '', ['Test Video Three', 'all']),
00086 ItemInfo('1234571', 'Test Video 4', '2008', '2008-01-02', 'ADirector',
00087 'None.', '5.3', 'Rated PG-13', '103', 'Some Writter', 'Cast member',
00088 'Action', 'USA', '', ['Test Video Four', 'all']),
00089 ItemInfo('1234572', 'Test Video 5', '2008', '2008-01-02', 'ADirector',
00090 'None.', '5.3', 'Rated PG-13', '103', 'Some Writter', 'Cast member',
00091 'Action', 'USA', '', ['Test Video Five', 'all']),
00092 ItemInfo('1234573', 'Test Video 6', '2008', '2008-01-02', 'ADirector',
00093 'None.', '5.3', 'Rated PG-13', '103', 'Some Writter', 'Cast member',
00094 'Action', 'USA', '', ['Test Video Six', 'all']),
00095 ItemInfo('1234574', 'Test Video 7', '2008', '2008-01-02', 'ADirector',
00096 'None.', '5.3', 'Rated PG-13', '103', 'Some Writter', 'Cast member',
00097 'Action', 'USA', '', ['Test Video Seven', 'all']),
00098 ItemInfo('1234575', 'Test Video 8', '2008', '2008-01-02', 'ADirector',
00099 'None.', '5.3', 'Rated PG-13', '103', 'Some Writter', 'Cast member',
00100 'Action', 'USA', '', ['Test Video Eight', 'all']),
00101 ItemInfo('1234576', 'Test Video 9', '2008', '2008-01-02', 'ADirector',
00102 'None.', '5.3', 'Rated PG-13', '103', 'Some Writter', 'Cast member',
00103 'Action', 'USA', '', ['Test Video Nine', 'all']),
00104 ItemInfo('1234577', 'Test Video 10', '2008', '2008-01-02', 'ADirector',
00105 'None.', '5.3', 'Rated PG-13', '103', 'Some Writter', 'Cast member',
00106 'Action', 'USA', '', ['Test Video Ten', 'all']),
00107 ItemInfo('1234578', 'Test Video 11', '2008', '2008-01-02', 'ADirector',
00108 'None.', '5.3', 'Rated PG-13', '103', 'Some Writter', 'Cast member',
00109 'Action', 'USA', '', ['Test Video Eleven', 'all']),
00110 ItemInfo('1234579', 'Test Video 12', '2008', '2008-01-02', 'ADirector',
00111 'None.', '5.3', 'Rated PG-13', '103', 'Some Writter', 'Cast member',
00112 'Action', 'USA', '', ['Test Video Twelve', 'all']),
00113 ItemInfo('1234580', 'Test Video 13', '2008', '2008-01-02', 'ADirector',
00114 'None.', '5.3', 'Rated PG-13', '103', 'Some Writter', 'Cast member',
00115 'Action', 'USA', '', ['Test Video Thirteen', 'all']),
00116 ItemInfo('1234580',
00117 'MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM',
00118 '2008', '2008-01-02', 'ADirector',
00119 'None.', '5.3', 'Rated PG-13', '103', 'Some Writter', 'Cast member',
00120 'Action', 'USA', '', ['long', 'all']),
00121 ]
00122
00123 poster_map = dict()
00124
00125 def title_search(title):
00126 for video in items:
00127 search_list = [video.m_title]
00128 if (video.m_akas):
00129 search_list.extend(video.m_akas)
00130 for ts in search_list:
00131 if ts.lower() == title.lower():
00132 print('%s:%s' % (video.m_id, video.m_title))
00133 break
00134
00135 def data_search(uid):
00136 for video in items:
00137 if video.m_id.lower() == uid.lower():
00138 print(video)
00139 break
00140
00141 def poster_search(uid):
00142 item = poster_map.get(uid)
00143 if item:
00144 print('%s/%s' % (IMAGE_URL_BASE % (URL_HOST, URL_PORT), item));
00145 else:
00146 print('%s/%s' % (IMAGE_URL_BASE % (URL_HOST, URL_PORT), 'not_there'));
00147
00148 def __open_db():
00149 conn = MySQLdb.connect(host = DB_HOST, user = DB_USER,
00150 passwd = DB_PASSWORD, db = DB_DATABASE)
00151 return conn
00152
00153 def get_db_setting(cur, name, hostname):
00154 cur.execute("SELECT data FROM settings WHERE "
00155 "value = %s AND hostname = %s", (name, hostname))
00156 row = cur.fetchone()
00157 if (row and len(row)):
00158 return row[0]
00159 return None
00160
00161 def set_db_setting(cur, name, value, hostname):
00162 cur.execute("DELETE FROM settings WHERE value = %s AND hostname = %s",
00163 (name, hostname))
00164 cur.execute("INSERT INTO settings (value, data, hostname) "
00165 "VALUES (%s, %s, %s)", (name, value, hostname))
00166
00167 def get_video_startup_dir(cur, hostname):
00168 vsd = get_db_setting(cur, 'VideoStartupDir', hostname)
00169 return vsd.split(':')[0]
00170
00171 def install():
00172 backupfile = os.path.expanduser(SETTINGS_BACKUP)
00173 if os.path.exists(backupfile):
00174 print('Error: a backup already exists, will not install over.')
00175 return
00176
00177 try:
00178 hostname = socket.gethostname()
00179
00180 conn = __open_db()
00181 c = conn.cursor()
00182 old_movie_list = get_db_setting(c, 'MovieListCommandLine', hostname)
00183 old_poster = get_db_setting(c, 'MoviePosterCommandLine', hostname)
00184 old_data = get_db_setting(c, 'MovieDataCommandLine', hostname)
00185
00186 if (old_movie_list and old_poster and old_data):
00187 f = open(backupfile, 'w')
00188 f.writelines(
00189 """MovieListCommandLine=%(movie_list_cmd)s
00190 MoviePosterCommandLine=%(movie_poster_cmd)s
00191 MovieDataCommandLine=%(movie_data_cmd)s
00192 """ % { 'movie_list_cmd' : old_movie_list,
00193 'movie_poster_cmd' : old_poster,
00194 'movie_data_cmd' : old_data})
00195 f.close()
00196
00197 thisscript = os.path.abspath(sys.argv[0])
00198 set_db_setting(c, 'MovieListCommandLine',
00199 '%s -M' % (thisscript,), hostname)
00200 set_db_setting(c, 'MoviePosterCommandLine',
00201 '%s -P' % (thisscript,), hostname)
00202 set_db_setting(c, 'MovieDataCommandLine',
00203 '%s -D' % (thisscript,), hostname)
00204
00205 vsd = get_video_startup_dir(c, hostname)
00206 testdir = os.path.join(vsd, 'mythvideo_search')
00207 print('Creating dummy video files here: %s' % (testdir,))
00208 if os.path.isdir(testdir) or os.mkdir(testdir):
00209 for item in items:
00210 f = open(os.path.join(testdir, item.m_title + '.iso'), 'w')
00211 f.write('dummyfile')
00212 f.close()
00213
00214 print('Installation complete, use --uninstall to revert.')
00215 except:
00216 raise
00217
00218 def uninstall():
00219 backupfile = os.path.expanduser(SETTINGS_BACKUP)
00220 if not os.path.exists(backupfile):
00221 print('Error: backup file %s not found, unable to restore.' %
00222 (backupfile,))
00223 return
00224
00225 try:
00226 hostname = socket.gethostname()
00227
00228 conn = __open_db()
00229 c = conn.cursor()
00230
00231 f = open(backupfile, "r")
00232 for line in f:
00233 line = line.strip()
00234 if len(line):
00235 (value, data) = line.split('=', 1)
00236 set_db_setting(c, value, data, hostname)
00237
00238 os.unlink(backupfile)
00239 vsd = get_video_startup_dir(c, hostname)
00240 testdir = os.path.join(vsd, 'mythvideo_search')
00241 print('Removing dummy video files from: %s' % (testdir,))
00242 if os.path.isdir(testdir):
00243 for item in items:
00244 tf = os.path.join(testdir, item.m_title + '.iso')
00245 if os.path.isfile(tf):
00246 os.unlink(tf)
00247 else:
00248 print('Dummy test file (%s) does not exist, ignoring' %
00249 (tf))
00250 else:
00251 print('No test directory found, ignoring.')
00252 print('Restore now complete.')
00253 except:
00254 raise
00255
00256 def main():
00257 for item in items:
00258 if item.m_poster:
00259 poster_map[item.m_id] = item.m_poster
00260
00261 parser = OptionParser()
00262 parser.add_option("-M", "--title", type="string", dest="title_search",
00263 help="Searches for entries matching TITLE.", metavar="TITLE")
00264 parser.add_option("-D", "--data", type="string", dest="data_search",
00265 help="Searches for data associated with UID.", metavar="UID")
00266 parser.add_option("-P", "--poster", type="string", dest="poster_search",
00267 help="Searches for the poster associated with UID.", metavar="UID")
00268 parser.add_option("--install", action="store_true", dest="install",
00269 help="Installs this script as the MythVideo external script for "
00270 "poster/data/search.")
00271 parser.add_option("--uninstall", action="store_true", dest="uninstall",
00272 help="Undoes what --install did.")
00273 parser.add_option("--db-user", type="string", dest="db_user",
00274 help="Database user.")
00275 parser.add_option("--db-host", type="string", dest="db_host",
00276 help="Database host.")
00277 parser.add_option("--db-password", type="string", dest="db_password",
00278 help="Database password.")
00279 parser.add_option("-p", "--port", type="int", dest="port",
00280 help="Port returned URLs point to.")
00281 parser.add_option("-u", "--url-host", type="string", dest="url_host",
00282 help="Host serving returned poster URLs.")
00283 parser.add_option("--delay", type="int", dest="search_delay", default='1',
00284 help="Delay all search commands by DELAY seconds.", metavar="DELAY")
00285 (options, args) = parser.parse_args()
00286
00287 global DB_USER, DB_PASSWORD, DB_HOST, URL_PORT, URL_HOST
00288 if options.db_user:
00289 DB_USER = options.db_user
00290 if options.db_host:
00291 DB_HOST = options.db_host
00292 if options.db_password:
00293 DB_PASSWORD = options.db_password
00294 if options.port:
00295 URL_PORT=options.port
00296 if options.url_host:
00297 URL_HOST=options.url_host
00298
00299 if options.install:
00300 install()
00301 elif options.uninstall:
00302 uninstall()
00303 elif options.title_search:
00304 time.sleep(options.search_delay)
00305 title_search(options.title_search)
00306 elif options.data_search:
00307 time.sleep(options.search_delay)
00308 data_search(options.data_search)
00309 elif options.poster_search:
00310 time.sleep(options.search_delay)
00311 poster_search(options.poster_search)
00312
00313 if __name__ == '__main__':
00314 main()