-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
197 lines (162 loc) · 6.5 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
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
from flask import Flask, jsonify, render_template, request, redirect, url_for, session, g, flash
from flaskext.oauth import OAuth
from wrappers import TwitterApiWrapper
from sqlalchemy import create_engine, Column, Integer, String
from sqlalchemy.orm import scoped_session, sessionmaker
from sqlalchemy.ext.declarative import declarative_base
# configuration
DATABASE_URI = 'sqlite:////tmp/flask-oauth.db'
SECRET_KEY = '3d0d40c0-3b00-11e1-a1e5-00264a1c552e'
app = Flask(__name__)
app.secret_key = SECRET_KEY
oauth = OAuth()
#twitter = oauth.remote_app('twitter',
# base_url='http://api.twitter.com/1/',
# request_token_url='http://api.twitter.com/oauth/request_token',
# access_token_url='http://api.twitter.com/oauth/access_token',
# authorize_url='http://api.twitter.com/oauth/authenticate',
# consumer_key='zYZS8rzUAxhPFRcKBenhPQ',
# consumer_secret='AgXBoAjx3QuBtUQ60bEWFupNYGLITa1dTY3ljWw'
# )
twitter = oauth.remote_app('twitter',
# unless absolute urls are used to make requests, this will be added
# before all URLs. This is also true for request_token_url and others.
base_url='https://api.twitter.com/1/',
# where flask should look for new request tokens
request_token_url='https://api.twitter.com/oauth/request_token',
# where flask should exchange the token with the remote application
access_token_url='https://api.twitter.com/oauth/access_token',
# twitter knows two authorizatiom URLs. /authorize and /authenticate.
# they mostly work the same, but for sign on /authenticate is
# expected because this will give the user a slightly different
# user interface on the twitter side.
authorize_url='https://api.twitter.com/oauth/authorize',
# the consumer keys from the twitter application registry.
consumer_key='bCNMPo60kdOe1kMQ6Bpk5g',
consumer_secret='MlUmEsYDmW9Pd8DqJNQK5lptEYGGOmBZnQGWNpQpjI'
)
# setup sqlalchemy
engine = create_engine(DATABASE_URI)
db_session = scoped_session(sessionmaker(autocommit=False,
autoflush=False,
bind=engine))
Base = declarative_base()
Base.query = db_session.query_property()
def init_db():
Base.metadata.create_all(bind=engine)
# models = (InstanceInfo, InstanceSegment)
# engine = get_engine()
# for model in models:
# model.metadata.create_all(engine)
class User(Base):
__tablename__ = 'users'
id = Column('user_id', Integer, primary_key=True)
name = Column(String(60))
oauth_token = Column(String(200))
oauth_secret = Column(String(200))
def __init__(self, name):
self.name = name
@app.before_request
def before_request():
g.user = None
if 'user_id' in session:
g.user = User.query.get(session['user_id'])
@app.after_request
def after_request(response):
db_session.remove()
return response
@twitter.tokengetter
def get_twitter_token():
"""This is used by the API to look for the auth token and secret
it should use for API calls. During the authorization handshake
a temporary set of token and secret is used, but afterwards this
function has to return the token and secret. If you don't want
to store this in the database, consider putting it into the
session instead.
"""
user = g.user
if user is not None:
return user.oauth_token, user.oauth_secret
@app.route('/')
def index():
if not g.user:
return redirect('/login')
wrapper = TwitterApiWrapper()
tweets = []
if g.user is not None:
resp = twitter.get('favorites.json')
if resp.status == 200:
tweets = wrapper.favorites(resp.data)
return render_template('index.html', twits=tweets)
#return render_template('index.html', twits=wrapper.favorites())
@app.route('/unfavorite')
def unfavorite():
wrapper = TwitterApiWrapper(
app.config['TWITTER_CONSUMER_KEY'],
app.config['TWITTER_CONSUMER_SECRET'],
app.config['TWITTER_ACCESS_TOKEN'],
app.config['TWITTER_ACCESS_TOKEN_SECRET'])
id = request.args.get('id', '')
if id:
wrapper.unfavorite(id)
return jsonify(result='ok')
@app.route('/retweet')
def retweet():
wrapper = TwitterApiWrapper(
app.config['TWITTER_CONSUMER_KEY'],
app.config['TWITTER_CONSUMER_SECRET'],
app.config['TWITTER_ACCESS_TOKEN'],
app.config['TWITTER_ACCESS_TOKEN_SECRET'])
id = request.args.get('id', '')
if id:
wrapper.retweet(id)
return jsonify(result='ok')
@app.route('/login')
def login():
return render_template('login.html')
@app.route('/login/twitter')
def twitter_login():
return twitter.authorize(callback=url_for('oauth_authorized',
next=request.args.get('next') or '/'))
@app.route('/logout/twitter')
def twitter_logout():
session.pop('user_id', None)
flash('You were signed out')
return redirect(request.referrer or url_for('index'))
@app.route('/oauth-authorized')
@twitter.authorized_handler
def oauth_authorized(resp):
"""Called after authorization. After this function finished handling,
the OAuth information is removed from the session again. When this
happened, the tokengetter from above is used to retrieve the oauth
token and secret.
Because the remote application could have re-authorized the application
it is necessary to update the values in the database.
If the application redirected back after denying, the response passed
to the function will be `None`. Otherwise a dictionary with the values
the application submitted. Note that Twitter itself does not really
redirect back unless the user clicks on the application name.
"""
next_url = request.args.get('next') or url_for('index')
if resp is None:
flash(u'You denied the request to sign in.')
return redirect(next_url)
user = User.query.filter_by(name=resp['screen_name']).first()
# user never signed on
if user is None:
user = User(resp['screen_name'])
db_session.add(user)
# in any case we update the authenciation token in the db
# In case the user temporarily revoked access we will have
# new tokens here.
user.oauth_token = resp['oauth_token']
user.oauth_secret = resp['oauth_token_secret']
db_session.commit()
session['user_id'] = user.id
flash('You were signed in')
return redirect(next_url)
if __name__ == '__main__':
app.debug = True
app.config.from_object('config.twitter.TwitterConfig')
init_db()
app.run()