Beispiel #1
0
    def auto_update_torrent_records(self):
        with self.app.app_context():
            while True:
                self.lock.acquire()
                try:
                    for t in self.lt_session.get_torrents():
                        s = t.status()
                        Hash = str(s.info_hash).lower()
                        torrent = Torrent.find_by_hash(Hash)
                        if not torrent: continue

                        torrent.update_to_db(
                            Hash,
                            dict(download_speed=s.download_rate,
                                 downloaded_bytes=s.total_wanted_done,
                                 is_finished=t.is_finished(),
                                 is_paused=s.paused,
                                 name=t.name() if t.name() else "Unknown",
                                 num_connections=s.num_connections,
                                 num_peers=s.num_peers,
                                 num_seeds=s.num_seeds,
                                 num_trackers=len(t.trackers()),
                                 progress=int(s.progress * 100),
                                 queue_position=t.queue_position(),
                                 total_bytes=s.total_wanted,
                                 upload_speed=s.upload_rate))

                        if t.is_seed():
                            torrent.update_to_db(
                                Hash,
                                dict(download_speed=0,
                                     is_paused=True,
                                     num_connections=0,
                                     num_peers=0,
                                     num_seeds=0,
                                     num_trackers=0,
                                     upload_speed=0))
                            self.lt_session.remove_torrent(t)
                            del torrent
                except Exception as e:
                    print("Error from auto update thread:", e)

                time.sleep(2)
                self.lock.release()
Beispiel #2
0
    def add_magnet(self, magnet, username=None, save_path=None):
        save_path = save_path or self.default_save_path
        magnet = urllib.parse.unquote(magnet)
        Hash = self.get_info_hash(magnet)

        if not Hash: return
        if Torrent.find_by_hash(Hash):
            return Hash

        save_path = os.path.join(save_path, username, Hash)
        os.makedirs(save_path, exist_ok=True)

        t = self.add_torrent(magnet, save_path)
        if not t:
            self.remove_path(save_path)
            return

        torrent = Torrent(added_time=int(time.time()),
                          download_path=save_path,
                          download_speed=0,
                          downloaded_bytes=0,
                          Hash=Hash,
                          magnet=magnet,
                          username=username,
                          is_finished=False,
                          is_paused=False,
                          name="Unknown",
                          num_connections=0,
                          num_peers=0,
                          num_seeds=0,
                          num_trackers=0,
                          progress=0,
                          queue_position=-1,
                          total_bytes=0,
                          upload_speed=0)
        torrent.save_to_db()

        return Hash