CommonCrawl_URL_Processor/warc_wat_url_processor.py

76 lines
2.4 KiB
Python
Raw Normal View History

2024-01-20 03:25:50 +00:00
import subprocess
import os
import gzip
import re
2024-01-20 11:25:59 +00:00
import traceback
2024-01-20 03:25:50 +00:00
from multiprocessing import Pool
def extract_urls_from_file(file_path):
urls = []
try:
with gzip.open(file_path, 'rt', encoding='latin-1') as file:
for line in file:
url_pattern = re.compile(r'http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\\(\\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+')
line_urls = re.findall(url_pattern, line)
urls.extend(line_urls)
except (gzip.BadGzipFile, EOFError) as e:
print(f"Error while reading the compressed file '{file_path}': {e}")
except Exception as e:
print(f"An unexpected error occurred while processing '{file_path}': {e}")
print("Full traceback:")
2024-01-20 11:25:59 +00:00
traceback.print_exc()
2024-01-20 11:29:57 +00:00
2024-01-20 03:25:50 +00:00
return urls
def process_file(file_path):
print(f"Processing file: {file_path}")
# Extract URLs from the gzipped file
urls = extract_urls_from_file(file_path)
# Create the output file path with '_urls.txt' extension
output_file_path = os.path.splitext(file_path)[0] + '_urls.txt'
print(f"Output file path: {output_file_path}")
# Write the URLs to the output file
with open(output_file_path, 'w') as output_file:
output_file.write('\n'.join(urls))
print(f"URLs written to {output_file_path}")
2024-01-20 11:29:57 +00:00
# Use zstd command-line tool for compression
command = f'zstd -T0 -18 --long {output_file_path} -o {output_file_path}.zst'
subprocess.run(command, shell=True)
2024-01-20 03:25:50 +00:00
print(f"Compressed file saved as '{output_file_path}.zst'")
# Remove the original gzipped file
os.remove(file_path)
print(f"Original file removed: {file_path}")
def extract_urls_from_directory(directory_path):
file_list = sorted(os.listdir(directory_path))
pool = Pool(processes=7)
pool.map(process_file, [os.path.join(directory_path, filename) for filename in file_list if filename.endswith('.warc.gz')])
pool.close()
pool.join()
with open('urls_to_download.txt', 'r') as file:
urls = file.readlines()
urls = [url.strip() for url in urls]
batch_size = 48
concurrency_level = 4
batches = [urls[i:i+batch_size] for i in range(0, len(urls), batch_size)]
for batch in batches:
pool = Pool(processes=concurrency_level)
for url in batch:
2024-01-20 03:26:03 +00:00
command = f'axel -n 4 {url}'
2024-01-20 03:25:50 +00:00
pool.apply_async(subprocess.run, args=(command,), kwds={'shell': True})
pool.close()
pool.join()
extract_urls_from_directory(os.getcwd())