Last active
December 24, 2023 17:07
-
-
Save catbaron0/c59e8ea66924657a1c8c128da3be1bd9 to your computer and use it in GitHub Desktop.
Download gcores radio albums
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
""" | |
Usage: | |
Download one radio: | |
python gcd.py -radio RADIO_ID | |
python gcd.py -r RADIO_ID | |
Download radios from an album: | |
python gcd.py -a ALBUM_ID | |
python gcd.py -album ALBUM_ID | |
The ID is available from the URLs to the radio or album | |
- https://www.gcores.com/albums/142 -> 142 is the ALBUM_ID | |
- https://www.gcores.com/radios/164711 -> 164711 is the radio_id | |
""" | |
import sys | |
import requests | |
from dataclasses import dataclass | |
from clint.textui import progress | |
from pathlib import Path | |
from typing import Dict | |
def _download(url: str, to_path): | |
r = requests.get(url, stream=True) | |
with open(to_path, 'wb') as f: | |
total_length = int(r.headers.get('content-length')) | |
for chunk in progress.bar( | |
r.iter_content(chunk_size=1024), | |
expected_size=(total_length/1024) + 1 | |
): | |
if chunk: | |
f.write(chunk) | |
f.flush() | |
@dataclass() | |
class Radio: | |
title: str | |
audio: str | |
cover: str | |
to_path: str | |
def download(self): | |
url = self.audio | |
print(self.title) | |
print(f"Downloading audio from {url}") | |
_download( | |
url, | |
Path(self.to_path) / (self.title + Path(url).suffix) | |
) | |
url = self.cover | |
print(f"Downloading cover from {url}") | |
_download( | |
url, | |
Path(self.to_path) / (self.title + Path(url).suffix) | |
) | |
def get_radio(radio_id: str, to_path: str = None) -> Radio: | |
r = requests.get(f"https://www.gcores.com/gapi/v1/radios/{radio_id}?include=media").json() | |
title = r["data"]["attributes"]["title"] | |
cover = r["data"]["attributes"]["cover"] | |
audio = r["included"][0]["attributes"]["audio"] | |
if not audio.startswith("https"): | |
audio="https://alioss.gcores.com/uploads/audio/" + audio | |
if to_path is None: | |
to_path = title | |
Path(to_path).mkdir(parents=True, exist_ok=True) | |
return Radio( | |
title=title, | |
audio=audio, | |
cover="https://image.gcores.com/" + cover, | |
to_path=to_path | |
) | |
def get_radios_album(album_id: str) -> Dict: | |
radios = requests.get( | |
f"https://www.gcores.com/gapi/v1/albums/{album_id}" | |
"/published-radios?page%5Blimit%5D=100&page%5Boffset%5D=0" | |
).json()["data"] | |
album = requests.get( | |
f"https://www.gcores.com/gapi/v1/albums/{album_id}" | |
).json()["data"]["attributes"] | |
return { | |
"title": album["title"], | |
"cover": "https://image.gcores.com/" + album["cover"], | |
"radios": [get_radio(r["id"], album["title"]) for r in radios if r["type"] == "radios"] | |
} | |
def main(): | |
album_types = {"album", "a"} | |
radio_types = {"radio", "r"} | |
media_type = sys.argv[1] | |
media_id = sys.argv[2] | |
assert media_type in album_types | radio_types | |
assert media_id.isdigit() == True | |
if media_type in radio_types: | |
print("Loading radio info...") | |
try: | |
radio = get_radio(media_id) | |
except Exception: | |
print(f"Invalid radio id: {media_id}") | |
return | |
radio.download() | |
else: | |
print("Loading album info...") | |
try: | |
album = get_radios_album(media_id) | |
except Exception: | |
print(f"Invalid album id: {media_id}") | |
return | |
print(f"Dowload covers of {album['title']}") | |
url = album["cover"] | |
_download( | |
url, | |
Path(album["title"]) / ("cover" + Path(url).suffix) | |
) | |
print(f"Dowload {len(album['radios'])} radios from {album['title']}") | |
for radio in album["radios"]: | |
print(" - " + radio.title) | |
print() | |
for radio in album["radios"]: | |
radio.download() | |
print() | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment