-
Notifications
You must be signed in to change notification settings - Fork 0
/
boosty_to_rss.py
executable file
·209 lines (174 loc) · 6.84 KB
/
boosty_to_rss.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
import requests
from feedgen.feed import FeedGenerator
from datetime import datetime, timezone
import ast
import json
import uuid
import os.path
import urllib.parse
import sys
from time import time
API_URL = 'https://api.boosty.to'
class BoostyToRSS():
def __init__(self, config_path='config.json'):
self.config_path = config_path
if os.path.isfile(self.config_path):
with open(self.config_path, 'r') as config_file:
self.config = json.load(config_file)
else:
self.config = {}
if 'uuid' in self.config:
self.uuid = self.config['uuid']
else:
self.uuid = str(uuid.uuid1())
if 'phone_number' in self.config:
self.phone_number = self.config['phone_number']
else:
self.phone_number = urllib.parse.quote_plus(input("Enter your phone number: "))
if 'access_token' in self.config:
self.access_token = self.config['access_token']
self.refresh_token = self.config['refresh_token']
if 'expires' in self.config:
self.expires = self.config['expires']
if int(time()) >= self.expires:
refresh_needed = True
else:
refresh_needed = True
else:
self.authenticate()
if refresh_needed:
self.refresh_auth()
def __del__(self):
print('Destructor called, vehicle deleted.')
def save_config(self):
with open(self.config_path, 'w') as config_file:
self.config['uuid'] = self.uuid
self.config['phone_number'] = self.phone_number
self.config['access_token'] = self.access_token
self.config['refresh_token'] = self.refresh_token
self.config['expires'] = self.expires
json.dump(self.config, config_file)
def authenticate(self):
request_body = f'client_id={self.phone_number}'
headers = {
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:91.0) Gecko/20100101 Firefox/91.0',
'Accept': 'application/json, text/plain, */*',
'Accept-Language': 'en-US,en;q=0.5',
'Content-Type': 'application/x-www-form-urlencoded',
'X-From-Id': self.uuid,
'X-App': 'web',
'X-Referer': '',
'Sec-Fetch-Dest': 'empty',
'Sec-Fetch-Mode': 'cors',
'Sec-Fetch-Site': 'same-site',
}
response = requests.post(f'{API_URL}/oauth/phone/authorize',
data=request_body,
headers=headers)
response_data = response.json()
code = response_data['code']
sms_code = input("SMS Code: ")
request_body = f'device_id={self.uuid}&sms_code={sms_code}&device_os=web&client_id={self.phone_number}&code={code}'
response = requests.post(f'{API_URL}/oauth/phone/token',
data=request_body,
headers=headers)
response_data = response.json()
self.refresh_token = response_data['refresh_token']
self.access_token = response_data['access_token']
self.expires = int(time()) + response_data['expires_in']
self.save_config()
def refresh_auth(self):
headers = {
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:91.0) Gecko/20100101 Firefox/91.0',
'Authorization': f'Bearer {self.access_token}',
'Accept': 'application/json, text/plain, */*',
'Accept-Language': 'en-US,en;q=0.5',
'Content-Type': 'application/x-www-form-urlencoded',
'X-From-Id': self.uuid,
'X-App': 'web',
'X-Referer': '',
'Sec-Fetch-Dest': 'empty',
'Sec-Fetch-Mode': 'cors',
'Sec-Fetch-Site': 'same-site',
}
request_body = f'device_id={self.uuid}&device_os=web&grant_type=refresh_token&refresh_token={self.refresh_token}'
response = requests.post(f'{API_URL}/oauth/token/',
data=request_body,
headers=headers)
response_data = response.json()
self.refresh_token = response_data['refresh_token']
self.access_token = response_data['access_token']
self.expires = int(time()) + response_data['expires_in']
self.save_config()
def generate_rss(self, author):
headers = {
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:91.0) Gecko/20100101 Firefox/91.0',
'Authorization': f'Bearer {self.access_token}',
'Accept': 'application/json, text/plain, */*',
'Accept-Language': 'en-US,en;q=0.5',
'Content-Type': 'application/x-www-form-urlencoded',
'X-From-Id': self.uuid,
'X-App': 'web',
'X-Referer': '',
'Sec-Fetch-Dest': 'empty',
'Sec-Fetch-Mode': 'cors',
'Sec-Fetch-Site': 'same-site',
}
# TODO: check for authentication errors
response = requests.get(f'{API_URL}/v1/blog/{author}',
headers=headers)
response_data = response.json()
if response_data['owner']['hasAvatar']:
logo_url = response_data['owner']['avatarUrl']
title = response_data['owner']['name']
podcast_desc = ''
for block in response_data['description']:
if 'modificator' in block and block['modificator'] != 'BLOCK_END' and block['type'] == 'text':
text = ast.literal_eval(block['content'])
podcast_desc += text[0] + '\r\n'
response = requests.get(f'{API_URL}/v1/blog/{author}/post/',
headers=headers)
response_data = response.json()
fg = FeedGenerator()
fg.load_extension('podcast')
fg.link(href=f'https://boosty.to/{author}', rel='self')
fg.title(title)
fg.description(podcast_desc)
fg.image(url=logo_url)
# TODO: move from feedgen, it's very unflexible
for post in response_data['data']:
if not post['hasAccess']:
continue
desc = ''
download_url = None
for content in post['data']:
if content['type'] == 'audio_file':
url = content['url']
params = post['signedQuery']
download_url = f'{url}{params}'
elif content['type'] == 'file' and 'mp3' in content['title']:
url = content['url']
params = post['signedQuery']
download_url = f'{url}{params}'
if 'modificator' in content and content['modificator'] != 'BLOCK_END' and content['type'] == 'text':
text = ast.literal_eval(content['content'])
desc += text[0] + '\r\n'
if 'download_url' in vars() and download_url:
# TODO: add post covers
fe = fg.add_entry()
for teaser in post['teaser']:
if 'url' in teaser:
post_logo = teaser['url']
fe.enclosure(post_logo, 0, 'image/jpeg')
# TODO: get real timezone
fe.pubDate(datetime.fromtimestamp(post['publishTime'], timezone.utc))
fe.title(post['title'])
fe.description(desc)
fe.enclosure(download_url, 0, 'audio/mpeg')
fg.rss_str(pretty=True)
fg.rss_file(f'{author}.xml')
if __name__ == "__main__":
# TODO: add cli arguments handling
podcast_name = sys.argv[1]
b = BoostyToRSS()
b.generate_rss(podcast_name)