Пример #1
0
 def insert_or_update_in_db(self):
     # if not self.isInDB:
     sql = "INSERT INTO `TrackedTorrentFiles` (`hash`, `name`, `timeout`, `torrentHash`, `torrentFileName`) VALUES (%s, %s, UNIX_TIMESTAMP()+%s, %s, %s) ON DUPLICATE KEY UPDATE timeout=VALUES(timeout);"
     DatabaseManager.Instance().cursor.execute(
         sql, (self.hash, self.name, self.timeout, self.torrent_hash,
               self.torrent_file_name))
     DatabaseManager.Instance().connector.commit()
Пример #2
0
    def execute_on_torrent_downloaded_actions(self):
        """
        Execute onTorrentDownloaded action
        """
        curs = DatabaseManager.Instance().cursor
        # "SELECT id, data FROM AutomatedActions WHERE `trigger`='onTorrentDownloaded' AND notifier='TvShowManager';"
        actions = self.actions["onTorrentDownloaded"]
        # for a in curs:
        #     actions.append(a)
        for id_, data in actions.items():
            id_ = int(id_)
            try:
                self.logger.info("try to execute action id=%d", id_)
                success = self.execute_action(data)
                self.logger.info("action (id=%d) result=%d", id_, success)
                delete = success
            except KeyError:
                self.logger.info(
                    "error while processing action (id=%d) torrent does not exist",
                    id_)
                delete = True
            finally:
                pass

            if delete:
                self.logger.info("remove action with id=%d", id_)
                delete_query = "DELETE FROM AutomatedActions WHERE id=%s;"
                curs.execute(delete_query, (id_, ))
                DatabaseManager.Instance().connector.commit()
Пример #3
0
 def set_timeout(self, timeout=2538000):
     # default timeout set to 30 days
     sql = "UPDATE `TrackedTorrentFiles` SET `timeout`=UNIX_TIMESTAMP()+ %d WHERE `name`=%s;"
     DatabaseManager.Instance().cursor.execute(sql, (
         timeout,
         self.name,
     ))
     DatabaseManager.Instance().connector.commit()
Пример #4
0
 def add_automated_actions(self, torrent_id, tv_show, episode_name):
     self.logger.debug("addAutomatedActions | new (%s, %s, %s)", torrent_id,
                       tv_show, episode_name)
     data = "&&".join(["move", torrent_id, tv_show, episode_name])
     query = "INSERT INTO `AutomatedActions` (`notifier`, `trigger`, `data`) VALUES ('TvShowManager', 'onTorrentDownloaded', %s);"
     self.logger.info("add automated action, quest=%s, data=%s", query,
                      data)
     DatabaseManager.Instance().cursor.execute(query, (data, ))
     DatabaseManager.Instance().connector.commit()
Пример #5
0
    def __init__(self, user, torrent_manager):
        """
        :type user: str
        :type torrent_manager: TomatoPy.api.torrents.TorrentManager
        :param user:
        :param torrent_manager:
        :return:
        """
        super(ReplicatorManager, self).__init__("ReplicatorManager")

        self.logger = logging.getLogger("ReplicatorManager")

        self.user = user
        self.serviceName = "Replicator"
        self.dbm = DatabaseManager.Instance()
        self.torrent_manager = torrent_manager
        self.replicator_actions = {}
        self.replicator_servers = []

        sql = "SELECT * FROM RemoteServices WHERE `ServiceName`=%s;"
        self.dbm.cursor.execute(sql, (self.serviceName, ))
        for res in self.dbm.cursor:
            self.replicator_servers.append(
                dict(name=str(res[1]), url=str(res[2])))

        # Load destinations from DB
        self.destinations = {}
        sql = "SELECT * FROM TrackedDestinations;"
        self.dbm.cursor.execute(sql)
        for res in self.dbm.cursor:
            self.destinations[str(res[0])] = str(res[1])

        self.load_remote_actions()
        self.process_replicator_actions()
        self.load_actions()
Пример #6
0
    def __init__(self):
        self.url = ""
        self.user = ""
        self.service_name = "NotificationServer"
        self.notifications = {}
        if DatabaseManager.Instance().cursor:
            sql = "SELECT * FROM RemoteServices WHERE `ServiceName`=%s LIMIT 1;"
            DatabaseManager.Instance().cursor.execute(sql,
                                                      (self.service_name, ))
            for res in DatabaseManager.Instance().cursor:
                data = str(res[2]).split("&&")
                self.user = data[0]
                self.url = data[1]
                break
        else:

            self.user = "******"
            self.url = "http://bandb.dnsd.info/cgi-bin/replicator"
Пример #7
0
 def load_actions(self, reload_=False):
     if not self.actionsLoaded or reload_:
         curs = DatabaseManager.Instance().cursor
         query = "SELECT id, `trigger`, data FROM AutomatedActions WHERE notifier=%s;"
         curs.execute(query, (self.actionNotifierName, ))
         for action in curs:
             data = str(action[2]).split("&&")
             self.actions[str(action[1])][str(action[0])] = data
         self.actionsLoaded = True
Пример #8
0
 def __init__(self, parameters=None):
     self.logger = logging.getLogger(__name__)
     if parameters is None:
         query = "SELECT parameters FROM Parameters WHERE name='TorrentManager' LIMIT 1"
         DatabaseManager.Instance().cursor.execute(query)
         (parameters_string,
          ) = DatabaseManager.Instance().cursor.fetchone()
         parameters = str(parameters_string).split("&&")
     self.download_directory = parameters[0]
     self.host = parameters[1]
     self.port = 9091
     self.user = None
     self.password = None
     if len(parameters) > 2:
         self.port = parameters[2]
     if len(parameters) > 3:
         self.user = parameters[3]
     if len(parameters) > 4:
         self.password = parameters[4]
     if len(parameters) > 5:
         self.init_with_extra_params(parameters[5:])
Пример #9
0
    def map(self, clean=False):
        """
        Build the list of files in this destination

        :type clean: bool
        :param clean: do we clean the database before
        :return:
        """
        self.files = {}
        curs = DatabaseManager.Instance().cursor
        if clean:
            curs.execute(
                "DELETE FROM DestinationsFilesList WHERE `destinationName`=%s;",
                (self.name, ))
            DatabaseManager.Instance().connector.commit()
        for root, directory, files in os.walk(self.path):

            for file_ in files:
                path = os.path.join(root, file_)
                relative_path = self.get_relative_path(path)
                if self.filter.test(File(path)):
                    fwh = None
                    if not clean:
                        sql = "SELECT * FROM DestinationsFilesList WHERE `path`=%s AND `destinationName`=%s LIMIT 1;"
                        curs.execute(sql, (relative_path, self.name))
                        res = curs.fetchone()
                        # curs.fetchall()
                        if res is not None:
                            # file already in DB, so use it
                            fwh = FileWithHash.from_sql_query(res)
                    if fwh is None:
                        fwh = FileWithHash(path, self.name, None,
                                           relative_path)
                        sql2 = "INSERT INTO DestinationsFilesList (`hash`, `path`, `destinationName`) VALUES(%s, %s, %s);"
                        # self.logger.info("%s add: %s", [self.name, relative_path]
                        curs.execute(
                            sql2,
                            (fwh.hash, relative_path, fwh.destination_name))
                        DatabaseManager.Instance().connector.commit()
                    self.files[fwh.hash] = fwh
Пример #10
0
    def grab_interesting_files(self):
        # Load old from DB
        sql = "SELECT * FROM `TrackedTorrentFiles`;"
        DatabaseManager.Instance().cursor.execute(sql)
        for res in DatabaseManager.Instance().cursor:
            interesting_file = InterestingFile.from_sql_query(res)
            if interesting_file is not None:
                self.interesting_files[
                    interesting_file.name] = interesting_file

        # Get new from torrent manager
        torrents = self.torrent_manager.get_torrents()
        for torrent in torrents:
            if torrent.is_finished:
                torrent_files = self.torrent_manager.get_torrent_files(
                    torrent.hash)
                # for each torrentFile in torrent
                for f in torrent_files:
                    file_ = File(
                        os.path.join(self.torrent_manager.download_directory,
                                     f.name))
                    # if "file" is a valid file
                    if self.filter.test(file_):
                        # if file exists
                        if os.path.exists(file_.fullPath):
                            interesting_file = InterestingFile(
                                file_.fullPath, torrent.hash, f.name)
                            interesting_file.insert_or_update_in_db(
                            )  # update timeout in any case
                            if file_.fullPath not in self.interesting_files:
                                self.interesting_files[
                                    file_.fullPath] = interesting_file
                            sql = "INSERT INTO `TrackedTorrents` (`hash`, `name`, `torrentFile`, `magnet`) VALUES (%s, %s, %s, %s) ON DUPLICATE KEY UPDATE `hash`=VALUES(hash);"
                            t = TrackedTorrent.from_torrent(torrent)
                            DatabaseManager.Instance().cursor.execute(
                                sql, (t.hash, t.name, t.torrent_file_data,
                                      t.magnet))
                            DatabaseManager.Instance().connector.commit()
Пример #11
0
    def execute_on_torrent_downloaded_actions(self):
        curs = DatabaseManager.Instance().cursor
        actions = self.actions["onTorrentDownloaded"]
        for id_, data in actions.items():
            id_ = int(id_)
            try:
                self.logger.info("try to execute action id=%d", id_)
                success = self.execute_action(data)
                self.logger.info("action (id=%s) result=%s" % (id_, success))
                delete = success
            except KeyError as e:
                self.logger.exception(
                    "error while processing action (id=%d) torrent does not exist"
                    % (id_, ))
                delete = True
            finally:
                pass

            if delete:
                self.logger.info("remove action with id=%s", id_)
                del_query = "DELETE FROM AutomatedActions WHERE id=%s;"
                curs.execute(del_query, (id_, ))
                DatabaseManager.Instance().connector.commit()
Пример #12
0
    def __init__(self):
        self.logger = logging.getLogger(__name__)
        dbm = DatabaseManager.Instance()
        query = "SELECT parameters FROM Parameters WHERE name='XbmcLibraryManager' LIMIT 1"
        dbm.cursor.execute(query)
        (parametersString, ) = dbm.cursor.fetchone()
        parameters = str(parametersString).split("&&")

        self.host = parameters[0]
        self.port = int(parameters[1])
        self.user = None
        self.pwd = None
        if len(parameters) > 3:
            self.user = parameters(2)
            self.pwd = parameters(3)
        self.pendingRequests = {}
        self.jsonrpcVersion = "2.0"
Пример #13
0
    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)
Пример #14
0
    def __init__(self, torrent_manager):
        super(TvShowManager, self).__init__("TvShowManager")

        self.logger = logging.getLogger("TvShowManager")

        dbm = DatabaseManager.Instance()
        self.torrent_manager = torrent_manager

        self.tracked_tv_shows = [
        ]  # tv shows that can be downloaded // Get them form db

        query = "SELECT parameters FROM Parameters WHERE name='TvShowManager' LIMIT 1"
        dbm.cursor.execute(query)
        (parametersString, ) = dbm.cursor.fetchone()
        parameters = str(parametersString).split("&&")
        self.beta_user = parameters[1]
        self.tv_show_directory = "" + parameters[0]
        self.file_system_encoding = None
        if len(parameters) > 2:
            self.file_system_encoding = parameters[2]

        query = "SELECT title, filter, authorFilter, sizeLimits, episodeProviderSearchString, preferredTorrentProvider FROM TrackedTvShows;"
        dbm.cursor.execute(query)

        for (title, name_filter, author_filter, size_limits, search_string,
             preferred_torrent_provider) in dbm.cursor:
            title = str(title)
            name_filter = str(name_filter)
            author_filter = str(author_filter)
            size_limits = str(size_limits)
            search_string = str(search_string)
            preferred_torrent_provider = str(preferred_torrent_provider)

            sizes = {}
            size_limits = size_limits.split(":")
            if len(size_limits[0]) > 0:
                sizes["gt"] = int(size_limits[0])
            if len(size_limits) > 1:
                if len(size_limits[1]) > 0:
                    sizes["lt"] = int(size_limits[1])
            filter_ = TorrentFilter(name_filter.split(":"), author_filter,
                                    sizes)
            self.tracked_tv_shows.append(
                TrackedTvShow(title, filter_, search_string,
                              preferred_torrent_provider))
        dbm.connector.commit()

        # TODO: Change
        self.registered_episode_providers = [
            BetaserieRSSScrapper(self.beta_user)
        ]
        self.registered_torrent_providers = {
            "TPB": TPBScrapper(),
            "TPB-Old": TPBOldScrapper()
        }

        self.directory_mapper = DirectoryMapper(self.tv_show_directory,
                                                r"(.*)\.(mkv|avi|mp4|wmv)$",
                                                self.file_system_encoding)

        self.load_actions()