Skip to content

Instantly share code, notes, and snippets.

@gs-niteesh
Created May 31, 2021 18:47
Show Gist options
  • Save gs-niteesh/881c89c3d1b1ae365ca6fcb49785dc55 to your computer and use it in GitHub Desktop.
Save gs-niteesh/881c89c3d1b1ae365ca6fcb49785dc55 to your computer and use it in GitHub Desktop.
import requests
import sys
from threading import Thread
import queue
import logging
logging.basicConfig(level=logging.INFO)
logging = logging.getLogger(__name__)
class Worker(Thread):
def __init__(self, name, queue):
Thread.__init__(self)
self.name = name
self.queue = queue
def run(self):
while not self.queue.empty():
link = self.queue.get()
file_name = link.split('/')[-1].strip('\r\n').replace('%20', '_')
logging.info(f'{self.name}: {link}')
res = requests.get(link)
if res.status_code != 200:
logging.error(f'Error retrieving link: {link}')
else:
with open(file_name, 'wb') as f:
f.write(res.content)
self.queue.task_done()
def run(links_file):
links = []
work_queue = queue.Queue()
with open(links_file, 'r') as f:
links = f.readlines()
for link in links:
link = link.strip('\n\r')
if link != '':
work_queue.put(link)
for i in range(8):
worker = Worker(f'Thread {i}', work_queue)
worker.daemon = True
worker.start()
work_queue.join()
if __name__ == '__main__':
if len(sys.argv) != 2:
logging.info('Usage: python3 download.py links_file')
sys.exit(0)
run(sys.argv[1])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment