This repository has been archived by the owner on Jul 18, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
spotcast.py
73 lines (55 loc) · 2.4 KB
/
spotcast.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#!/usr/bin/env python3
import os
from urllib.parse import urlparse
from pathlib import Path
import requests
from bs4 import BeautifulSoup
from dotenv import load_dotenv
load_dotenv()
SHOW_URL = "https://open.spotify.com/show/2G3qmhWe8reqyCc7SRqI8F"
DOWNLOAD_PATH = "./downloads"
def get_episodes(url):
headers = {
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.1.2 Safari/605.1.15",
}
response = requests.get(url, headers=headers)
soup = BeautifulSoup(response.text, features="html.parser")
meta = soup.findAll("meta", {"property": "music:song"})
url_list = [tag["content"] for tag in meta]
return [urlparse(url).path.split("/")[-1] for url in url_list]
def sanitize_filename(name):
return "".join(x for x in name if (x.isalnum() or x in "._- "))
def fetch_episode(episode_id):
url = "https://api.spotify.com/v1/episodes/" + episode_id
headers = {
"Authorization": "Bearer {}".format(os.getenv("SPOTIFY_TOKEN")),
"Origin": "https://open.spotify.com",
"Referer": "https://open.spotify.com/show/2G3qmhWe8reqyCc7SRqI8F",
"Accept": "*/*",
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.1.2 Safari/605.1.15",
}
response = requests.get(url, headers=headers)
response.raise_for_status()
parsed = response.json()
print(f"Fetching episode {episode_id} - {parsed['name']}")
download_url = parsed["external_playback_url"]
filename = "{}.mp3".format(sanitize_filename(parsed["name"]))
print(" --> " + download_url)
headers = {
"Accept": "*/*",
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.1.2 Safari/605.1.15",
"Referer": "https://open.spotify.com/show/2G3qmhWe8reqyCc7SRqI8F",
"Accept-Encoding": "identity",
"Connection": "Keep-Alive",
}
episode = requests.get(download_url, headers=headers)
file_dir = Path(DOWNLOAD_PATH)
file_dir.mkdir(parents=True, exist_ok=True)
with open(file_dir / filename, "wb") as f:
f.write(episode.content)
episodes = get_episodes(SHOW_URL)
print(f"Found {len(episodes)} episodes")
print(episodes)
for ep in episodes:
fetch_episode(ep)