Last active
May 11, 2024 16:19
-
-
Save meetox80/98ab52737ce9363d37a5b2e9f4eca395 to your computer and use it in GitHub Desktop.
Fontawesome v6.5.1 scraper
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import re | |
import requests | |
from urllib.parse import urljoin, urlparse | |
def download_file(url, directory_path): | |
filename = url.split("/")[-1] | |
filepath = os.path.join(directory_path, filename) | |
with open(filepath, "wb") as f: | |
f.write(requests.get(url).content) | |
print(f"Downloaded {filename} to {directory_path}") | |
def fetch_and_download_files(css_url, base_url, download_dir): | |
response = requests.get(css_url) | |
if response.status_code == 200: | |
css_content = response.text | |
file_urls = re.findall(r"url\((.*?)\)", css_content) | |
for url in file_urls: | |
if url.endswith(".woff2") or url.endswith(".ttf"): | |
# Join the base_url with the parent directory of css_url and the url from CSS | |
url = urljoin(urljoin(base_url, os.path.dirname(css_url)), url.strip("'\"")) | |
# Include version number | |
url = url.replace("releases/", f"releases/v6.5.1/") | |
print("Constructed URL:", url) # Add this line | |
parsed_url = urlparse(url) | |
filename = os.path.basename(parsed_url.path) | |
directory_path = os.path.join(download_dir, "webfonts") | |
os.makedirs(directory_path, exist_ok=True) | |
download_file(url, directory_path) | |
else: | |
print(f"Failed to fetch CSS. Status code: {response.status_code}") | |
def main(): | |
base_url = "https://site-assets.fontawesome.com/releases/v6.5.1/" | |
css_url = urljoin(base_url, "css/all.css") | |
download_dir = "./releases/v6.5.1/" | |
# Create the directory structure | |
os.makedirs(os.path.join(download_dir, "css"), exist_ok=True) | |
css_filename = "all.css" | |
css_filepath = os.path.join(download_dir, "css", css_filename) | |
# Download and save the CSS file | |
with open(css_filepath, "wb") as f: | |
f.write(requests.get(css_url).content) | |
print(f"Downloaded CSS file to {css_filepath}") | |
# Fetch and download the .woff2 files | |
fetch_and_download_files(css_url, base_url, download_dir) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment