-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
51 lines (37 loc) · 1.73 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
from flask import Flask,render_template,request, abort, jsonify,Markup,make_response
import requests
app = Flask(__name__)
@app.errorhandler(404)
def resource_not_found(e):
return render_template("404.html.jinja"),404
@app.route('/', methods=['GET'])
def index():
return render_template('index.html.jinja'),200
def parse_content(content):
content = content.decode()
content = content.replace(r"\n", "")
content = content[16:].replace('\\','').replace("“","").replace("”","")
content = content[:-2].replace("—"," - ")
content = content[:-6]
content = content.split("</h3> <br/>")[1]
content = content[:-8]
content = content.split(" <br/> <br/> ")[0]
content = content.replace(" - "," <br/> <style> #author { text-align: right; }</style> <div id='author'> - ")
content = content + "</div>"
return content
@app.route('/getbadge', methods=['GET'])
def getbadge():
try:
goodReadsProfileUrl = request.args.get('goodReadsUrl')
goodReadsprofileId = goodReadsProfileUrl.split('/')[-1]
goodReadsQuotesUrl = "https://www.goodreads.com/quotes/widget/" +goodReadsProfileUrl.split('/')[-1]
content = requests.get(goodReadsQuotesUrl).content
content = parse_content(content=content)
template = render_template('badge.svg.jinja',content=content,goodReadsQuotesUrl=goodReadsQuotesUrl,goodReadsProfileUrl=goodReadsProfileUrl)
response = make_response(template)
response.headers["Content-Type"] = "image/svg+xml"
response.headers["charset"] = "utf-8"
response._status_code = 200
return response
except Exception as e:
abort(404, description="Resource not found")