-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
95 lines (64 loc) · 2.19 KB
/
app.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
from flask import Flask,jsonify,request
from flask_cors import CORS,cross_origin
import feedparser
from markupsafe import escape
import os
import re
app=Flask(__name__)
CORS(app)
# app.config['SECRET_KEY']=ENV['SECRET_KEY']
titles=[] # will contain the title
descs=[] # will contain the description each item
links=[] # corresponding links will be stored here
dates=[] # each item's date will be stored in a particluar format
def cleanhtml(raw_html):
cleanr = re.compile('<.*?>')
cleantext = re.sub(cleanr, '', raw_html)
return cleantext
# for getting correct links
def unique(sequence):
seen = set()
return [x for x in sequence if not (x in seen or seen.add(x))]
#creating embed url
def create_embed_link(pod_url):
end=pod_url.partition('/episodes')
final = end[0]+'/embed' +end[1] + end[2]
return final
@app.route('/') #default root route
@cross_origin()
def home():
result={"message":"Welcome to the unofficial anchor.fm API built to test endpoints"}
return jsonify(result)
@app.route('/path/<path:rss_url>')
@cross_origin()
def index(rss_url):
rss=rss_url
parser= feedparser.parse(rss)
for i in range (len(parser.entries)):
title=parser.entries[i].title # parse the title field
titles.append(title)
date=parser.entries[i].published[5:16] # parse the date field
dates.append(date)
link=parser.entries[i].link # parse the link field
flink=create_embed_link(link)
links.append(flink)
desc=parser.entries[i].description # parse the description field
clean=cleanhtml(desc)
f=" ".join(clean.split())
descs.append(f)
linksss=unique(links)
#getting content in correct order before saving
linksss.reverse()
titles.reverse()
descs.reverse()
dates.reverse()
len1=len(linksss)
#storing data of each podcast in results
results=[]
for i in range(len1):
result={"episode":i ,"iframelink":linksss[i],"title":titles[i],"description":descs[i],"date_of_publishing":dates[i]}
results.append(result)
# print(results)
return jsonify(results)
if __name__=='__main__':
app.run(debug=False)