def __init__(self): self.logger = logging.getLogger("FileTracer ") # create useful objects self.dbm = DatabaseManager.Instance() self.torrent_manager = TransmissionTorrentRPC() # Load parameters # Load destinations self.destinations = [] self.dbm.cursor.execute("SELECT * FROM `TrackedDestinations`;") for res in self.dbm.cursor: d = Destination.from_sql_query(res) if d is not None: self.destinations.append(d) # init DoneTorrentFilter self.dtf = DoneTorrentFilter(self.torrent_manager)
class FileTracer: def __init__(self): self.logger = logging.getLogger("FileTracer ") # create useful objects self.dbm = DatabaseManager.Instance() self.torrent_manager = TransmissionTorrentRPC() # Load parameters # Load destinations self.destinations = [] self.dbm.cursor.execute("SELECT * FROM `TrackedDestinations`;") for res in self.dbm.cursor: d = Destination.from_sql_query(res) if d is not None: self.destinations.append(d) # init DoneTorrentFilter self.dtf = DoneTorrentFilter(self.torrent_manager) def run(self): # get interesting files self.dtf.grab_interesting_files() if len(self.dtf.interesting_files) > 0: # Map all sources for d in self.destinations: d.map() d.look_for_interesting_files(self.dtf.interesting_files) self.dbm.connector.commit() # For each destinations for destination in self.destinations: # For each tuple (trackedFile, destinationFile) in interestingFiles for trackedFile, destinationFile in destination.validInterestingFiles: sql = "SELECT * FROM `TrackedTorrents` WHERE `hash`=%s LIMIT 1;" self.dbm.cursor.execute(sql, (trackedFile.torrent_hash,)) res = self.dbm.cursor.fetchone() # If torrent is in TrackedTorrents DB if res is not None: tt = TrackedTorrent.from_sql_query(res) if tt is not None: sql = "SELECT count(1) FROM ReplicatorActions WHERE `torrentName`=%s AND `torrentFileName`=%s LIMIT 1;" self.dbm.cursor.execute(sql, (tt.name, trackedFile.torrent_file_name)) if not self.dbm.cursor.fetchone()[0]: self.logger.info("New replicator action with file: %s", trackedFile.torrent_file_name) sql = "INSERT INTO `ReplicatorActions` (torrentName, torrentFileName, torrentData, destinationName, destinationRelativePath) VALUES (%s, %s, %s, %s, %s) ON DUPLICATE KEY UPDATE torrentFileName=torrentFileName;" self.dbm.cursor.execute( sql, ( tt.name, trackedFile.torrent_file_name, tt.magnet, destination.name, destinationFile.relativePath, ), ) self.dbm.connector.commit() else: self.logger.warn("This action already exists in the database.") # Remove File from TrackedTorrentFiles DB # self.logger.info("Remove TrackedTorrentFile %s", trackedFile.name) # sql = "DELETE FROM `TrackedTorrentFiles` WHERE `hash`=%s;" # self.dbm.cursor.execute(sql, (trackedFile.hash, )) # self.dbm.connector.commit() else: self.logger.error("Unable to create TrackedTorrent with query %s", res) else: self.logger.error("res is None for hash=%s", trackedFile.torrent_hash) def clean(self): # self.dbm.cursor.execute("DELETE FROM TrackedTorrentFiles WHERE timeout<UNIX_TIMESTAMP()") self.logger.debug("Beginning Clean up.") torrents = {} for torrent in self.torrent_manager.get_torrents(): torrents[torrent.hash] = 1 delete_tt_sql = "DELETE FROM `TrackedTorrents` WHERE `hash`=%s;" get_ttf_with_torrent_hash_sql = "SELECT COUNT(1) FROM `TrackedTorrentFiles` WHERE `torrentHash`=%s LIMIT 1;" for iF in self.dtf.interesting_files.itervalues(): # Clean up TrackedTorrentFiles DB delete = False # Remove if file does not exist (deleted, moved) if not os.path.exists(iF.name) and iF.torrent_hash not in torrents: delete = True # Remove if associated torrent does not exists # if not (iF.torrentHash in torrents): # delete = True if delete: self.logger.info("Remove TrackedTorrentFile %s", iF.name) self.dbm.cursor.execute("DELETE FROM `TrackedTorrentFiles` WHERE `name`=%s", (iF.name,)) self.dbm.connector.commit() # Clean up TrackedTorrents DB sql = "SELECT `hash` FROM `TrackedTorrents`;" self.dbm.cursor.execute(sql) tracked_torrents = [] for res in self.dbm.cursor: tracked_torrents.append(res[0]) for hashStr in tracked_torrents: # No TrackedTorrentFile associated with this TrackedTorrent => remove self.dbm.cursor.execute(get_ttf_with_torrent_hash_sql, (hashStr,)) if not self.dbm.cursor.fetchone()[0]: self.logger.info("Remove TrackedTorrent with hash=%s", hashStr) self.dbm.cursor.execute(delete_tt_sql, (hashStr,)) self.dbm.connector.commit() self.logger.debug("End Clean up.")
class FileTracer: def __init__(self): self.logger = logging.getLogger("FileTracer ") # create useful objects self.dbm = DatabaseManager.Instance() self.torrent_manager = TransmissionTorrentRPC() # Load parameters # Load destinations self.destinations = [] self.dbm.cursor.execute("SELECT * FROM `TrackedDestinations`;") for res in self.dbm.cursor: d = Destination.from_sql_query(res) if d is not None: self.destinations.append(d) # init DoneTorrentFilter self.dtf = DoneTorrentFilter(self.torrent_manager) def run(self): # get interesting files self.dtf.grab_interesting_files() if len(self.dtf.interesting_files) > 0: # Map all sources for d in self.destinations: d.map() d.look_for_interesting_files(self.dtf.interesting_files) self.dbm.connector.commit() # For each destinations for destination in self.destinations: # For each tuple (tracked_file, destinationFile) in interestingFiles for tracked_file, destinationFile in destination.valid_interesting_files: sql = "SELECT * FROM `TrackedTorrents` WHERE `hash`=%s LIMIT 1;" self.dbm.cursor.execute(sql, (tracked_file.torrent_hash, )) res = self.dbm.cursor.fetchone() # If torrent is in TrackedTorrents DB if res is not None: tt = TrackedTorrent.from_sql_query(res) if tt is not None: sql = "SELECT count(1) FROM ReplicatorActions WHERE `torrentName`=%s AND `torrentFileName`=%s LIMIT 1;" self.dbm.cursor.execute( sql, (tt.name, tracked_file.torrent_file_name)) if not self.dbm.cursor.fetchone()[0]: self.logger.info( "New replicator action with file: %s", tracked_file.torrent_file_name) sql = "INSERT INTO `ReplicatorActions` (torrentName, torrentFileName, torrentData, destinationName, destinationRelativePath) VALUES (%s, %s, %s, %s, %s) ON DUPLICATE KEY UPDATE torrentFileName=torrentFileName;" self.dbm.cursor.execute( sql, (tt.name, tracked_file.torrent_file_name, tt.magnet, destination.name, destinationFile.relativePath)) self.dbm.connector.commit() else: self.logger.debug( "This action already exists in the database.") # Remove File from TrackedTorrentFiles DB # self.logger.info("Remove TrackedTorrentFile %s", tracked_file.name) # sql = "DELETE FROM `TrackedTorrentFiles` WHERE `hash`=%s;" # self.dbm.cursor.execute(sql, (tracked_file.hash, )) # self.dbm.connector.commit() else: self.logger.error( "Unable to create TrackedTorrent with query %s", res) else: self.logger.error("res is None for hash=%s", tracked_file.torrent_hash) def clean(self): # self.dbm.cursor.execute("DELETE FROM TrackedTorrentFiles WHERE timeout<UNIX_TIMESTAMP()") self.logger.debug("Beginning Clean up.") torrents = {} for torrent in self.torrent_manager.get_torrents(): torrents[torrent.hash] = 1 delete_tt_sql = "DELETE FROM `TrackedTorrents` WHERE `hash`=%s;" get_ttf_with_torrent_hash_sql = "SELECT COUNT(1) FROM `TrackedTorrentFiles` WHERE `torrentHash`=%s LIMIT 1;" for iF in self.dtf.interesting_files.values(): # Clean up TrackedTorrentFiles DB delete = False # Remove if file does not exist (deleted, moved) if not os.path.exists(iF.name) and iF.torrent_hash not in torrents: delete = True # Remove if associated torrent does not exists # if not (iF.torrentHash in torrents): # delete = True if delete: self.logger.info("Remove TrackedTorrentFile %s", iF.name) self.dbm.cursor.execute( "DELETE FROM `TrackedTorrentFiles` WHERE `name`=%s", (iF.name, )) self.dbm.connector.commit() # Clean up TrackedTorrents DB sql = "SELECT `hash` FROM `TrackedTorrents`;" self.dbm.cursor.execute(sql) tracked_torrents = [] for res in self.dbm.cursor: tracked_torrents.append(str(res[0])) for hashStr in tracked_torrents: # No TrackedTorrentFile associated with this TrackedTorrent => remove self.dbm.cursor.execute(get_ttf_with_torrent_hash_sql, (hashStr, )) if not self.dbm.cursor.fetchone()[0]: self.logger.info("Remove TrackedTorrent with hash=%s", hashStr) self.dbm.cursor.execute(delete_tt_sql, (hashStr, )) self.dbm.connector.commit() self.logger.debug("End Clean up.")