-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
176 lines (137 loc) · 4.74 KB
/
server.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
from flask import Flask, jsonify, render_template, request, \
redirect, url_for, session
import MySQLdb
import random
import string
import requests
from config import secrets, Secret_Key, db
from flask.ext.script import Manager
from flask_oauth import OAuth
app = Flask(__name__)
manager = Manager(app)
GOOGLE_CLIENT_ID = secrets['GOOGLE_CLIENT_ID']
GOOGLE_CLIENT_SECRET = secrets['GOOGLE_CLIENT_SECRET']
REDIRECT_URI = secrets['REDIRECT_URI']
app.secret_key = Secret_Key
base_url = "https://www.google.com/accounts/"
authorize_url = "https://accounts.google.com/o/oauth2/auth"
request_token_params = {
'scope': 'https://www.googleapis.com/auth/userinfo.email',
'response_type': 'code'}
access_token_url = "https://accounts.google.com/o/oauth2/token"
oauth = OAuth()
google = oauth.remote_app('google',
base_url=base_url,
authorize_url=authorize_url,
request_token_url=None,
request_token_params=request_token_params,
access_token_url=access_token_url,
access_token_method='POST',
access_token_params={
'grant_type':
'authorization_code'},
consumer_key=GOOGLE_CLIENT_ID,
consumer_secret=GOOGLE_CLIENT_SECRET)
@app.route("/")
def main():
access_token = session.get('access_token')
if access_token is None:
return render_template('index.html')
access_token = access_token[0]
url = "https://www.googleapis.com/oauth2/v1/userinfo"
try:
req = requests.get(url,
params=dict(access_token=access_token)
).json()
session['uid'] = req['id']
except Exception:
return redirect(url_for("login"))
return render_template('home.html')
@app.route('/login')
def login():
callback = url_for('authorized', _external=True)
return google.authorize(callback=callback)
@app.route('/logout/')
def logout():
session.clear()
return redirect("/")
@app.route(REDIRECT_URI)
@google.authorized_handler
def authorized(resp):
access_token = resp['access_token']
session['access_token'] = access_token, ''
return redirect(url_for('main'))
@google.tokengetter
def get_access_token():
return session.get('access_token')
@app.route('/view_records/')
def view():
c, conn = connection()
query = "SELECT * FROM urls WHERE userid="
value = session['uid']
x = c.execute(query + value)
if x:
data = c.fetchall()
return str(str(data[0]))
conn.commit()
c.close()
conn.close()
return "View"
@app.route("/<shorturl>")
def renderUI(shorturl):
c, conn = connection()
query = "SELECT * FROM urls WHERE shorturl="
values = "'{0}'".format(str(shorturl))
x = c.execute(query + values)
if x:
data = c.fetchall()
url = data[0][2]
req = requests.get(url)
if 'x-frame-options' in req.headers:
return redirect(url)
return render_template("abc.html",
url=str(data[0][2]),
message=str(data[0][3]))
else:
return redirect(url_for("/"))
return render_template("abc.html")
@app.route("/api/", methods=['GET', 'POST'])
def create_entry():
if request.method == "POST":
if request.json:
data = request.json
sourceurl = data.get("url")
message = data.get("message")
c, conn = connection()
shorturl = randomword()
query = "INSERT into urls VALUES"
values = "('{0}','{1}','{2}','{3}')"\
.format(session['uid'], shorturl, sourceurl, message)
c.execute(query + values)
conn.commit()
c.close()
conn.close()
return jsonify(shorturl=shorturl)
return render_template("addurl.html")
@app.route("/api/<shorturl>")
def ShortUrl(shorturl):
c, conn = connection()
query = "SELECT * FROM url WHERE shorturl = "
values = "'{0}'".format(str(shorturl))
x = c.execute(query + values)
if x:
data = c.fetchall()
return jsonify(username=data[0][0], url=data[0][2], message=data[0][3])
else:
return "not Found", 404
@app.route("/geturl/", methods=['GET', 'POST'])
def geturl():
return render_template("geturl.html")
def connection():
conn = MySQLdb.connect(db["host"], db["user"], db["password"],
db["database"])
c = conn.cursor()
return c, conn
def randomword():
return ''.join(random.choice(string.lowercase) for i in range(7))
manager.run()