mirror of
https://github.com/THIS-IS-NOT-A-BACKUP/zspotify.git
synced 2024-11-29 19:24:34 +01:00
added experimental podcast download method
This commit is contained in:
parent
8053045022
commit
3beaa7564c
@ -27,7 +27,8 @@ def get_show_episodes(show_id_str) -> list:
|
|||||||
limit = 50
|
limit = 50
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
resp = ZSpotify.invoke_url_with_params(f'{SHOWS_URL}/{show_id_str}/episodes', limit=limit, offset=offset)
|
resp = ZSpotify.invoke_url_with_params(
|
||||||
|
f'{SHOWS_URL}/{show_id_str}/episodes', limit=limit, offset=offset)
|
||||||
offset += limit
|
offset += limit
|
||||||
for episode in resp[ITEMS]:
|
for episode in resp[ITEMS]:
|
||||||
episodes.append(episode[ID])
|
episodes.append(episode[ID])
|
||||||
@ -37,6 +38,33 @@ def get_show_episodes(show_id_str) -> list:
|
|||||||
return episodes
|
return episodes
|
||||||
|
|
||||||
|
|
||||||
|
def download_podcast_experimental(url, filename):
|
||||||
|
import functools
|
||||||
|
import pathlib
|
||||||
|
import shutil
|
||||||
|
import requests
|
||||||
|
from tqdm.auto import tqdm
|
||||||
|
|
||||||
|
r = requests.get(url, stream=True, allow_redirects=True)
|
||||||
|
if r.status_code != 200:
|
||||||
|
r.raise_for_status() # Will only raise for 4xx codes, so...
|
||||||
|
raise RuntimeError(
|
||||||
|
f"Request to {url} returned status code {r.status_code}")
|
||||||
|
file_size = int(r.headers.get('Content-Length', 0))
|
||||||
|
|
||||||
|
path = pathlib.Path(filename).expanduser().resolve()
|
||||||
|
path.parent.mkdir(parents=True, exist_ok=True)
|
||||||
|
|
||||||
|
desc = "(Unknown total file size)" if file_size == 0 else ""
|
||||||
|
r.raw.read = functools.partial(
|
||||||
|
r.raw.read, decode_content=True) # Decompress if needed
|
||||||
|
with tqdm.wrapattr(r.raw, "read", total=file_size, desc=desc) as r_raw:
|
||||||
|
with path.open("wb") as f:
|
||||||
|
shutil.copyfileobj(r_raw, f)
|
||||||
|
|
||||||
|
return path
|
||||||
|
|
||||||
|
|
||||||
def download_episode(episode_id) -> None:
|
def download_episode(episode_id) -> None:
|
||||||
podcast_name, episode_name = get_episode_info(episode_id)
|
podcast_name, episode_name = get_episode_info(episode_id)
|
||||||
|
|
||||||
@ -47,9 +75,6 @@ def download_episode(episode_id) -> None:
|
|||||||
else:
|
else:
|
||||||
filename = podcast_name + ' - ' + episode_name
|
filename = podcast_name + ' - ' + episode_name
|
||||||
|
|
||||||
episode_id = EpisodeId.from_base62(episode_id)
|
|
||||||
stream = ZSpotify.get_content_stream(episode_id, ZSpotify.DOWNLOAD_QUALITY)
|
|
||||||
|
|
||||||
download_directory = os.path.join(
|
download_directory = os.path.join(
|
||||||
os.path.dirname(__file__),
|
os.path.dirname(__file__),
|
||||||
ZSpotify.get_config(ROOT_PODCAST_PATH),
|
ZSpotify.get_config(ROOT_PODCAST_PATH),
|
||||||
@ -58,33 +83,12 @@ def download_episode(episode_id) -> None:
|
|||||||
download_directory = os.path.realpath(download_directory)
|
download_directory = os.path.realpath(download_directory)
|
||||||
create_download_directory(download_directory)
|
create_download_directory(download_directory)
|
||||||
|
|
||||||
total_size = stream.input_stream.size
|
filepath = os.path.join(download_directory, f"{filename}.mp3")
|
||||||
|
|
||||||
filepath = os.path.join(download_directory, f"{filename}.ogg")
|
download_url = ZSpotify.invoke_url(
|
||||||
if (
|
'https://api-partner.spotify.com/pathfinder/v1/query?operationName=getEpisode&variables={"uri":"spotify:episode:3u0krXMhLcdGZUZkDVmMSt"}&extensions={"persistedQuery":{"version":1,"sha256Hash":"224ba0fd89fcfdfb3a15fa2d82a6112d3f4e2ac88fba5c6713de04d1b72cf482"}}')["data"]["episode"]["audio"]["items"][-1]["url"]
|
||||||
os.path.isfile(filepath)
|
|
||||||
and os.path.getsize(filepath) == total_size
|
|
||||||
and ZSpotify.get_config(SKIP_EXISTING_FILES)
|
|
||||||
):
|
|
||||||
print(
|
|
||||||
"\n### SKIPPING:",
|
|
||||||
podcast_name,
|
|
||||||
"-",
|
|
||||||
episode_name,
|
|
||||||
"(EPISODE ALREADY EXISTS) ###",
|
|
||||||
)
|
|
||||||
return
|
|
||||||
|
|
||||||
with open(filepath, 'wb') as file, tqdm(
|
download_podcast_experimental(download_url, filepath)
|
||||||
desc=filename,
|
|
||||||
total=total_size,
|
|
||||||
unit='B',
|
|
||||||
unit_scale=True,
|
|
||||||
unit_divisor=1024
|
|
||||||
) as bar:
|
|
||||||
for _ in range(int(total_size / ZSpotify.get_config(CHUNK_SIZE)) + 1):
|
|
||||||
bar.update(file.write(
|
|
||||||
stream.input_stream.stream().read(ZSpotify.get_config(CHUNK_SIZE))))
|
|
||||||
|
|
||||||
# convert_audio_format(ROOT_PODCAST_PATH +
|
# convert_audio_format(ROOT_PODCAST_PATH +
|
||||||
# extra_paths + filename + '.ogg')
|
# extra_paths + filename + '.ogg')
|
||||||
|
Loading…
Reference in New Issue
Block a user