Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- import csv
- import sys
- import urllib.request
- import urllib.response
- import re
- import queue
- import threading
- import time
- '''
- Step 001:
- # Read the CSV file
- # For each row in csv file extract the links
- Step 002:
- # Write the link into a text file with '\n' (New line) concat
- Step 003:
- # Read that text file
- # Create a queue
- # And insert all the link into queue which contains .MP3 Extension
- Step 004:
- # Create a function which will download file.
- # This function will take queue and saving directory as parameter
- '''
- start = time.time()
- q = queue.Queue()
- threads = []
- txt_file = open('C:\\Users\\Engineers\\Downloads\\172.27.102.252_14th_Feb_2019.txt', 'w')
- save_directory = 'C:\\Users\\Engineers\\Downloads\\MP3\\'
- thread_lock = threading.Lock()
- # Step 001
- with open('C:\\Users\\Engineers\\Downloads\\172.27.102.252_14th_Feb_2019.csv', 'r') as csv_file:
- for row in csv.reader(csv_file, delimiter=','):
- if re.match(r'https?:\/{2}(?:[\/-\\w.]|(?:%[\da-fA-F]{2}))+', str(list(row)[0])):
- # Step 002
- txt_file.write(str(list(row)[0]) + '\n')
- # Step 003
- with open('C:\\Users\\Engineers\\Downloads\\172.27.102.252_14th_Feb_2019.txt', 'r') as f:
- for line in csv.reader(f, delimiter='\n'):
- if str(line[0]).__contains__('.MP3'):
- q.put(str(line[0]))
- print(q.qsize())
- # Step 004
- def multi_threaded_downloader(q, save_dir):
- try:
- while q.empty() is not True:
- base_url = q.get(block=False)
- request_body = urllib.request.Request(base_url)
- request_body.add_header('User-Agent',
- r'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:66.0) Gecko/20100101 Firefox/66.0')
- response_body = urllib.request.urlopen(request_body)
- # response_data = response_body.read()
- if not os.path.exists(save_dir[0:len(save_dir) - len(os.path.basename(save_dir))]):
- os.mkdir(save_dir[0:len(save_dir) - len(os.path.basename(save_dir))])
- with open(save_dir[0:len(save_dir) - len(os.path.basename(save_dir))] + urllib.parse.unquote(
- os.path.basename(base_url)),
- 'wb') as f:
- with thread_lock:
- total = response_body.length
- downloaded = 0
- while total > downloaded:
- chunk_size = int(max(1024, 1024*1024))
- x = response_body.read(chunk_size)
- f.write(x)
- downloaded = downloaded + len(x)
- done = int((100 * downloaded) / total)
- # print(str(done) + '\n')
- sys.stdout.write('\r[{}{}]'.format('█' * done, str(done)))
- sys.stdout.flush()
- if total == downloaded:
- print("Successfully Downloaded {0}\n".format(
- urllib.parse.unquote(os.path.basename(base_url))))
- q.task_done()
- except Exception as ex:
- print(ex)
- q.task_done()
- for i in range(5):
- t = threading.Thread(name='thread {0}'.format(i), target=multi_threaded_downloader, args=(q, save_directory))
- threads.append(t)
- t.setDaemon(True)
- t.start()
- q.join()
- # for t in threads:
- # t.join()
- # print('{0} Has joined'.format(t.getName()))
- end = time.time()
- print("Total elapsed time {0}".format(end - start))
Add Comment
Please, Sign In to add comment