示例#1
0
    def _launch_torrent(self, data):
        """
        Given torrent data adds torrent to current libtorrent session
        :param data: dict
        :return:
        """

        # if data["status"] == "pause":
        #     log.debug("Torrent %s is on pause. Skipping" % data["infohash"])
        #     return

        add_torrent_params = None
        log.debug("Launching torrent %s" % data["infohash"])
        if self.is_torrent_file_exist(data["infohash"]):
            log.debug("Loading info from torrent file")
            lt_info = lt.torrent_info(
                os.path.join(self.torrent_metadata_path,
                             ("%s.torrent" % data["infohash"])))
            add_torrent_params = lt.add_torrent_params()
            add_torrent_params.ti = lt_info
        else:
            log.debug("Adding torrent with infohash only")
            add_torrent_params = lt.parse_magnet_uri(data["magnet"])
        add_torrent_params.save_path = data["torrent_directory"]
        infohash = add_torrent_params.info_hash
        handle = self.session.find_torrent(infohash)
        if handle.is_valid():
            log.debug("Torrent already present in session: %s" %
                      data["infohash"])
            return
        self._add_torrent(add_torrent_params)
示例#2
0
 def get_save_path(self, infohash):
     info = self.get_torrent_info(infohash)
     lt_info = lt.parse_magnet_uri(info["magnet"])
     handle = self.session.find_torrent(lt_info.info_hash)
     if handle.is_valid():
         status = handle.status()
         log.debug("LT save path %s" % status.save_path)
         log.debug("save path %s" % info["torrent_directory"])
         return status.save_path
示例#3
0
    def download_torrent(self,
                         magnet,
                         on_complete,
                         on_start_download,
                         on_timeout,
                         on_update=None,
                         abort_ev=None):
        """
        Downloads torrent by given magnet URI and calls callbacks when download process changes state
        This method is created to be used in the interactive forms, when user will wait for download to complete
        Callbacks are used to let the user know about download state changes and any appearing problems.
        :param magnet: Magnet URI
        :param on_complete: callback when download is 100% completed
        :param on_start_download: callback, once metadata is obtained and download started
        :param on_timeout: callback in the event of timeout. It will be called if average download speed
                    falls bellow the threshold
        :param on_update: callback called on every libtorrent status poll. By default it happens every second.
        :param abort_ev: if true - download will return immediately
        :return: none
        """
        # Parse magnet
        log.debug("Torrent download request on timeout: %s abort_ev: %s" %
                  (str(on_timeout), str(abort_ev)))
        add_torrent_params = lt.parse_magnet_uri(magnet)

        # extract infohash
        infohash = add_torrent_params.info_hash
        handle = self.session.find_torrent(infohash)
        if handle.is_valid():
            log.debug("Torrent exists in the current session")
            self._download_existing_torrent(handle, infohash, on_complete,
                                            on_start_download, on_update,
                                            abort_ev, on_timeout)
            return
        log.debug("Adding torrent %s" % magnet)
        t_path = os.path.join(self.torrents_path, str(infohash))
        if not os.path.exists(t_path):
            os.mkdir(t_path)
        add_torrent_params.save_path = t_path
        self.save_torrent_info(add_torrent_params, magnet, "active")
        handle = self._add_torrent(add_torrent_params)
        self.await_download_start(handle=handle,
                                  on_download_started=on_start_download,
                                  abort_ev=abort_ev,
                                  on_timeout=on_timeout)
        self.await_download_completion(handle=handle,
                                       on_complete=on_complete,
                                       on_update=on_update,
                                       abort_ev=abort_ev,
                                       on_timeout=on_timeout)
示例#4
0
 def begin_torrent_download(self, magnet):
     add_torrent_params = lt.parse_magnet_uri(magnet)
     handle = self.session.find_torrent(add_torrent_params.info_hash)
     if handle.is_valid():
         log.debug("Torrent already exists: %s" %
                   add_torrent_params.info_hash)
         return
     t_path = os.path.join(self.torrents_path,
                           str(add_torrent_params.info_hash))
     if not os.path.exists(t_path):
         os.mkdir(t_path)
     add_torrent_params.save_path = t_path
     self.save_torrent_info(add_torrent_params, magnet, "active")
     self._add_torrent(add_torrent_params)
示例#5
0
 def resume_torrent(self, infohash):
     log.debug("Pausing torrent %s" % infohash)
     info = self.get_torrent_info(infohash)
     lt_info = lt.parse_magnet_uri(info["magnet"])
     handle = self.session.find_torrent(lt_info.info_hash)
     if handle.is_valid():
         handle.resume()
         ti = TorrentInfo.from_dict(self.get_torrent_info(infohash))
         ti["status"] = "active"
         self._write_torrent_info_data(
             self._get_torrent_info_path(infohash), ti.dump())
     else:
         log.exception("Error pausing torrent: handle is invalid! %s " %
                       get_stack())
示例#6
0
 def save_torrent_info(self,
                       at_params,
                       magnet,
                       status="active",
                       save_path=None):
     """
     Saves torrent info in JSON file.
     If file already exists - it will be overwritten
     :param at_params: libtorrent add_torrent_params object
     :param magnet: str Magnet link
     :param status: status to assign to torrent
     :param save_path: Save path
     :return:
     """
     log.debug("Saving torrent info %s" % magnet)
     ti = lt.parse_magnet_uri(magnet)
     #infohash = str(at_params.ti.info_hash)
     infohash = str(ti.info_hash)
     torrent_dir = None
     torrent_path = None
     if save_path:
         torrent_path = save_path
         torrent_dir = os.path.dirname(save_path)
     else:
         torrent_dir = os.path.join(self.torrents_path, infohash)
         torrent_path = os.path.join(torrent_dir, at_params.name)
     filepath = os.path.join(self.torrent_metadata_path,
                             ("%s.json" % infohash))
     log.debug("Initializing TorrentInfo object %s " % magnet)
     info = TorrentInfo.from_dict({
         "infohash": infohash,
         "magnet": magnet,
         "torrent_directory": torrent_dir,
         "name": at_params.name,
         "full_path": torrent_path,
         "status": status
     })
     log.debug("Done. Writing to file...")
     self._write_torrent_info_data(filepath, info.dump())
     log.debug("Torrent info saved")
示例#7
0
 def delete_torrent(self, infohash, with_files=False):
     log.debug("Deleting torrent %s" % str(with_files))
     info = self.get_torrent_info(infohash)
     t_info = lt.parse_magnet_uri(info["magnet"])
     handle = self.session.find_torrent(t_info.info_hash)
     if handle.is_valid():
         self.session.remove_torrent(handle, with_files)
     info_path = self._get_torrent_info_path(infohash)
     torrent_file_path = os.path.join(self.torrent_metadata_path,
                                      "%s.torrent" % infohash)
     files_path = os.path.join(self.torrents_path, infohash)
     time.sleep(.7)
     if os.path.exists(info_path):
         os.remove(info_path)
     if os.path.exists(torrent_file_path):
         os.remove(torrent_file_path)
     if os.path.isdir(files_path) and with_files:
         shutil.rmtree(files_path)
     if os.path.exists(info["full_path"]) and with_files:
         if os.path.isdir(info["full_path"]):
             shutil.rmtree(info["full_path"])
         else:
             os.remove(info["full_path"])
     log.debug("Delete torrent successful")