-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.py
95 lines (72 loc) · 2.71 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
# -*- coding: utf-8 -*-
import random
from flask import Flask, render_template, request, session, redirect
import requests
import os
from user_agents import parse
from yo import Yo
app = Flask(__name__, static_url_path='')
app.config.from_object(__name__)
yo = Yo(client_id=os.environ.get('CLIENT_ID'),
client_secret=os.environ.get('CLIENT_SECRET'),
redirect_uri=os.environ.get('REDIRECT_URI'))
if not yo.client_id or \
not yo.client_secret or \
not yo.redirect_uri:
print 'Yo, you need to set your client straight: https://dashboard.justyo.co'
exit(1)
def fetch_random_gif(keyword):
giphy_result = requests.get('http://api.giphy.com/v1/gifs/search?q=%s&api_key=dc6zaTOxFJmzC' % keyword)
data = giphy_result.json()['data']
urls = [item['images']['original']['url'] for item in data]
random_url = random.choice(urls)
return random_url
@app.route('/')
def home():
keyword = os.environ.get('GIPHY_KEYWORD', 'pug')
random_gif_url = fetch_random_gif(keyword)
user_agent = parse(request.headers.get('User-Agent'))
if session.get('access_token'):
yo.access_token = session.get('access_token')
contacts = yo.fetch_contacts()
if contacts is None:
session.pop('access_token', None)
return redirect('/')
colors = [
'turquoise',
'emerald',
'peter',
'asphalt',
'green',
'sunflower',
'belize',
'wisteria'
]
return render_template('index.html', contacts=contacts, colors=colors, background_url=random_gif_url,
is_mobile=user_agent.is_mobile)
else:
url_for_authorize = yo.url_for_authorize()
return render_template('login.html', url_for_authorize=url_for_authorize, background_url=random_gif_url,
is_mobile=user_agent.is_mobile)
@app.route('/yo', methods=['POST'])
def send_yo():
yo.access_token = session.get('access_token')
response = yo.send(username=request.form.get('username'),
link=request.form.get('link'),
response_pair=u'🐶.😂')
print response.status_code, response.text
return response.text
@app.route('/logout')
def logout():
session.pop('access_token', None)
return redirect('/')
@app.route('/authorized')
def authorized():
access_token = yo.exchange_code_for_access_token(request.args.get('code'))
session['access_token'] = access_token
return redirect('/')
if __name__ == "__main__":
app.debug = True
app.secret_key = os.environ.get('SECRET_KEY')
port = int(os.environ.get('PORT', 8000))
app.run(host="0.0.0.0", port=port)