Add download history logging

Logs downloads as to not make duplicate files
This commit is contained in:
Jack 2016-01-11 18:01:46 -05:00
parent 42c26102d8
commit 12cd06cae5

View file

@ -1,3 +1,4 @@
import sqlite3
from lbrynet.lbryfile.StreamDescriptor import LBRYFileStreamType from lbrynet.lbryfile.StreamDescriptor import LBRYFileStreamType
from lbrynet.lbryfile.client.LBRYFileDownloader import LBRYFileSaverFactory, LBRYFileOpenerFactory from lbrynet.lbryfile.client.LBRYFileDownloader import LBRYFileSaverFactory, LBRYFileOpenerFactory
from lbrynet.lbryfile.client.LBRYFileOptions import add_lbry_file_to_sd_identifier from lbrynet.lbryfile.client.LBRYFileOptions import add_lbry_file_to_sd_identifier
@ -20,6 +21,20 @@ import sys
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
class DummyDownloader(object):
def __init__(self, directory, file_name):
self.download_directory = directory
self.file_name = file_name
class DummyStream(object):
def __init__(self, row):
download_directory = os.path.join(*row[2].split('/')[:-1])
file_name = row[2].split('/')[len(row[2].split('/')) - 1]
self.stream_hash = row[0]
self.downloader = DummyDownloader(download_directory, file_name)
self.is_dummy = True
class LBRYDaemon(xmlrpc.XMLRPC): class LBRYDaemon(xmlrpc.XMLRPC):
""" """
@ -282,23 +297,28 @@ class LBRYDaemon(xmlrpc.XMLRPC):
self.sd_identifier.add_stream_downloader_factory(LBRYFileStreamType, downloader_factory) self.sd_identifier.add_stream_downloader_factory(LBRYFileStreamType, downloader_factory)
return defer.succeed(True) return defer.succeed(True)
def _download_name(self, name): def _download_name(self, history, name):
def _disp(stream): def _disp(stream):
print '[' + str(datetime.now()) + ']' + ' Downloading: ' + str(stream.stream_hash) print '[' + str(datetime.now()) + ']' + ' Downloading: ' + str(stream.stream_hash)
log.debug('[' + str(datetime.now()) + ']' + ' Downloading: ' + str(stream.stream_hash)) log.debug('[' + str(datetime.now()) + ']' + ' Downloading: ' + str(stream.stream_hash))
return defer.succeed(None) return defer.succeed(None)
stream = GetStream(self.sd_identifier, self.session, self.session.wallet, self.lbry_file_manager, if not history:
max_key_fee=self.max_key_fee, data_rate=self.data_rate) stream = GetStream(self.sd_identifier, self.session, self.session.wallet, self.lbry_file_manager,
self.downloads.append(stream) max_key_fee=self.max_key_fee, data_rate=self.data_rate)
d = self.session.wallet.get_stream_info_for_name(name) self.downloads.append(stream)
d.addCallback(lambda stream_info: stream.start(stream_info))
d.addCallback(lambda _: _disp(stream))
d.addCallback(lambda _: {'ts': datetime.now(),'name': name})
d.addErrback(lambda err: str(err.getTraceback()))
return d d = self.session.wallet.get_stream_info_for_name(name)
d.addCallback(lambda stream_info: stream.start(stream_info))
d.addCallback(lambda _: _disp(stream))
d.addCallback(lambda _: {'ts': datetime.now(),'name': name})
d.addErrback(lambda err: str(err.getTraceback()))
return d
else:
self.downloads.append(DummyStream(history[0]))
return defer.succeed(None)
def _path_from_name(self, name): def _path_from_name(self, name):
d = self.session.wallet.get_stream_info_for_name(name) d = self.session.wallet.get_stream_info_for_name(name)
@ -327,6 +347,57 @@ class LBRYDaemon(xmlrpc.XMLRPC):
d.callback(None) d.callback(None)
return d return d
def _check_history(self, name, metadata):
con = sqlite3.connect(os.path.join(self.db_dir, 'daemon.sqlite'))
cur = con.cursor()
query = "create table if not exists history \
(stream_hash char(96) primary key not null,\
uri text not null, \
path text not null);"
cur.execute(query)
con.commit()
r = cur.execute("select * from history where stream_hash='" + metadata['stream_hash'] + "'")
files = r.fetchall()
if files:
if not os.path.isfile(files[0][2]):
print "Couldn't find", files[0][2], ", trying to redownload it"
cur.execute("delete from history where stream_hash='" + files[0][0] + "'")
con.commit()
con.close()
return []
else:
con.close()
return files
else:
con.close()
return files
def _add_to_history(self, name, path):
con = sqlite3.connect(os.path.join(self.db_dir, 'daemon.sqlite'))
cur = con.cursor()
query = "create table if not exists history \
(stream_hash char(96) primary key not null,\
uri text not null, \
path text not null);"
cur.execute(query)
con.commit()
r = cur.execute("select * from history where stream_hash='" + path['stream_hash'] + "'")
files = r.fetchall()
if not files:
vals = path['stream_hash'], name, path['path']
r = cur.execute("insert into history values (?, ?, ?)", vals)
con.commit()
else:
print 'Already downloaded', path['stream_hash'], '->', path['path']
con.close()
return path['path']
def xmlrpc_get_settings(self): def xmlrpc_get_settings(self):
""" """
Get LBRY payment settings Get LBRY payment settings
@ -447,7 +518,7 @@ class LBRYDaemon(xmlrpc.XMLRPC):
return defer.succeed(None) return defer.succeed(None)
stream = GetStream(self.sd_identifier, self.session, self.session.wallet, self.lbry_file_manager, stream = GetStream(self.sd_identifier, self.session, self.session.wallet, self.lbry_file_manager,
max_key_fee=self.max_key_fee, data_rate=self.data_rate) max_key_fee=self.max_key_fee, data_rate=self.data_rate)
self.downloads.append(stream) self.downloads.append(stream)
@ -456,7 +527,6 @@ class LBRYDaemon(xmlrpc.XMLRPC):
d.addCallback(lambda _: _disp(stream)) d.addCallback(lambda _: _disp(stream))
d.addCallback(lambda _: {'ts': datetime.now(),'name': name}) d.addCallback(lambda _: {'ts': datetime.now(),'name': name})
d.addErrback(lambda err: str(err.getTraceback())) d.addErrback(lambda err: str(err.getTraceback()))
return d return d
def xmlrpc_path_from_name(self, name): def xmlrpc_path_from_name(self, name):
@ -484,10 +554,12 @@ class LBRYDaemon(xmlrpc.XMLRPC):
@return: {stream_hash, path}: @return: {stream_hash, path}:
""" """
d = defer.Deferred(None) d = self._resolve_name(name)
d.addCallback(lambda _: self._download_name(name)) d.addCallback(lambda metadata: self._check_history(name, metadata))
d.addCallback(lambda hist: self._download_name(hist, name))
d.addCallback(lambda _: self._path_from_name(name))
d.addCallback(lambda path: self._add_to_history(name, path))
d.addCallback(lambda _: self._path_from_name(name)) d.addCallback(lambda _: self._path_from_name(name))
d.callback(None)
return d return d