Skip to content

Commit

Permalink
Adjust to datetime issues with MythTV v0.26's move to UTC datetimes i…
Browse files Browse the repository at this point in the history
…n DB

Signed-off-by: Raymond Wagner <rwagner@mythtv.org>
  • Loading branch information
rdv authored and wagnerrp committed Oct 6, 2012
1 parent 1efa59b commit fcf6da5
Showing 1 changed file with 58 additions and 38 deletions.
96 changes: 58 additions & 38 deletions mythtv/contrib/imports/mirobridge/mirobridge.py
Expand Up @@ -30,7 +30,7 @@
Miro v2.0.3 or later must already be installed and configured and capable of downloading videos. Miro v2.0.3 or later must already be installed and configured and capable of downloading videos.
''' '''


__version__=u"v0.6.8" __version__=u"v0.6.9"
# 0.1.0 Initial development # 0.1.0 Initial development
# 0.2.0 Initial Alpha release for internal testing only # 0.2.0 Initial Alpha release for internal testing only
# 0.2.1 Fixes from initial alpha test # 0.2.1 Fixes from initial alpha test
Expand Down Expand Up @@ -209,15 +209,17 @@
# by MythVideo # by MythVideo
# Fixed the options "-h, --help" command line display # Fixed the options "-h, --help" command line display
# 0.6.8 Sometimes Miro metadata has no video filename. Skip these invalid videos. # 0.6.8 Sometimes Miro metadata has no video filename. Skip these invalid videos.
# 0.6.9 Adjust to datetime issues with MythTV v0.26's move to UTC datatimes in DB


examples_txt=u''' examples_txt=u'''
For examples, please see the Mirobridge's wiki page at http://www.mythtv.org/wiki/MiroBridge For examples, please see the Mirobridge's wiki page at http://www.mythtv.org/wiki/MiroBridge
''' '''


# Common function imports # Common function imports
import sys, os, re, locale, subprocess, locale, ConfigParser, codecs, shutil, struct import sys, os, re, locale, subprocess, locale, ConfigParser, codecs, shutil, struct
import datetime, fnmatch, string, time, logging, traceback, platform, fnmatch, ConfigParser import fnmatch, string, time, logging, traceback, platform, fnmatch, ConfigParser
from datetime import date from datetime import timedelta
from dateutil.tz import tzutc, tzlocal
from optparse import OptionParser from optparse import OptionParser
from socket import gethostbyname from socket import gethostbyname
import formatter import formatter
Expand Down Expand Up @@ -392,6 +394,7 @@ def __getattr__(self, attr):
from MythTV import OldRecorded, Recorded, RecordedProgram, Record, Channel, \ from MythTV import OldRecorded, Recorded, RecordedProgram, Record, Channel, \
MythDB, Video, MythVideo, MythBE, MythError, MythLog MythDB, Video, MythVideo, MythBE, MythError, MythLog
from MythTV.database import DBDataWrite from MythTV.database import DBDataWrite
from MythTV.utility import datetime
mythdb = None mythdb = None
mythbeconn = None mythbeconn = None
try: try:
Expand Down Expand Up @@ -634,7 +637,7 @@ def isMiroRunning():
return False if Miro is NOT running return False if Miro is NOT running
''' '''
try: try:
p = subprocess.Popen(u'ps aux | grep "miro.real"', shell=True, bufsize=4096, p = subprocess.Popen(u'ps aux | grep "miro.real"', shell=True, bufsize=4096,
stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
close_fds=True) close_fds=True)
except: except:
Expand Down Expand Up @@ -676,34 +679,48 @@ def is_not_punct_char(char):
return not is_punct_char(char) return not is_punct_char(char)




class delOldRecorded( OldRecorded ): def delOldRecorded(chanid, starttime, endtime, title, \
subtitle, description):
''' '''
Just delete an oldrecorded record. Never abort as sometimes a record may not exist. This routine is not supported in the native python bindings as MiroBridge uses the
This routine is not supported in the native python bindings as MiroBridge uses the oldrecorded table outside of its original intent.
oldrecorded table outside of its original intent. return nothing return nothing
''' '''
_table = 'oldrecorded' sql_cmd = u"""DELETE FROM `mythconverg`.`oldrecorded`
def delete(self): WHERE `oldrecorded`.`chanid` = '%s'
""" AND `oldrecorded`.`starttime` = '%s'
Delete video entry from database. AND `oldrecorded`.`endtime` = '%s';"""
""" sql_del_a_record(sql_cmd, (chanid, set_del_datatime(starttime), set_del_datatime(endtime)))
DBDataWrite.delete(self)
# end delOldRecorded() # end delOldRecorded()
MythDB.searchOldRecorded.handler = delOldRecorded




class delRecorded( Recorded ): def delRecorded(chanid, starttime):
'''Just delete a recorded record. Never abort as sometimes a record may not exist. '''Just delete a recorded record. Never abort as sometimes a record may not exist.
return nothing return nothing
''' '''
_table = 'recorded' sql_cmd = u"""DELETE FROM `mythconverg`.`recorded`
def delete(self): WHERE `recorded`.`chanid` = %s
""" AND `recorded`.`starttime` = '%s';"""
Delete video entry from database. sql_del_a_record(sql_cmd, (chanid, set_del_datatime(starttime)))
"""
DBDataWrite.delete(self)
# end delRecorded() # end delRecorded()
MythDB.searchRecorded.handler = delRecorded

def set_del_datatime(rec_time):
''' Set the SQL datetime so that the delRecorded and delOldrecorded
methods use UTC datetime values.
return rec_time
'''
#
return rec_time.replace(tzinfo=tzlocal()).astimezone(
tzutc()).strftime("%Y-%m-%d %H:%M:%S")

def sql_del_a_record(sql_cmd):
## Get a MythTV data base cursor
cursor = mythdb.cursor()
cursor.execute(sql_cmd)
cursor.close()
#
return




def hashFile(filename): def hashFile(filename):
Expand Down Expand Up @@ -1170,8 +1187,9 @@ def getOldrecordedOrphans():
try: try:
# Sometimes a channel issues videos with identical publishing (starttime) dates. # Sometimes a channel issues videos with identical publishing (starttime) dates.
# Try to using additiional details to identify the correct oldrecord. # Try to using additiional details to identify the correct oldrecord.
delOldRecorded((channel_id, data['starttime'], data['endtime'], data['title'], delOldRecorded(channel_id, data['starttime'],
data['subtitle'], data['description'])).delete() data['endtime'], data['title'],
data['subtitle'], data['description'])
except: except:
pass pass


Expand Down Expand Up @@ -1208,7 +1226,7 @@ def getStartEndTimes(duration, downloadedTime):
return an array of initialised values if either duration or downloadedTime is invalid return an array of initialised values if either duration or downloadedTime is invalid
return an array of the video's start, end times and isotime return an array of the video's start, end times and isotime
''' '''
starttime = datetime.datetime.now() starttime = datetime.now()
end = starttime end = starttime
start_end = [starttime.strftime('%Y-%m-%d %H:%M:%S'), start_end = [starttime.strftime('%Y-%m-%d %H:%M:%S'),
starttime.strftime('%Y-%m-%d %H:%M:%S'), starttime.strftime('%Y-%m-%d %H:%M:%S'),
Expand All @@ -1218,8 +1236,8 @@ def getStartEndTimes(duration, downloadedTime):
try: try:
dummy = downloadedTime.strftime('%Y-%m-%d') dummy = downloadedTime.strftime('%Y-%m-%d')
except ValueError: except ValueError:
downloadedTime = datetime.datetime.now() downloadedTime = datetime.now()
end = downloadedTime+datetime.timedelta(seconds=duration) end = downloadedTime+timedelta(seconds=duration)
start_end[0] = downloadedTime.strftime('%Y-%m-%d %H:%M:%S') start_end[0] = downloadedTime.strftime('%Y-%m-%d %H:%M:%S')
start_end[1] = end.strftime('%Y-%m-%d %H:%M:%S') start_end[1] = end.strftime('%Y-%m-%d %H:%M:%S')
start_end[2] = downloadedTime.strftime('%Y%m%d%H%M%S') start_end[2] = downloadedTime.strftime('%Y%m%d%H%M%S')
Expand All @@ -1229,8 +1247,8 @@ def getStartEndTimes(duration, downloadedTime):
while True: while True:
if not len(list(mythdb.searchOldRecorded(chanid=channel_id, starttime=start_end[0]))): if not len(list(mythdb.searchOldRecorded(chanid=channel_id, starttime=start_end[0]))):
break break
starttime = starttime + datetime.timedelta(0,1) starttime = starttime + timedelta(0,1)
end = end + datetime.timedelta(0,1) end = end + timedelta(0,1)
start_end[0] = starttime.strftime('%Y-%m-%d %H:%M:%S') start_end[0] = starttime.strftime('%Y-%m-%d %H:%M:%S')
start_end[1] = end.strftime('%Y-%m-%d %H:%M:%S') start_end[1] = end.strftime('%Y-%m-%d %H:%M:%S')
start_end[2] = starttime.strftime('%Y%m%d%H%M%S') start_end[2] = starttime.strftime('%Y%m%d%H%M%S')
Expand Down Expand Up @@ -1574,7 +1592,7 @@ def createChannelRecord(icon, channel_id, channel_num):
data['icon'] = icon data['icon'] = icon
data['callsign'] = u'Miro' data['callsign'] = u'Miro'
data['name'] = u'Miro' data['name'] = u'Miro'
data['last_record'] = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S') data['last_record'] = datetime.now().strftime('%Y-%m-%d %H:%M:%S')


if simulation: if simulation:
logger.info(u"Simulation: Create Miro channel record channel_id(%d) and channel_num(%d)" % \ logger.info(u"Simulation: Create Miro channel record channel_id(%d) and channel_num(%d)" % \
Expand Down Expand Up @@ -1740,7 +1758,7 @@ def updateMythRecorded(items):
try: try:
# Attempting to clean up an recorded record and # Attempting to clean up an recorded record and
# its associated video file (miro symlink) # its associated video file (miro symlink)
rtn = delRecorded((record['chanid'], record['starttime'])).delete() rtn = delRecorded(record['chanid'], record['starttime'])
except MythError, e: except MythError, e:
pass pass


Expand All @@ -1750,9 +1768,9 @@ def updateMythRecorded(items):
record[u'starttime'].strftime('%Y%m%d%H%M%S'), u'png')) record[u'starttime'].strftime('%Y%m%d%H%M%S'), u'png'))


try: # Attempting to clean up an orphaned oldrecorded record which may or may not exist try: # Attempting to clean up an orphaned oldrecorded record which may or may not exist
rtn = delOldRecorded((record['chanid'], record['starttime'], rtn = delOldRecorded(record['chanid'], record['starttime'],
record['endtime'], record['title'], record['endtime'], record['title'],
record['subtitle'], record['description'])).delete() record['subtitle'], record['description'])
except Exception, e: except Exception, e:
pass pass


Expand Down Expand Up @@ -1913,10 +1931,12 @@ def updateMythVideo(items):
try: # An orphaned oldrecorded record may not exist try: # An orphaned oldrecorded record may not exist
for oldrecorded in mythdb.searchOldRecorded(title=record[u'title'], for oldrecorded in mythdb.searchOldRecorded(title=record[u'title'],
subtitle=record[u'subtitle'] ): subtitle=record[u'subtitle'] ):
rtn = delOldRecorded((channel_id, oldrecorded['starttime'], \ rtn = delOldRecorded(channel_id,
oldrecorded['endtime'], oldrecorded['title'], \ oldrecorded['starttime'],
oldrecorded['subtitle'], oldrecorded['description'])).\ oldrecorded['endtime'],
delete() oldrecorded['title'],
oldrecorded['subtitle'],
oldrecorded['description'])
except Exception, e: except Exception, e:
pass pass
statistics[u'Total_Miro_MythVideos']-=1 statistics[u'Total_Miro_MythVideos']-=1
Expand Down

0 comments on commit fcf6da5

Please sign in to comment.