90 lines
3.3 KiB
Python
90 lines
3.3 KiB
Python
import time
|
|
import sys
|
|
import os
|
|
import upnpclient
|
|
os.add_dll_directory('C:\\ChaChaGameRepo\\Embedded\\libtorrent')
|
|
import libtorrent as lt
|
|
|
|
class ChaCha_downloader:
|
|
default_listen_address = "0.0.0.0"
|
|
default_listen_port = "8000"
|
|
|
|
def __init__(self,listen_interfaces:str = None):
|
|
if listen_interfaces is not None:
|
|
self.listen_interfaces = {'listen_interfaces': listen_address}
|
|
else:
|
|
self.listen_interfaces = {'listen_interfaces': "{0}:{1}".format(self.default_listen_address,self.default_listen_port)}
|
|
self.LibTorrentSession = None
|
|
self.ar_ActiveTorrents_Obj = []
|
|
self.ar_TorrentList = []
|
|
|
|
def Run(self):
|
|
#self.PreOpenListeningports()
|
|
self.GetTorrentList()
|
|
self.LaunchSync()
|
|
self.WaitSyncFinished()
|
|
|
|
def GetTorrentList(self):
|
|
dir_path = os.path.dirname(os.path.realpath(__file__))
|
|
print(dir_path)
|
|
torrent_path = os.path.join(dir_path,"UT99_commonBase.torrent")
|
|
print(torrent_path)
|
|
self.ar_TorrentList.append(str(torrent_path))
|
|
print(self.ar_TorrentList)
|
|
|
|
def PreOpenListeningports(self):
|
|
raise RuntimeError("NotImplemented")
|
|
print("Searching for UPnP Devices...")
|
|
devices = upnpclient.discover()
|
|
#print(devices)
|
|
service = None
|
|
for dev in devices:
|
|
print("Found Devices: << {0} >> [ << {1} >> ]".format(dev.friendly_name,dev.device_name ))
|
|
print("Searching Layer3Forwarding1 service...")
|
|
try:
|
|
service = dev.Layer3Forwarding1
|
|
print("Passed :)")
|
|
except:
|
|
print("Failed'(")
|
|
continue
|
|
|
|
if service :
|
|
self.listen_interfaces = {'listen_interfaces': '0.0.0.0:8000'}
|
|
else:
|
|
raise RuntimeError("\n/!\\ No suitable router found, you should open manually an input port. /!\\\n")
|
|
|
|
def LaunchSync(self):
|
|
self.LibTorrentSession = lt.session(self.listen_interfaces)
|
|
self.LibTorrentSession.add_extension("ut_pex")
|
|
self.LibTorrentSession.add_extension("smart_ban")
|
|
|
|
print('== adding torrents ==')
|
|
for torrentpath in self.ar_TorrentList:
|
|
print(torrentpath)
|
|
_info = lt.torrent_info(torrentpath)
|
|
h = self.LibTorrentSession.add_torrent({'ti': _info, 'save_path': './Storage'})
|
|
self.ar_ActiveTorrents_Obj.append(h)
|
|
_s = h.status()
|
|
print("Added: {0} \t[{1}]==".format(_s.name,torrentpath))
|
|
print("All torrents added")
|
|
|
|
|
|
def WaitSyncFinished(self):
|
|
while True:
|
|
for torrent in self.ar_ActiveTorrents_Obj:
|
|
_s = torrent.status()
|
|
print('\r%s %.2f%% complete (down: %.1f kB/s up: %.1f kB/s peers: %d) %s\n' % ( _s.name,
|
|
_s.progress * 100, _s.download_rate / 1000, _s.upload_rate / 1000,
|
|
_s.num_peers, _s.state), end=' ')
|
|
alerts = self.LibTorrentSession.pop_alerts()
|
|
for a in alerts:
|
|
if a.category() & lt.alert.category_t.error_notification:
|
|
print(a)
|
|
|
|
sys.stdout.flush()
|
|
time.sleep(1)
|
|
print(h.status().name, 'complete')
|
|
|
|
if __name__ == '__main__':
|
|
dldr = ChaCha_downloader()
|
|
dldr.Run() |