python-ox/ox/torrent/__init__.py

76 lines
2.3 KiB
Python
Raw Permalink Normal View History

2008-04-27 16:54:37 +00:00
# -*- coding: utf-8 -*-
2008-06-19 09:21:21 +00:00
# vi:si:et:sw=4:sts=4:ts=4
2012-08-21 08:49:36 +00:00
# GPL 2007-2012
2008-04-27 16:54:37 +00:00
from threading import Event
2010-09-03 21:19:19 +00:00
from hashlib import sha1
2009-09-03 16:29:04 +00:00
import os
2008-04-27 16:54:37 +00:00
2023-07-27 11:07:13 +00:00
from .bencode3 import bencode, bdecode
2008-04-27 16:54:37 +00:00
2012-08-21 08:49:36 +00:00
__all__ = ['create_torrent', 'get_info_hash', 'get_torrent_info', 'get_files', 'get_torrent_size']
2008-04-27 16:54:37 +00:00
2016-06-08 13:32:46 +00:00
def create_torrent(file, url, params={}, flag=Event(),
progress=lambda x: None, progress_percent=1):
2008-06-19 09:21:21 +00:00
"Creates a torrent for a given file, using url as tracker url"
2014-10-01 09:21:11 +00:00
from .makemetafile import make_meta_file
2008-06-19 09:21:21 +00:00
return make_meta_file(file, url, params, flag, progress, progress_percent)
2008-04-27 16:54:37 +00:00
2012-08-21 08:49:36 +00:00
def get_info_hash(torrentFile):
2008-06-19 09:21:21 +00:00
"Returns Torrent Info Hash from torrent file"
metainfo_file = open(torrentFile, 'rb')
metainfo = bdecode(metainfo_file.read())
info = metainfo['info']
2010-09-03 21:19:19 +00:00
return sha1(bencode(info)).hexdigest()
2008-04-27 16:54:37 +00:00
2012-08-21 08:49:36 +00:00
def get_torrent_info(data=None, file=None):
if file:
2014-09-30 19:04:46 +00:00
if not isinstance(file, bytes):
2013-10-11 18:38:35 +00:00
file = file.encode('utf-8')
2012-08-21 08:49:36 +00:00
with open(file, 'rb') as f:
data = f.read()
2010-09-03 21:19:19 +00:00
2008-06-19 09:21:21 +00:00
"Returns Torrent Info from torrent file"
tinfo = {}
metainfo = bdecode(data)
info = metainfo['info']
piece_length = info['piece length']
2014-09-30 19:04:46 +00:00
if 'length' in info:
2008-06-19 09:21:21 +00:00
# let's assume we just have one file
file_length = info['length']
else:
# let's assume we have a directory structure
file_length = 0;
for f in info['files']:
file_length += f['length']
for key in info:
if key != 'pieces':
tinfo[key] = info[key]
for key in metainfo:
if key != 'info':
tinfo[key] = metainfo[key]
tinfo['size'] = file_length
2010-09-03 21:19:19 +00:00
tinfo['hash'] = sha1(bencode(info)).hexdigest()
2020-07-28 09:36:20 +00:00
if 'announce' in metainfo:
tinfo['announce'] = metainfo['announce']
2012-08-21 08:49:36 +00:00
if file:
tinfo['timestamp'] = os.stat(file).st_ctime
2008-06-19 09:21:21 +00:00
return tinfo
2008-04-27 16:54:37 +00:00
2012-08-21 08:49:36 +00:00
def get_files(data):
2009-09-03 16:29:04 +00:00
files = []
2012-08-21 08:49:36 +00:00
info = get_torrent_info(data=data)
2009-09-03 16:29:04 +00:00
if 'files' in info:
for f in info['files']:
path = [info['name'], ]
path.extend(f['path'])
files.append(os.path.join(*path))
else:
files.append(info['name'])
return files
2012-08-21 08:49:36 +00:00
def get_torrent_size(file):
2008-06-19 09:21:21 +00:00
"Returns Size of files in torrent file in bytes"
2012-08-21 08:49:36 +00:00
return get_torrent_info(file=file)['size']
2008-04-27 16:54:37 +00:00