Ejemplo n.º 1
0
 def list_torrents(self, hashes=None, username=None):
     if hashes:
         return [
             self.torrent_status(Hash, username) for Hash in hashes
             if self.torrent_status(Hash, username)
         ]
     return [t.JSON for t in Torrent.find_by_username(username)][::-1]
Ejemplo n.º 2
0
    def get(self):
        Hash = request.args.get('hash', None)
        if not Hash:
            return JU.make_response("parameter '?hash=' required", 400)

        torrent = Torrent.find_by_hash_and_username(
            Hash, username=get_jwt_identity())
        if not torrent: return JU.make_response("torrent not found", 404)

        structure = fs.json_tree(torrent.download_path)
        if not structure: return JU.make_response("download path error", 404)

        return make_response(structure, 200)
Ejemplo n.º 3
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
Ejemplo n.º 4
0
 def remove_torrent(self, Hash, username=None):
     torrent = Torrent.find_by_hash_and_username(Hash, username)
     if not torrent: return
     try:
         for t in self.lt_session.get_torrents():
             s = t.status()
             info_hash = str(s.info_hash).lower()
             if info_hash == Hash:
                 self.lt_session.remove_torrent(t)
         self.remove_path(torrent.download_path)
         torrent.delete_from_db()
         return True
     except Exception as e:
         print(e)
Ejemplo n.º 5
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()
Ejemplo n.º 6
0
 def torrent_status(self, Hash, username):
     torrent = Torrent.find_by_hash_and_username(Hash, username)
     return torrent.JSON if torrent else None