Ticket #2734: mythtv.py_additions.3.diff
File mythtv.py_additions.3.diff, 5.3 KB (added by , 17 years ago) |
---|
-
mythvideo/mythvideo/scripts/MythTV.py
53 53 class MythDB: 54 54 def __init__(self): 55 55 """ 56 A connection to the mythtv database 56 A connection to the mythtv database. 57 57 """ 58 58 config_files = [ 59 59 '/usr/local/share/mythtv/mysql.txt', … … 129 129 130 130 class MythTV: 131 131 """ 132 A connection to MythTV backend 132 A connection to MythTV backend. 133 133 """ 134 134 def __init__(self, conn_type='Monitor'): 135 135 self.db = MythDB() … … 156 156 157 157 def backendCommand(self, data): 158 158 """ 159 Sends a command via a socket to the mythbackend in160 the format that it expects. Returns the result from 161 the backend159 Sends a formatted command via a socket to the mythbackend. 160 161 Returns the result from the backend. 162 162 """ 163 163 def recv(): 164 164 """ 165 Reads the data returned f omr the backend165 Reads the data returned from the backend. 166 166 """ 167 167 # The first 8 bytes of the response gives us the length 168 168 data = self.socket.recv(8) … … 184 184 185 185 def getPendingRecordings(self): 186 186 """ 187 Returns a list of Program objects which are scheduled to be 188 recorded 187 Returns a list of Program objects which are scheduled to be recorded. 189 188 """ 190 189 programs = [] 191 190 res = self.backendCommand('QUERY_GETALLPENDING').split(BACKEND_SEP) … … 199 198 200 199 def getScheduledRecordings(self): 201 200 """ 202 Returns a list of Program objects which are scheduled to be 203 recorded 201 Returns a list of Program objects which are scheduled to be recorded. 204 202 """ 205 203 programs = [] 206 204 res = self.backendCommand('QUERY_GETALLSCHEDULED').split(BACKEND_SEP) … … 213 211 214 212 def getUpcomingRecordings(self): 215 213 """ 216 Returns a list of Program objects for programs which are actually 217 going to be recorded. 214 Returns a list of Program objects which are scheduled to be recorded. 215 216 Sorts the list by recording start time and only returns those with 217 record status of WillRecord. 218 218 """ 219 219 def sort_programs_by_starttime(x, y): 220 220 if x.starttime > y.starttime: … … 231 231 programs.sort(sort_programs_by_starttime) 232 232 return programs 233 233 234 def getRecorderList(self): 235 """ 236 Returns a list of recorders, or an empty list if none. 237 """ 238 recorders = [] 239 c = self.db.cursor() 240 c.execute('SELECT cardid FROM capturecard') 241 row = c.fetchone() 242 while row is not None: 243 recorders.append(int(row[0])) 244 row = c.fetchone() 245 c.close() 246 return recorders 247 234 248 def getFreeRecorderList(self): 235 249 """ 236 Returns a list of free recorders, or an empty list if none 250 Returns a list of free recorders, or an empty list if none. 237 251 """ 238 252 res = self.backendCommand('GET_FREE_RECORDER_LIST').split(BACKEND_SEP) 239 253 recorders = [int(d) for d in res] 240 if recorders[0]: 241 return recorders 254 return recorders 255 256 def getRecorderDetails(self, recorder_id): 257 """ 258 Returns a Recorder object with details of the recorder. 259 """ 260 c = self.db.cursor() 261 c.execute("""SELECT cardid, cardtype, videodevice, hostname 262 FROM capturecard WHERE cardid = %s""", recorder_id) 263 row = c.fetchone() 264 if row: 265 recorder = Recorder(row) 266 return recorder 242 267 else: 243 return []268 return None 244 269 245 270 def getCurrentRecording(self, recorder): 271 """ 272 Returns a Program object for the current recorders recording. 273 """ 246 274 res = self.backendCommand('QUERY_RECORDER %s[]:[]GET_CURRENT_RECORDING' % recorder) 247 275 return Program(res.split(BACKEND_SEP)) 248 276 249 277 def isRecording(self, recorder): 278 """ 279 Returns a boolean as to whether the given recorder is recording. 280 """ 250 281 res = self.backendCommand('QUERY_RECORDER %s[]:[]IS_RECORDING' % recorder) 251 282 if res == '1': 252 283 return True 253 284 else: 254 285 return False 255 286 287 def isActiveBackend(self, hostname): 288 """ 289 Returns a boolean as to whether the given host is an active backend 290 """ 291 res = self.backendCommand('QUERY_IS_ACTIVE_BACKEND[]:[]%s' % hostname) 292 if res == 'TRUE': 293 return True 294 else: 295 return False 296 256 297 class MythVideo: 257 298 def __init__(self): 258 299 self.db = MythDB() 259 300 260 301 def pruneMetadata(self): 302 """ 303 Removes metadata from the database for files that no longer exist. 304 """ 261 305 c = self.db.cursor() 262 306 c.execute(""" 263 307 SELECT intid, filename … … 336 380 return None 337 381 338 382 def setMetadata(self, data, id=None): 383 """ 384 Adds or updates the metadata in the database for a video item. 385 """ 339 386 c = self.db.cursor() 340 387 if id is None: 341 388 fields = ', '.join(data.keys()) … … 354 401 c.execute(sql, sql_values) 355 402 c.close() 356 403 404 class Recorder: 405 def __str__(self): 406 return "Recorder %s (%s)" % (self.cardid, self.cardtype) 407 408 def __repr__(self): 409 return "Recorder %s (%s)" % (self.cardid, self.cardtype) 410 411 def __init__(self, data): 412 """ 413 Load the list of data into the object. 414 """ 415 self.cardid = data[0] 416 self.cardtype = data[1] 417 self.videodevice = data[2] 418 self.hostname = data[3] 419 357 420 class Program: 421 def __str__(self): 422 return "%s (%s)" % (self.title, self.starttime.strftime('%Y-%m-%d %H:%M:%S')) 423 424 def __repr__(self): 425 return "%s (%s)" % (self.title, self.starttime.strftime('%Y-%m-%d %H:%M:%S')) 426 358 427 def __init__(self, data): 359 428 """ 360 Load the list of data into the object 429 Load the list of data into the object. 361 430 """ 362 431 self.title = data[0] 363 432 self.subtitle = data[1]