Ticket #13114: mythtv_import_evtest.py

File mythtv_import_evtest.py, 2.9 KB (added by devmonkey@…, 2 years ago)

Import recorder event test script

Line 
1#!/usr/bin/env python
2
3# mythtv_import_evtest.py
4# sample import script by mythrec (c)2017
5# Licensed under GPL-3.0+ <http://spdx.org/licenses/GPL-3.0+>"
6
7# This is for use with the MythTV Import Recorder and System Events
8# Typically in conjunction with RecordingStarted and RecordingFinished events
9# see https://www.mythtv.org/wiki/Import_recorder
10# FIXME Update app path & log filename below as appropriate
11
12# You should call this script in the Recording Started event e.g.
13# mythtv_import_evtest.py -s %STARTTIMEUTC% -e %ENDTIMEUTC% -f %FILE% -c %CHANID% -t %CARDID%
14# see https://www.mythtv.org/wiki/MythTV_System_Events
15
16from MythTV import MythBE,MythDB,Channel
17import sys
18import os
19import getopt
20import datetime
21import time
22
23#Import a recording
24def imp(card,chanid,st,et,file):
25    mythtv = MythBE()
26    #xml_id=Channel(chanid).xmltvid
27
28    retry=0
29    while True:
30        #this is not necessarily the prog we want to start recording
31        #i.e. could still be on the previous show if they are back to back
32        prog=mythtv.getCurrentRecording(card)
33        if prog and prog.filename and file in prog.filename:
34            fp=prog.filename
35            break
36        retry+=1
37        if retry>1:
38            raise RuntimeError('timeout when waiting for import path')
39        time.sleep(30)
40
41    sd=(et - st).total_seconds()
42    f.write("Starting import %s\n"%(fp))
43    app='(mythtv_import.sh "%s" %d &)' % (fp,sd) #FIXME Update app path
44    f.write("%s\n"%(app))
45    os.system(app)
46
47#Check command line options
48def parseCliOpts(argc,argv):
49    opts,args = getopt.getopt(argv[1:],'s:e:f:c:t:')
50    for (opt,arg) in opts:
51        if opt == '-s':
52            start=datetime.datetime.strptime(arg,"%Y%m%d%H%M%S")
53        elif opt == '-e':
54            end=datetime.datetime.strptime(arg,"%Y%m%d%H%M%S")
55        elif opt == '-f':
56            file=arg
57        elif opt == '-c':
58            chan=int(arg)
59        elif opt == '-t':
60            card=int(arg)
61           
62    mythdb = MythDB()   
63    with mythdb as cursor:     
64        cursor.execute("""SELECT cardtype from capturecard WHERE cardid=%s""",(card,))   
65        row = cursor.fetchone()     
66        if row is None:
67            raise ValueError('unknown card number',card)
68        ct=row[0]   
69               
70    if (ct=="IMPORT"):
71        imp(card,chan,start,end,file)
72
73#Main
74def main(argc,argv):
75    parseCliOpts(argc,argv)
76
77if __name__ == "__main__":
78    f=open('/home/mythtv/import.log','a') #FIXME Update as appropriate
79    try:
80        f.write("=======IMP-START:%s=======\n"%(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")))
81        f.write(" ".join(sys.argv[:]))
82        f.write("\n")
83        main(len(sys.argv),sys.argv)
84        f.write("=======IMP-END:%s=======\n"%(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")))
85        f.close()
86    except Exception, e:
87        print 'Caught main exception:',e
88        f.write("IMP-ERR: Exception %s\n"%(e))
89        sys.exit(1)