-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
94 lines (80 loc) · 2.71 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
from flask import Flask, session, request, redirect, render_template
from os import environ
import logging
import uuid
from urllib.parse import urlencode, urlparse, parse_qs
from base64 import b64encode
from json import dumps
from tumblr import Tumblr, RateLimitException
app = Flask(__name__)
app.secret_key = environ.get("FLASK_SECRET_KEY")
@app.route('/')
def index():
"""
index
"""
return render_template("intro.html")
@app.route("/list_blogs")
def list_blogs():
try:
tumblr = Tumblr(token=session.get("tumblr_token"))
blog_list = tumblr.user_blogs()
non_mutual_followers = []
for blog in blog_list:
followers = tumblr.blog_followers(blog)
new = [f for f in followers if f["following"] != True]
for follower in new:
follower["follows"] = blog.name
non_mutual_followers += new
sus_blogs = {b.name: [] for b in blog_list}
for follower in non_mutual_followers:
info = tumblr.public_blog_post_count(blog_name=follower["name"])
if info[0] == 0:
follower["avatar"] = info[1]
follower["report_json_str"] = dumps({
"post":None,
"urlreporting":f"https://www.tumblr.com/{follower['name']}",
"tumblelog":follower["name"],
"context":"blog"
})
sus_blogs[follower["follows"]].append(follower)
return render_template("blog_list.html", blog_list=blog_list, sus_blogs=sus_blogs)
except RateLimitException:
return render_template("rate_limit.html")
@app.route('/auth')
def auth_handler():
"""
auth
"""
parsed_url = urlparse(request.url)
qs = parse_qs(parsed_url.query)
state = session.get("state")
returned_state = qs.get("state")
returned_code = qs.get("code")[0]
tumblr = Tumblr()
tumblr.authenticate(returned_code)
logging.error(str(tumblr))
session["tumblr_token"] = tumblr.token.to_dict()
return render_template("auth_success.html")
def make_url_params(writeable: bool = False):
scope = 'basic write' if writeable else 'basic'
state = uuid.uuid4()
session['state'] = state
params = {
'response_type': 'code',
'client_id': environ.get('TUMBLR_CONSUMER_KEY'),
'redirect_uri': environ.get('REDIRECT_URI'),
'scope': scope,
'approval_prompt': 'auto',
'state': state,
}
return params
@app.route('/initiate-auth')
def auth_initiator():
"""
init auth
"""
params = make_url_params(writeable=False)
return redirect(f"https://www.tumblr.com/oauth2/authorize?{urlencode(params)}")
if __name__ == '__main__':
app.run()