-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpocketcasts.py
121 lines (99 loc) · 3.3 KB
/
pocketcasts.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import os
import json
import urllib3
def do_login(http, user, pw):
if not user or not pw:
return None
data = {"email": f"{user}", "password": f"{pw}", "scope": "webplayer"}
encoded_data = json.dumps(data).encode("utf-8")
print(encoded_data)
response = http.request(
"POST",
"https://api.pocketcasts.com/user/login",
headers={"Content-Type": "application/json"},
body=encoded_data,
)
token = json.loads(response.data)["token"]
print(token)
return token
def create_auth_headers(token):
return {"Authorization": f"Bearer {token}"}
# Documenting the endpoint. This isn't used at all.
def get_history(http, token):
header = create_auth_headers(token)
response = http.request(
"POST",
"https://api.pocketcasts.com/user/history",
headers=header,
)
data = json.loads(response.data)
return data
def search_podcasts(http, token, term):
header = create_auth_headers(token)
body = json.dumps({"term":term}, ensure_ascii=False).encode("ascii", errors="ignore")
header["content-length"] = len(body)
print(header)
print(body)
response = http.request(
"POST",
"https://api.pocketcasts.com/discover/search",
headers=header,
body=body,
)
return json.loads(response.data)
def search_podcasts_and_get_first_uuid(http, token, term):
search_result = search_podcasts(http, token, term)
try:
search_result["podcasts"][0]
except(IndexError, KeyError):
return None
# Get the first result
# It would be very rare to have two podcasts with the same name
# FIXME: Also check author here. Not sure if authors are consistent
# across platforms.
return search_result["podcasts"][0]["uuid"]
def get_subscriptions(http, token):
header = create_auth_headers(token)
body = json.dumps({"v": 1}).encode("utf-8")
response = http.request(
"POST",
"https://api.pocketcasts.com/user/podcast/list",
headers=header,
body=body,
)
return json.loads(response.data)
def add_subscription(http, token, uuid):
header = create_auth_headers(token)
body = json.dumps({"uuid": uuid}).encode("utf-8")
response = http.request(
"POST", "https://api.pocketcasts.com/user/podcast/subscribe", headers=header, body=body
)
return json.loads(response.data)
def get_episodes(http, token, podcast_uuid):
header = create_auth_headers(token)
response = http.request(
"GET", f"https://podcast-api.pocketcasts.com/podcast/full/{podcast_uuid}",
headers=header
)
data = json.loads(response.data)
try:
data["podcast"]["episodes"]
except(IndexError, KeyError):
return {}
episodes = {}
for episode in data["podcast"]["episodes"]:
episodes[episode["title"]] = episode["uuid"]
return episodes
def update_podcast_episode(http, token, body):
print("Updating episode:")
print(body)
header = create_auth_headers(token)
response = http.request(
"POST", "https://api.pocketcasts.com/sync/update_episode", headers=header, body=body
)
return json.loads(response.data)
if __name__ == "__main__":
http = urllib3.PoolManager()
token = do_login(http)
history = get_history(http, token)
print(history)