Created
September 25, 2024 04:55
-
-
Save its-arun/bf7d38388cc92faa7076b6a4b2be4867 to your computer and use it in GitHub Desktop.
Panels Wallpapers Downloader
This file contains 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 requests | |
import json | |
from urllib.parse import urlparse, unquote | |
def load_json_from_url(url): | |
try: | |
response = requests.get(url) | |
response.raise_for_status() | |
return response.json() | |
except requests.exceptions.RequestException as e: | |
print(f"Error fetching JSON from {url}: {e}") | |
return None | |
def download_file(url, file_path): | |
try: | |
response = requests.get(url, stream=True) | |
response.raise_for_status() | |
os.makedirs(os.path.dirname(file_path), exist_ok=True) | |
with open(file_path, 'wb') as file: | |
for chunk in response.iter_content(chunk_size=8192): | |
if chunk: | |
file.write(chunk) | |
print(f"Downloaded: {file_path}") | |
except requests.exceptions.RequestException as e: | |
print(f"Error downloading {url}: {e}") | |
def generate_file_path(type, url): | |
parsed_url = urlparse(url) | |
path_without_query = parsed_url.path | |
path_without_query = unquote(path_without_query) | |
url_path = path_without_query.split('/content/')[-1] | |
formatted_path = url_path.replace('_', '/').replace('~', '-') | |
return os.path.join(type, formatted_path) | |
def process_json_data(data): | |
for entry in data.values(): | |
if 'dhd' in entry: | |
dhd_url = entry['dhd'] | |
dhd_file_path = generate_file_path('DHD', dhd_url) | |
download_file(dhd_url, dhd_file_path) | |
if 'dsd' in entry: | |
dsd_url = entry['dsd'] | |
dsd_file_path = generate_file_path('DSD', dsd_url) | |
download_file(dsd_url, dsd_file_path) | |
json_url = "https://storage.googleapis.com/panels-api/data/20240916/media-1a-c-p~uhd" | |
json_data = load_json_from_url(json_url) | |
if json_data and 'data' in json_data: | |
process_json_data(json_data['data']) | |
else: | |
print("No valid data found in the JSON.") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment