Alles in ~/.tivo/ packen; Logging verwenden

This commit is contained in:
Stefan Bethke 2014-07-01 07:04:45 +00:00
parent cb3930bf2c
commit 9ac3e036cb

View file

@ -1,6 +1,6 @@
#!/usr/local/bin/python #!/usr/local/bin/python
# $Schlepperbande: src/tivomirror/tivomirror,v 1.55 2014/06/24 16:41:00 stb Exp $ # $Schlepperbande: src/tivomirror/tivomirror,v 1.56 2014/06/28 12:16:12 stb Exp $
# #
# Stefans Script, um die Sendungen vom Tivo runterzuladen und in MPEG4 # Stefans Script, um die Sendungen vom Tivo runterzuladen und in MPEG4
# zu transkodieren. # zu transkodieren.
@ -17,6 +17,8 @@ import datetime
import getopt import getopt
import errno import errno
import functools import functools
import logging
import logging.handlers
import os import os
import re import re
import requests import requests
@ -56,9 +58,13 @@ includes['The Big Bang Theory'] = 1
includes['The Colbert Report'] = 1 includes['The Colbert Report'] = 1
includes['The Daily Show With Jon Stewart'] = 1 includes['The Daily Show With Jon Stewart'] = 1
includes['The Late Late Show With Craig Ferguson'] = 1 includes['The Late Late Show With Craig Ferguson'] = 1
#includes['The Tonight Show Starring Jimmy Fallon'] = 1 includes['The Tonight Show Starring Jimmy Fallon'] = 1
includes['"The Wil Wheaton Project'] = 1
logger = logging.getLogger('tivomirror')
logger.setLevel(logging.INFO)
class flushfile(object): class flushfile(object):
def __init__(self, f): def __init__(self, f):
self.f = f self.f = f
@ -67,10 +73,6 @@ class flushfile(object):
self.f.flush() self.f.flush()
sys.stdout = flushfile(sys.stdout) sys.stdout = flushfile(sys.stdout)
try:
os.makedirs("tivo")
except OSError:
pass
tmp = "/tmp" tmp = "/tmp"
# prepare global requests sesssion to download the TOC and the episodes # prepare global requests sesssion to download the TOC and the episodes
@ -171,7 +173,7 @@ def loadtoc(offset):
'AnchorOffset': offset 'AnchorOffset': offset
} }
url = "https://{}/TiVoConnect".format(host) url = "https://{}/TiVoConnect".format(host)
print " offset %d" % (offset) logger.debug(" offset %d" % (offset))
r = session.get(url, params=params) r = session.get(url, params=params)
if r.status_code != 200: if r.status_code != 200:
r.raise_for_status() r.raise_for_status()
@ -229,14 +231,14 @@ def quit_process(pid):
@timeout(7200) @timeout(7200)
def download(file, url, mak, target): def download(url, mak, target):
global session global session
count = 0 count = 0
start = time.time() start = time.time()
upd = start upd = start
#url = re.sub("tivo.lassitu.de:80", "localhost:8888", url) #url = re.sub("tivo.lassitu.de:80", "localhost:8888", url)
#url = re.sub("tivo.lassitu.de:80", "krokodil-vpn.zs64.net:8888", url) #url = re.sub("tivo.lassitu.de:80", "krokodil-vpn.zs64.net:8888", url)
print "--- downloading \"%s\"" % (url) logger.info("--- downloading \"%s\"" % (url))
start = time.time() start = time.time()
r = session.get(url, stream=True) r = session.get(url, stream=True)
try: try:
@ -254,7 +256,9 @@ def download(file, url, mak, target):
upd = now upd = now
dur = now - start dur = now - start
mb = count / 1e6 mb = count / 1e6
print " %5.3f GB downloaded in %.0f min, %.3f MB/s" % (mb / 1e3, dur / 60, mb / dur) logger.debug(" %5.3f GB downloaded in %.0f min, %.3f MB/s" % (mb / 1e3, dur / 60, mb / dur))
except Exception, e:
logger.error("problem decoding: %s" % (e))
finally: finally:
try: try:
p_decode.stdin.close() p_decode.stdin.close()
@ -263,13 +267,13 @@ def download(file, url, mak, target):
pass pass
size = os.path.getsize(target) size = os.path.getsize(target)
if size < 1024: if size < 1024:
print "error downloading file: too small" logger.error("error downloading file: too small")
os.remove(target) os.remove(target)
raise TivoException("downloaded file is too small") raise TivoException("downloaded file is too small")
elapsed = time.time() - start elapsed = time.time() - start
throughput = size / elapsed throughput = size / elapsed
print "%5.3fGB transferred in %d:%02d, %.1f MB/s" % ( logger.info("%5.3fGB transferred in %d:%02d, %.1f MB/s" % (
size/1e9, int(elapsed/3600), int(elapsed / 60) % 60, throughput/1e6) size/1e9, int(elapsed/3600), int(elapsed / 60) % 60, throughput/1e6))
def download_decode(item, mak): def download_decode(item, mak):
@ -280,10 +284,10 @@ def download_decode(item, mak):
except OSError: except OSError:
pass pass
if 0 & os.path.exists(target): if 0 & os.path.exists(target):
print " reusing existing download file" logger.info(" reusing existing download file")
else: else:
try: try:
download(item.file, item.url, mak, target) download(item.url, mak, target)
except Exception, e: except Exception, e:
exc_info = sys.exc_info() exc_info = sys.exc_info()
try: try:
@ -296,11 +300,11 @@ def download_decode(item, mak):
os.utime(target, (item.time, item.time)) os.utime(target, (item.time, item.time))
#os.utime(mp4, [item.date, item.date]) #os.utime(mp4, [item.date, item.date])
except Exception, e: except Exception, e:
print "Problem setting timestamp: ", e logger.error("Problem setting timestamp: %" % (e))
def savetoc(dom): def savetoc(dom):
fd=open("tivomirror-toc.xml", "w") fd=open("toc.xml", "w")
fd.write(dom.toprettyxml()) fd.write(dom.toprettyxml())
fd.close() fd.close()
@ -324,60 +328,70 @@ def wantitem(item, downloaddb):
def mirror(dom, downloaddb): def mirror(dom, downloaddb):
avail = getAvail(targetdir) avail = getAvail(targetdir)
if avail < minfree: if avail < minfree:
print "%s: %.1fG available, at least %.1fG needed, stopping" % \ logger.error("%s: %.1fG available, at least %.1fG needed, stopping" % \
(targetdir, avail / gig, minfree / gig) (targetdir, avail / gig, minfree / gig))
sys.exit(1) sys.exit(1)
items = dom.getElementsByTagName("Item") items = dom.getElementsByTagName("Item")
print "*** %d shows listed" % (items.length) logger.info("*** %d shows listed" % (items.length))
for node in items: for node in items:
item = TivoItem(node) item = TivoItem(node)
reason = wantitem(item, downloaddb) reason = wantitem(item, downloaddb)
if (reason != ""): if (reason != ""):
print "*** skipping \"%s\": %s" % (item.name, reason) logger.info("*** skipping \"%s\": %s" % (item.name, reason))
continue continue
print "*** downloading \"%s\": %.3fGB" % (item.name, item.sourcesize / 1e9) logger.info("*** downloading \"%s\": %.3fGB" % (item.name, item.sourcesize / 1e9))
try: try:
download_decode(item, mak) download_decode(item, mak)
downloaddb[item.name] = item.datestr downloaddb[item.name] = item.datestr
if getattr(downloaddb, "sync", None) and callable(downloaddb.sync): if getattr(downloaddb, "sync", None) and callable(downloaddb.sync):
downloaddb.sync() downloaddb.sync()
# stop after the first successful download since the tivo hangs easily # stop after the first successful download since the tivo hangs easily
#print "Stopping after one successful transfer" #logger.info("Stopping after one successful transfer")
#break #break
except TivoException, e: except TivoException, e:
print "Error processing \"%s\": %s" % (item.name, e) logger.info("Error processing \"%s\": %s" % (item.name, e))
# stop after one beacuse tivodevode cannot be launch twice in a row?
break
print "*** Completed %s" % datetime.datetime.now().strftime("%Y-%m-%d %H:%M") logger.info("*** Completed %s" % datetime.datetime.now().strftime("%Y-%m-%d %H:%M"))
def printtoc(dom, downloaddb): def printtoc(dom, downloaddb):
items = dom.getElementsByTagName("Item") items = dom.getElementsByTagName("Item")
print "*** %d shows listed" % (items.length) logger.info("*** %d shows listed" % (items.length))
for node in items: for node in items:
item = TivoItem(node) item = TivoItem(node)
reason = wantitem(item, downloaddb) reason = wantitem(item, downloaddb)
if (reason != ""): if (reason != ""):
print "--- %-11.11s: %s" % (reason, item.name) logger.info("--- %-11.11s: %s" % (reason, item.name))
continue continue
print "*** downloading %s (%.3fGB)" % (item.name, item.sourcesize / 1e9) logger.info("*** downloading %s (%.3fGB)" % (item.name, item.sourcesize / 1e9))
def main(): def main():
global ignoreepisodetitle global ignoreepisodetitle, logger
curdir = os.getcwd() curdir = os.getcwd()
os.chdir(tmp) os.chdir(os.path.expanduser("~") + "/.tivo")
handler = logging.handlers.RotatingFileHandler("tivomirror.log", maxBytes=2*1024*1024, backupCount=5)
handler.setFormatter(logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s'))
logger.addHandler(handler)
options, remainder = getopt.getopt(sys.argv[1:], 'T', options, remainder = getopt.getopt(sys.argv[1:], 'dvT',
['ignoreepisodetitle']) ['ignoreepisodetitle'])
for opt, arg in options: for opt, arg in options:
if opt in ('-T', '--ignoreepisodetitle'): if opt in ('-T', '--ignoreepisodetitle'):
ignoreepisodetitle = True ignoreepisodetitle = True
downloaddb = anydbm.open(os.path.expanduser("~") + "/.tivo-downloads", "c") if opt in ('-d'):
print "*** Getting listing" logger.setLevel(logging.DEBUG)
if opt in ('-v'):
handler = logging.StreamHandler()
logger.addHandler(handler)
downloaddb = anydbm.open("downloads.db", "c")
logger.info("*** Getting listing")
dom = gettoc() dom = gettoc()
savetoc(dom) savetoc(dom)
@ -390,6 +404,7 @@ def main():
mirror(dom, downloaddb) mirror(dom, downloaddb)
downloaddb.close() downloaddb.close()
logger.info("*** Completed")
if __name__ == "__main__": if __name__ == "__main__":
main() main()