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
# $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
# zu transkodieren.
@ -17,6 +17,8 @@ import datetime
import getopt
import errno
import functools
import logging
import logging.handlers
import os
import re
import requests
@ -56,9 +58,13 @@ includes['The Big Bang Theory'] = 1
includes['The Colbert Report'] = 1
includes['The Daily Show With Jon Stewart'] = 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):
def __init__(self, f):
self.f = f
@ -67,10 +73,6 @@ class flushfile(object):
self.f.flush()
sys.stdout = flushfile(sys.stdout)
try:
os.makedirs("tivo")
except OSError:
pass
tmp = "/tmp"
# prepare global requests sesssion to download the TOC and the episodes
@ -171,7 +173,7 @@ def loadtoc(offset):
'AnchorOffset': offset
}
url = "https://{}/TiVoConnect".format(host)
print " offset %d" % (offset)
logger.debug(" offset %d" % (offset))
r = session.get(url, params=params)
if r.status_code != 200:
r.raise_for_status()
@ -229,14 +231,14 @@ def quit_process(pid):
@timeout(7200)
def download(file, url, mak, target):
def download(url, mak, target):
global session
count = 0
start = time.time()
upd = start
#url = re.sub("tivo.lassitu.de:80", "localhost: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()
r = session.get(url, stream=True)
try:
@ -254,7 +256,9 @@ def download(file, url, mak, target):
upd = now
dur = now - start
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:
try:
p_decode.stdin.close()
@ -263,13 +267,13 @@ def download(file, url, mak, target):
pass
size = os.path.getsize(target)
if size < 1024:
print "error downloading file: too small"
logger.error("error downloading file: too small")
os.remove(target)
raise TivoException("downloaded file is too small")
elapsed = time.time() - start
throughput = size / elapsed
print "%5.3fGB transferred in %d:%02d, %.1f MB/s" % (
size/1e9, int(elapsed/3600), int(elapsed / 60) % 60, throughput/1e6)
logger.info("%5.3fGB transferred in %d:%02d, %.1f MB/s" % (
size/1e9, int(elapsed/3600), int(elapsed / 60) % 60, throughput/1e6))
def download_decode(item, mak):
@ -280,10 +284,10 @@ def download_decode(item, mak):
except OSError:
pass
if 0 & os.path.exists(target):
print " reusing existing download file"
logger.info(" reusing existing download file")
else:
try:
download(item.file, item.url, mak, target)
download(item.url, mak, target)
except Exception, e:
exc_info = sys.exc_info()
try:
@ -296,11 +300,11 @@ def download_decode(item, mak):
os.utime(target, (item.time, item.time))
#os.utime(mp4, [item.date, item.date])
except Exception, e:
print "Problem setting timestamp: ", e
logger.error("Problem setting timestamp: %" % (e))
def savetoc(dom):
fd=open("tivomirror-toc.xml", "w")
fd=open("toc.xml", "w")
fd.write(dom.toprettyxml())
fd.close()
@ -324,60 +328,70 @@ def wantitem(item, downloaddb):
def mirror(dom, downloaddb):
avail = getAvail(targetdir)
if avail < minfree:
print "%s: %.1fG available, at least %.1fG needed, stopping" % \
(targetdir, avail / gig, minfree / gig)
logger.error("%s: %.1fG available, at least %.1fG needed, stopping" % \
(targetdir, avail / gig, minfree / gig))
sys.exit(1)
items = dom.getElementsByTagName("Item")
print "*** %d shows listed" % (items.length)
logger.info("*** %d shows listed" % (items.length))
for node in items:
item = TivoItem(node)
reason = wantitem(item, downloaddb)
if (reason != ""):
print "*** skipping \"%s\": %s" % (item.name, reason)
logger.info("*** skipping \"%s\": %s" % (item.name, reason))
continue
print "*** downloading \"%s\": %.3fGB" % (item.name, item.sourcesize / 1e9)
logger.info("*** downloading \"%s\": %.3fGB" % (item.name, item.sourcesize / 1e9))
try:
download_decode(item, mak)
downloaddb[item.name] = item.datestr
if getattr(downloaddb, "sync", None) and callable(downloaddb.sync):
downloaddb.sync()
# 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
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):
items = dom.getElementsByTagName("Item")
print "*** %d shows listed" % (items.length)
logger.info("*** %d shows listed" % (items.length))
for node in items:
item = TivoItem(node)
reason = wantitem(item, downloaddb)
if (reason != ""):
print "--- %-11.11s: %s" % (reason, item.name)
logger.info("--- %-11.11s: %s" % (reason, item.name))
continue
print "*** downloading %s (%.3fGB)" % (item.name, item.sourcesize / 1e9)
logger.info("*** downloading %s (%.3fGB)" % (item.name, item.sourcesize / 1e9))
def main():
global ignoreepisodetitle
global ignoreepisodetitle, logger
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'])
for opt, arg in options:
if opt in ('-T', '--ignoreepisodetitle'):
ignoreepisodetitle = True
downloaddb = anydbm.open(os.path.expanduser("~") + "/.tivo-downloads", "c")
print "*** Getting listing"
if opt in ('-d'):
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()
savetoc(dom)
@ -390,6 +404,7 @@ def main():
mirror(dom, downloaddb)
downloaddb.close()
logger.info("*** Completed")
if __name__ == "__main__":
main()