Skip to content
Snippets Groups Projects
Commit b7637a10 authored by onny's avatar onny
Browse files

added fallback direct dl support if download timesout

parent 99de7048
No related branches found
No related tags found
No related merge requests found
timeout = 60 # in seconds
...@@ -22,14 +22,16 @@ ses.listen_on(6881, 6891) ...@@ -22,14 +22,16 @@ ses.listen_on(6881, 6891)
# properly remove object # properly remove object
# verbose flag for additional information # verbose flag for additional information
# pacman like usage: python-progressbar # pacman like usage: python-progressbar
# p2p packet list # p2p packet database list
# torrent timout and then use normal dl
packages = [] packages = []
class torrent: class torrent:
def __init__(self, path): def __init__(self, path, link):
self.path = path self.path = path
self.starttime = time.time()
self.lastactivity = ""
self.link = link
info = lt.torrent_info(path) info = lt.torrent_info(path)
self.h = ses.add_torrent(info, "/var/cache/pacman/pkg/") self.h = ses.add_torrent(info, "/var/cache/pacman/pkg/")
...@@ -52,19 +54,30 @@ class torrent: ...@@ -52,19 +54,30 @@ class torrent:
(s.progress * 100, s.download_rate / 1000, s.upload_rate / 1000, \ (s.progress * 100, s.download_rate / 1000, s.upload_rate / 1000, \
s.num_peers)) s.num_peers))
#s.num_peers, state_str[s.state])) #s.num_peers, state_str[s.state]))
def idle(self):
s = self.h.status()
if s.progress != self.lastactivity:
self.lastactivity = s.progress
self.starttime = time.time()
return 0
else:
return time.time()-self.starttime
class threadstart(threading.Thread): class threadstart(threading.Thread):
def __init__(self, path): def __init__(self, path, link):
threading.Thread.__init__(self) threading.Thread.__init__(self)
self.path = path self.path = path
self.link = link
def run(self): def run(self):
#logging.debug('Loading torrent file in thread: '+self.path) #logging.debug('Loading torrent file in thread: '+self.path)
print("Loading torrent file in thread") print("Loading torrent file in thread")
package = torrent(self.path) package = torrent(self.path, self.link)
packages.append(package) packages.append(package)
print("syncing pacman database ...") os.system("pacman -Sy")
process = subprocess.Popen(['pacman', '-Syup'], shell=False, stdout=subprocess.PIPE) print(":: Starting full system upgrade...")
process = subprocess.Popen(['pacman', '-Sup'], shell=False, stdout=subprocess.PIPE)
processret = str(process.communicate()[0]) # get python stdout processret = str(process.communicate()[0]) # get python stdout
processret = processret.replace("pkg.tar.xz","pkg.tar.xz.torrent") # append to every link .torrent processret = processret.replace("pkg.tar.xz","pkg.tar.xz.torrent") # append to every link .torrent
...@@ -90,7 +103,7 @@ for link in torrentlinks: ...@@ -90,7 +103,7 @@ for link in torrentlinks:
else: else:
print("error: your mirror doesn't have proper torrent support") print("error: your mirror doesn't have proper torrent support")
exit(1) exit(1)
thread = threadstart("/var/cache/pacman/pkg/"+link.split('/')[-1]) thread = threadstart("/var/cache/pacman/pkg/"+link.split('/')[-1],link)
thread.start() thread.start()
time.sleep(5) time.sleep(5)
...@@ -99,6 +112,23 @@ while len(packages): ...@@ -99,6 +112,23 @@ while len(packages):
print("downloading "+str(len(packages))+" package(s):") print("downloading "+str(len(packages))+" package(s):")
for torrent in packages: for torrent in packages:
torrent.print_state() torrent.print_state()
if torrent.idle() > 60:
print("timeout reached, skipping torrent. idle time: %d" % torrent.idle())
print("starting manual direct download of: %s" % vars(torrent)['link'].split('/')[-1].replace('.torrent',''))
packages.pop(-1)
try:
r = requests.get(vars(torrent)['link'].replace('.torrent',''))
except:
print("error: mirror refues the connection, aborting.")
exit(1)
if r.status_code == 200:
with open("/var/cache/pacman/pkg/"+vars(torrent)['link'].split('/')[-1].replace('.torrent',''), 'wb') as f:
for chunk in r.iter_content():
f.write(chunk)
f.close
else:
print("error: your mirror doesn't have proper torrent support")
exit(1)
if not torrent.return_state(): if not torrent.return_state():
packages.pop(-1) packages.pop(-1)
time.sleep(5) time.sleep(5)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment