-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelper.py
133 lines (113 loc) · 3.73 KB
/
helper.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
import requests
import os
import logging
import json
import re
from datetime import datetime
from email import utils
from time import sleep
from dotenv import load_dotenv
from redisHelper import RedisHelper
load_dotenv()
GITHUB_API = "https://api.github.com"
GITHUB_TOKEN = os.environ.get("GITHUB_TOKEN","")
r = RedisHelper()
log = logging.getLogger('root.helper')
def writeGist(content:str,filename:str):
filename = f"{filename}.rss"
url=GITHUB_API+"/gists"
# print("Request URL: %s"%url)
#print headers,parameters,payload
headers={"Authorization":"token %s"%GITHUB_TOKEN}
params={"scope":"gist"}
payload = {
"description":"GIST created by python code",
"public":True,
"files":{
filename:{
"content":content
}
}
}
#make a requests
res=requests.post(url,headers=headers,params=params,data=json.dumps(payload))
response = res.json()
# print("resp",response,type(res.status_code))
if res.status_code >= 200 and res.status_code < 300:
return response["files"][filename]["raw_url"]
else:
return None
def uploadToPocketcasts(rss_url:str):
# print("url",rss_url)
endpoint = "https://refresh.pocketcasts.com/author/add_feed_url"
headers={
"origin": "https://pocketcasts.com",
"referer": "https://pocketcasts.com/"
}
payload = {
"public_option":"yes",
"url":rss_url
}
response = requests.post(endpoint,headers=headers,json=payload)
result = response.json()
if response.status_code==200:
if(result["status"] == "poll"):
# wait 2 secs then call api again;
timeout = 0.5
while result["status"] == "poll":
sleep(timeout)
timeout *= 2
response = requests.post(endpoint,headers=headers,json=payload)
result = response.json()
if(result["status"] == "ok"):
return {"status":"ok", "url":result["result"]["share_link"]}
# error state return error info
return result
def fixRSSfile(rss_url):
rss_response = requests.get(rss_url)
rss_response_content = rss_response.text
# add valid pubdate
now_datetime = utils.format_datetime(datetime.now())
rss_response_content = re.sub(
pattern=r"<!--<pubDate>file element=rss.pubDate</pubDate>-->",
string=rss_response_content,
repl=f"<pubDate>{now_datetime}</pubDate>"
)
return rss_response_content
def getBooks(search_title):
r = "https://librivox.org/api/feed/audiobooks/"
req =requests.get(
r,
params={
"title":f"^{search_title}",
"format":"json",
"limit":10
}
)
result = req.json()
if 'books' in result:
return result["books"]
return []
def uploadRss(librivox_rss_url,book_title,book_id):
key = f"book_{book_id}"
# check if original file works
pocketCasts = uploadToPocketcasts(librivox_rss_url)
if(pocketCasts["status"] !='ok'):
# check if it is already in redis
cached_rss_url = r.get(key)
if cached_rss_url:
log.info(f"use the cached url {cached_rss_url}")
rss_url = cached_rss_url
else:
fixed_rss = fixRSSfile(librivox_rss_url)
log.info(f"generate a new url {fixed_rss}")
rss_url = writeGist(fixed_rss,f"librivox_{book_id}")
# store the gist url in redis
r.set(key,rss_url)
if(rss_url):
pocketCasts = uploadToPocketcasts(rss_url)
else:
# delete the gist url in redis
r.delete(key)
return {"status":"error","message":"Failed to upload to gist"}
return pocketCasts