def run(self): td = tDiff(self.von,datetime.now()) deltas = total(td) self.timer = Timer(deltas, self.doRecord) self.timer.start() if deltas>0: print ("Record: Thread timer for '%s' started for %d seconds" % (self.name, deltas))
def run(self): self.kill() if not config.cfg_grab_time == '0' and len(config.cfg_grab_time)>=3: try: mytime = datetime.strptime(config.cfg_grab_time, "%H:%M").time() mydatetime = datetime.combine(datetime.now().date(), mytime) if mydatetime < datetime.now(): mydatetime = mydatetime + timedelta(days=1) td = tDiff(mydatetime,datetime.now()) deltas = total(td) self.timer = Timer(deltas, self.doGrab) self.timer.start() if deltas>0: print ("EPG Thread timer waiting till %s (%d seconds)" % (config.cfg_grab_time, deltas)) except: print ("Something went wrong with EPG thread. Please check your config settings regarding your start time")
def doRecord(self): self.running = 1 fftypes = config.cfg_ffmpeg_types fftypes = fftypes.lower().split() streamtype = self.url.lower().split(':', 1)[0] ffargs = config.cfg_ffmpeg_params ffargs = ffargs.split() dateholder = datetime.now().strftime("%Y%m%d%H%M%S") titleholder = "".join([x if x.isalnum() else "_" for x in self.name]) if sys.version_info[0] < 3 and streamtype in fftypes: # workaround for unicode, damn me if I ever get it working with 2.x titleholder = "".join([x if ord(x) < 128 else "_" for x in titleholder]) idholder = "%04d" % (self.myrow[8], ) fn = config.cfg_record_mask # Placeholders fn = fn.replace("%date%", dateholder) fn = fn.replace("%title%", titleholder) fn = fn.replace("%month%", datetime.now().strftime("%m")) fn = fn.replace("%year%", datetime.now().strftime("%Y")) fn = fn.replace("%day%", datetime.now().strftime("%d")) fn = fn.replace("%channelid%", idholder) fn = fn.replace("%channel%", self.myrow[9]) # Placeholders end for i in range(0, len(ffargs)): ffargs[i] = ffargs[i].replace("%date%", dateholder).replace("%title%", titleholder).replace("%month%", datetime.now().strftime("%m")).replace("%year%", datetime.now().strftime("%Y")).replace("%day%", datetime.now().strftime("%d")).replace("%channelid%", idholder).replace("%channel%", self.myrow[9]) if "/" in fn or "\\" in fn: try: path = fn.replace('\\', "/") pos = path.rfind("/") path = path[:pos] path = config.cfg_recordpath + path os.makedirs (path) except Exception as ex: pass fn = config.cfg_recordpath + fn # Check, if destination file already exists fn_check = fn + self.ext num = 1 while fileexists(fn_check) and num<127: fn_check = fn + ("_%s" % num) + self.ext num += 1 fn = fn_check # End check if streamtype in fftypes: delta = total(tDiff(self.bis, datetime.now())) deltasec = '%d' % delta attr = [config.cfg_ffmpeg_path,"-i", self.url, '-y', '-t', deltasec] + ffargs + [fn] print ("FFMPEG (%s) record '%s' called with:" % (streamtype, self.name)) print (attr) try: if config.cfg_switch_proxy == "1" and config.cfg_proxy != "": os.environ["http_proxy"] = config.cfg_proxy else: os.environ["http_proxy"] = "" self.process = subprocess.Popen(attr, stdout=subprocess.PIPE, stderr=subprocess.PIPE) cleaner = Timer(delta+30, self.cleanProcess) # if ffmpeg won't exit, try to terminate its process in 30 seconds cleaner.start() out, err = self.process.communicate() #self.process.wait() # oops... not needed? harmless! cleaner.cancel() if err: print ("FFMPEG record '%s' ended with an error:\n%s" % (self.name, err)) else: print ("FFMPEG record '%s' ended" % self.name) except: print ("FFMPEG could not be started") else: block_sz = 8192 print ("Record: '%s' started" % (self.name)) try: u = urllib32.urlopen(self.url) try: f = open(fn, 'wb') except: f = open(fn.encode('utf-8').decode(sys.getfilesystemencoding()), 'wb') #except urllib32.URLError: # print ("Stream could not be parsed (URL=%s), aborting..." % (self.url)) except ValueError as ex: print ("Unknown URL type (%s), record could not be started. Please check your channel settings" % (self.url)) except Exception as ex: print ("Output file %s could not be created. Please check your settings. Description: %s" % (fn, ex)) else: internalRetryCount = 0 maxRetryCount = 100 mybuffer = None while self.bis > datetime.now() and self.stopflag==0: try: mybuffer = u.read(block_sz) doInternalRetry = False except: doInternalRetry = True if (not mybuffer or doInternalRetry) and internalRetryCount < maxRetryCount: # connection lost? internalRetryCount += 1 try: u = urllib32.urlopen(self.url) mybuffer = u.read(block_sz) f.write(mybuffer) except: pass elif internalRetryCount >= maxRetryCount: print ("Record: '%s': too many internal retries, aborting..." % (self.name)) break else: f.write(mybuffer) f.close() if internalRetryCount > 0: print ("Record: '%s' ended with %s internal retries, please check your connection stability" % (self.name, internalRetryCount)) else: print ("Record: '%s' ended" % (self.name)) if config.cfg_switch_postprocess == "1" and config.cfg_postprocess != "": if fileexists(fn): attr = [] attr = config.cfg_postprocess.split(" ") for i in range(0, len(attr)): attr[i] = attr[i].replace("%file%", fn) print ("Postprocessing will be called with following parameters:") print (attr) try: postprocess = subprocess.Popen(attr, stdout=subprocess.PIPE, stderr=subprocess.PIPE) out, err = postprocess.communicate() except: print ("Exception calling postprocessing, please check your command line") # 2015-01-21 Fail & recurrency check if datetime.now() < self.bis - timedelta(seconds=10) and self.stopflag==0: delta = total(tDiff(self.bis, datetime.now())) if self.retry_count == 0: print ("Something went wrong with '%s'. No retries configured, aborting..." % (self.name)) sleep(delta) elif self.retries == self.retry_count: print ("Something went wrong with '%s'. Last retry reached, aborting..." % (self.name)) sleep(delta) elif self.retries < self.retry_count: self.retries += 1 print ("Something went wrong with '%s', retry %s/%s in 10 seconds" % (self.name, self.retries, self.retry_count)) sleep(10) self.run() return self.clean() rectimer = Timer(10, setRecords) rectimer.start()
def doRecord(self): self.running = 1 dateholder = datetime.now().strftime("%Y%m%d%H%M%S") titleholder = "".join([x if x.isalnum() else "_" for x in self.name]) fn = config.cfg_recordpath + config.cfg_record_mask.replace("%date%", dateholder).replace("%title%", titleholder) + self.ext num = 1 while fileexists(fn) and num<127: fn = config.cfg_recordpath + config.cfg_record_mask.replace("%date%", dateholder).replace("%title%", titleholder) + ("_%s" % num) + self.ext num += 1 fftypes = config.cfg_ffmpeg_types fftypes = fftypes.lower().split() streamtype = self.url.lower().split(':', 1)[0] ffargs = config.cfg_ffmpeg_params ffargs = ffargs.split() if streamtype in fftypes: delta = total(tDiff(self.bis, datetime.now())) deltasec = '%d' % delta attr = [config.cfg_ffmpeg_path,"-i", self.url, '-y', '-loglevel', 'fatal', '-t', deltasec] + ffargs + [fn] print ("FFMPEG (%s) record '%s' called with:" % (streamtype, self.name)) print (attr) try: self.process = subprocess.Popen(attr, stdout=subprocess.PIPE, stderr=subprocess.PIPE) cleaner = Timer(delta+30, self.cleanProcess) # if ffmpeg won't exit, try to terminate its process in 30 seconds cleaner.start() out, err = self.process.communicate() #self.process.wait() # oops... not needed? harmless! cleaner.cancel() if err: print ("FFMPEG record '%s' ended with an error:\n%s" % (self.name, err)) else: print ("FFMPEG record '%s' ended" % self.name) except: print ("FFMPEG could not be started") else: block_sz = 8192 print ("Record: '%s' started" % (self.name)) try: u = urllib32.urlopen(self.url) try: f = open(fn, 'wb') except: f = open(fn.encode('utf-8').decode(sys.getfilesystemencoding()), 'wb') except urllib32.URLError: print ("Stream could not be parsed (URL=%s), aborting..." % (self.url)) except ValueError as ex: print ("Unknown URL type (%s), record could not be started. Please check your channel settings" % (self.url)) except Exception as ex: print ("Output file %s could not be created. Please check your settings. (Err: %s)" % (fn, ex)) else: internalRetryCount = 0 maxRetryCount = 100 while self.bis > datetime.now() and self.stopflag==0: try: mybuffer = u.read(block_sz) doInternalRetry = False except: doInternalRetry = True if (not mybuffer or doInternalRetry) and internalRetryCount < maxRetryCount: # connection lost? internalRetryCount += 1 try: u = urllib32.urlopen(self.url) mybuffer = u.read(block_sz) f.write(mybuffer) except: pass elif internalRetryCount >= maxRetryCount: print ("Record: '%s': too many internal retries, aborting..." % (self.name)) break else: f.write(mybuffer) f.close() if internalRetryCount > 0: print ("Record: '%s' ended with %s internal retries, please check your connection stability" % (self.name, internalRetryCount)) else: print ("Record: '%s' ended" % (self.name)) if config.cfg_switch_postprocess == "1" and config.cfg_postprocess != "": if fileexists(fn): attr = [] attr = config.cfg_postprocess.split(" ") for i in range(0, len(attr)): attr[i] = attr[i].replace("%file%", fn) print ("Postprocessing will be called with following parameters:") print (attr) try: postprocess = subprocess.Popen(attr, stdout=subprocess.PIPE, stderr=subprocess.PIPE) out, err = postprocess.communicate() except: print ("Exception calling postprocessing, please check your command line") # 2015-01-21 Fail & recurrency check if datetime.now() < self.bis - timedelta(seconds=10) and self.stopflag==0: delta = total(tDiff(self.bis, datetime.now())) if self.retry_count == 0: print ("Something went wrong with '%s'. No retries configured, aborting..." % (self.name)) sleep(delta) elif self.retries == self.retry_count: print ("Something went wrong with '%s'. Last retry reached, aborting..." % (self.name)) sleep(delta) elif self.retries < self.retry_count: self.retries += 1 print ("Something went wrong with '%s', retry %s/%s in 10 seconds" % (self.name, self.retries, self.retry_count)) sleep(10) self.run() return self.clean() rectimer = Timer(10, setRecords) rectimer.start()
def doRecord(self): self.running = 1 dateholder = datetime.now().strftime("%Y%m%d%H%M%S") titleholder = "".join([x if x.isalnum() else "_" for x in self.name]) fn = config.cfg_recordpath + config.cfg_record_mask.replace( "%date%", dateholder).replace("%title%", titleholder) + self.ext num = 1 while fileexists(fn) and num < 127: fn = config.cfg_recordpath + config.cfg_record_mask.replace( "%date%", dateholder).replace( "%title%", titleholder) + ("_%s" % num) + self.ext num += 1 fftypes = config.cfg_ffmpeg_types fftypes = fftypes.lower().split() streamtype = self.url.lower().split(':', 1)[0] ffargs = config.cfg_ffmpeg_params ffargs = ffargs.split() if streamtype in fftypes: delta = total(tDiff(self.bis, datetime.now())) deltasec = '%d' % delta attr = [ config.cfg_ffmpeg_path, "-i", self.url, '-y', '-loglevel', 'fatal', '-t', deltasec ] + ffargs + [fn] print("FFMPEG (%s) record '%s' called with:" % (streamtype, self.name)) print(attr) try: self.process = subprocess.Popen(attr, stdout=subprocess.PIPE, stderr=subprocess.PIPE) cleaner = Timer( delta + 30, self.cleanProcess ) # if ffmpeg won't exit, try to terminate its process in 30 seconds cleaner.start() out, err = self.process.communicate() #self.process.wait() # oops... not needed? harmless! cleaner.cancel() if err: print("FFMPEG record '%s' ended with an error:\n%s" % (self.name, err)) else: print("FFMPEG record '%s' ended" % self.name) except: print("FFMPEG could not be started") else: block_sz = 8192 print("Record: '%s' started" % (self.name)) try: u = urllib32.urlopen(self.url) try: f = open(fn, 'wb') except: f = open( fn.encode('utf-8').decode(sys.getfilesystemencoding()), 'wb') except urllib32.URLError: print("Stream could not be parsed (URL=%s), aborting..." % (self.url)) except ValueError as ex: print( "Unknown URL type (%s), record could not be started. Please check your channel settings" % (self.url)) except Exception as ex: print( "Output file %s could not be created. Please check your settings. (Err: %s)" % (fn, ex)) else: internalRetryCount = 0 maxRetryCount = 100 while self.bis > datetime.now() and self.stopflag == 0: try: mybuffer = u.read(block_sz) doInternalRetry = False except: doInternalRetry = True if ( not mybuffer or doInternalRetry ) and internalRetryCount < maxRetryCount: # connection lost? internalRetryCount += 1 try: u = urllib32.urlopen(self.url) mybuffer = u.read(block_sz) f.write(mybuffer) except: pass elif internalRetryCount >= maxRetryCount: print( "Record: '%s': too many internal retries, aborting..." % (self.name)) break else: f.write(mybuffer) f.close() if internalRetryCount > 0: print( "Record: '%s' ended with %s internal retries, please check your connection stability" % (self.name, internalRetryCount)) else: print("Record: '%s' ended" % (self.name)) if config.cfg_switch_postprocess == "1" and config.cfg_postprocess != "": if fileexists(fn): attr = [] attr = config.cfg_postprocess.split(" ") for i in range(0, len(attr)): attr[i] = attr[i].replace("%file%", fn) print( "Postprocessing will be called with following parameters:") print(attr) try: postprocess = subprocess.Popen(attr, stdout=subprocess.PIPE, stderr=subprocess.PIPE) out, err = postprocess.communicate() except: print( "Exception calling postprocessing, please check your command line" ) # 2015-01-21 Fail & recurrency check if datetime.now() < self.bis - timedelta( seconds=10) and self.stopflag == 0: delta = total(tDiff(self.bis, datetime.now())) if self.retry_count == 0: print( "Something went wrong with '%s'. No retries configured, aborting..." % (self.name)) sleep(delta) elif self.retries == self.retry_count: print( "Something went wrong with '%s'. Last retry reached, aborting..." % (self.name)) sleep(delta) elif self.retries < self.retry_count: self.retries += 1 print( "Something went wrong with '%s', retry %s/%s in 10 seconds" % (self.name, self.retries, self.retry_count)) sleep(10) self.run() return self.clean() rectimer = Timer(10, setRecords) rectimer.start()