예제 #1
0
def update_db_torrent(db, cur, hash, torrent, id=None, torcache=None, quiet=False):
    global failed
    real_hash = hashlib.sha1(utils.bencode(torrent[b'info'])).hexdigest()
    if real_hash == hash:
        try:
            if not quiet:
                print("\rdoing %s" % hash)
            infos = get_torrent_info(db, hash, torrent)
            if not infos:
                return
            (name, created, files_nb, size, description) = infos
            if torcache is not None:
                cur.execute("UPDATE torrents SET name=%s, description=%s, size=%s, files_count=%s, created_at=%s, visible_status=0, torcache=%s WHERE hash=%s", (name, description, size, files_nb, time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime(created)), torcache, hash))
            else:
                cur.execute("UPDATE torrents SET name=%s, description=%s, size=%s, files_count=%s, created_at=%s, visible_status=0 WHERE hash=%s", (name, description, size, files_nb, time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime(created)), hash))
            if not quiet:
                print("\r  done %s" % name)
            return True
        except (KeyError, UnicodeDecodeError, LookupError, ValueError, TypeError) as e:
            print("\r: %r" % e)
            failed.add(hash)
            return False
    else:
        try:
            cur.execute("UPDATE torrents SET hash=%s WHERE hash=%s", (real_hash, hash))
        except MySQLdb.IntegrityError:
            cur.execute("DELETE FROM torrents WHERE hash=%s", (hash,))
        os.rename("%s/%s.torrent" % (config.torrents_dir, hash), "%s/%s.torrent" % (config.torrents_dir, real_hash))
예제 #2
0
 def extended_handshake(self, s):
     if not self._peer_extended[s]:
         raise ValueError("Peer does not support extension protocol")
     pl = bencode({'m':{'ut_metadata': self._am_metadata}})
     msg=struct.pack("!IBB", 1 + 1 + len(pl), 20, 0)
     msg+=pl
     s.send(msg)
예제 #3
0
def update_db_torrent(db, cur, hash, torrent, id=None, torcache=None, quiet=False):
    global failed
    real_hash = hashlib.sha1(utils.bencode(torrent[b'info'])).hexdigest()
    if real_hash == hash:
        try:
            if not quiet:
                print("\rdoing %s" % hash)
            infos = get_torrent_info(db, hash, torrent)
            if not infos:
                return
            (name, created, files_nb, size, description) = infos
            if not config.generate_description:
                description = None
            if torcache is not None:
                cur.execute("UPDATE torrents SET name=%s, description=%s, size=%s, files_count=%s, created_at=%s, visible_status=0, torcache=%s WHERE hash=%s", (name, description, size, files_nb, time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime(created)), torcache, hash))
            else:
                cur.execute("UPDATE torrents SET name=%s, description=%s, size=%s, files_count=%s, created_at=%s, visible_status=0 WHERE hash=%s", (name, description, size, files_nb, time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime(created)), hash))
            if not quiet:
                print("\r  done %s" % name)
            return True
        except (KeyError, UnicodeDecodeError, LookupError, ValueError, TypeError) as e:
            print("\r: %r" % e)
            failed.add(hash)
            return False
    else:
        try:
            cur.execute("UPDATE torrents SET hash=%s WHERE hash=%s", (real_hash, hash))
        except MySQLdb.IntegrityError:
            cur.execute("DELETE FROM torrents WHERE hash=%s", (hash,))
        os.rename("%s/%s.torrent" % (config.torrents_dir, hash), "%s/%s.torrent" % (config.torrents_dir, real_hash))
예제 #4
0
 def extended_handshake(self, s):
     if not self._peer_extended[s]:
         raise ValueError("Peer does not support extension protocol")
     pl = bencode({'m': {'ut_metadata': self._am_metadata}})
     msg = struct.pack("!IBB", 1 + 1 + len(pl), 20, 0)
     msg += pl
     s.send(msg)
예제 #5
0
 def metadata_reject(self, s, piece):
     if not self._peer_metadata[s]:
         raise ValueError("peer does not support metadata extension")
     if self._am_choking[s]:
         raise ValueError("chocked")
     pl = bencode({'msg_type': 2, 'piece': piece})
     msg=struct.pack("!IBB", 1 + 1 + len(pl), 20, self._peer_metadata[s])
     msg+=pl
     s.send(msg)
예제 #6
0
 def metadata_reject(self, s, piece):
     if not self._peer_metadata[s]:
         raise ValueError("peer does not support metadata extension")
     if self._am_choking[s]:
         raise ValueError("chocked")
     pl = bencode({'msg_type': 2, 'piece': piece})
     msg = struct.pack("!IBB", 1 + 1 + len(pl), 20, self._peer_metadata[s])
     msg += pl
     s.send(msg)
예제 #7
0
 def metadata_data(self, s, piece):
     if not self._peer_metadata[s]:
         raise ValueError("peer does not support metadata extension")
     if self._am_choking[s]:
         raise ValueError("chocked")
     pl = bencode({'msg_type': 1, 'piece': piece, 'total_size': len(self._metadata_pieces[self._socket_hash[s]][piece])})
     pl += self._metadata_pieces[self._socket_hash[s]][piece]
     msg=struct.pack("!IBB", 1 + 1 + len(pl), 20, self._peer_metadata[s])
     msg+=pl
     s.send(msg)
예제 #8
0
def get_new_torrent():
    files = os.listdir(config.torrents_new)
    if not files:
        return
    progress = progressbar.ProgressBar(widgets=widget("new torrents"))
    for f in progress(files):
        f = "%s/%s" % (config.torrents_new, f)
        try:
            torrent = utils.bdecode(open(f, 'rb').read())    
            real_hash = hashlib.sha1(utils.bencode(torrent[b'info'])).hexdigest()
            hash_to_ignore.add(real_hash.decode("hex"))
            os.rename(f, "%s/%s.torrent" % (config.torrents_dir, real_hash))
        except utils.BcodeError as e:
            pass
예제 #9
0
def get_new_torrent():
    files = os.listdir(config.torrents_new)
    if not files:
        return
    progress = progressbar.ProgressBar(widgets=widget("new torrents"))
    for f in progress(files):
        f = "%s/%s" % (config.torrents_new, f)
        try:
            torrent = utils.bdecode(open(f, 'rb').read())    
            real_hash = hashlib.sha1(utils.bencode(torrent[b'info'])).hexdigest()
            hash_to_ignore.add(real_hash.decode("hex"))
            os.rename(f, "%s/%s.torrent" % (config.torrents_dir, real_hash))
        except utils.BcodeError as e:
            pass
예제 #10
0
 def metadata_data(self, s, piece):
     if not self._peer_metadata[s]:
         raise ValueError("peer does not support metadata extension")
     if self._am_choking[s]:
         raise ValueError("chocked")
     pl = bencode({
         'msg_type':
         1,
         'piece':
         piece,
         'total_size':
         len(self._metadata_pieces[self._socket_hash[s]][piece])
     })
     pl += self._metadata_pieces[self._socket_hash[s]][piece]
     msg = struct.pack("!IBB", 1 + 1 + len(pl), 20, self._peer_metadata[s])
     msg += pl
     s.send(msg)
예제 #11
0
def compact_torrent(db=None):
    """SUpprime les info des block du torrent"""
    if db is None:
        db = MySQLdb.connect(**config.mysql)
    files = os.listdir(config.torrents_done)
    hashs = [h[:-8] for h in files if h.endswith(".torrent")]
    cur = db.cursor()
    i = 0
    step = 500
    archive_path = "%s/%s/" % (config.torrents_archive,
                               time.strftime("%Y-%m-%d"))
    try:
        os.mkdir(archive_path)
    except OSError as e:
        if e.errno != 17:  # file exist
            raise
    count = len(hashs)
    if count <= 0:
        return
    pbar = progressbar.ProgressBar(widgets=widget("files archived"),
                                   maxval=count).start()
    while hashs[i:i + step]:
        query = "SELECT hash, torcache FROM torrents WHERE created_at IS NOT NULL AND (%s)" % " OR ".join(
            "hash=%s" for hash in hashs[i:i + step])
        cur.execute(query, tuple(hashs[i:i + step]))
        db_hashs = dict((r[0], r[1]) for r in cur)
        c = i
        for hash, torcache in db_hashs.items():
            if config.compact_archived_torrents and torcache == 1:
                torrent = get_torrent(db, hash, base_path=config.torrents_done)
                torrent[b'info'][b'pieces'] = b''
                with open("%s%s.torrent" % (archive_path, hash), 'wb+') as f:
                    f.write(utils.bencode(torrent))
                os.remove("%s/%s.torrent" % (config.torrents_done, hash))
            else:
                os.rename("%s/%s.torrent" % (config.torrents_done, hash),
                          "%s%s.torrent" % (archive_path, hash))
            c += 1
            pbar.update(c)
        i = min(i + step, count)
        pbar.update(i)
    pbar.finish()
예제 #12
0
def compact_torrent(db=None):
    """SUpprime les info des block du torrent"""
    if db is None:
        db = MySQLdb.connect(**config.mysql)
    files = os.listdir(config.torrents_done)
    hashs = [h[:-8] for h in files if h.endswith(".torrent")]
    cur = db.cursor()
    i=0
    step=500
    archive_path = "%s/%s/" % (config.torrents_archive, time.strftime("%Y-%m-%d"))
    try: os.mkdir(archive_path)
    except OSError as e:
        if e.errno != 17: # file exist
            raise
    count = len(hashs)
    if count <= 0:
        return
    pbar = progressbar.ProgressBar(widgets=widget("files archived"), maxval=count).start()
    while hashs[i:i+step]:
        query = "SELECT hash, torcache FROM torrents WHERE created_at IS NOT NULL AND (%s)" % " OR ".join("hash=%s" for hash in hashs[i:i+step])
        cur.execute(query, tuple(hashs[i:i+step]))
        db_hashs = dict((r[0], r[1]) for r in cur)
        c=i
        for hash, torcache in db_hashs.items():
            if config.compact_archived_torrents and torcache == 1:
                torrent = get_torrent(db, hash, base_path=config.torrents_done)
                torrent[b'info'][b'pieces'] = b''
                with open("%s%s.torrent" % (archive_path, hash), 'wb+') as f:
                    f.write(utils.bencode(torrent))
                os.remove("%s/%s.torrent" % (config.torrents_done, hash))
            else:
                os.rename("%s/%s.torrent" % (config.torrents_done, hash), "%s%s.torrent" % (archive_path, hash))
            c+=1
            pbar.update(c)
        i=min(i + step, count)
        pbar.update(i)
    pbar.finish()