Skip to content
This repository was archived by the owner on Jun 17, 2024. It is now read-only.

Commit ec763a1

Browse files
committed
Use auth token in /stream requests to get 400 requests an hour instead of 200
1 parent 445554a commit ec763a1

File tree

3 files changed

+5
-20
lines changed

3 files changed

+5
-20
lines changed

application.py

+2-12
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
2-
import logging
31
import os
42

53
from flask import Flask, render_template, request, redirect, url_for, session
@@ -10,9 +8,7 @@
108

119
application = Flask(__name__)
1210
app = application
13-
#app.secret_key = os.environ.get('APP_SECRET_KEY')
14-
app.secret_key = os.urandom(32)
15-
#app.permanent_session_lifetime = timedelta(days=1)
11+
app.secret_key = os.environ.get('APP_SECRET_KEY')
1612

1713
stwrapper = StockTwitsWrapper()
1814
stwrapper.compile_regex_patterns()
@@ -52,18 +48,12 @@ def auth_redirect_uri():
5248
session['user_id'] = token_response.user_id
5349
session['username'] = token_response.username
5450

55-
logging.info('>>>>>>>>>>>>>>>>>>>>>>>>>>>>')
56-
logging.error(token_response.access_token)
57-
logging.warn('>>>>>>>>>>>>>>>>>>>>>>>>>>>>')
58-
5951
return redirect(request.url_root) # go back home after successful auth
6052

6153

62-
# Not actually using any ST endpoints that require auth yet
54+
# Using auth token boosts request limit to 400
6355
@app.before_request
6456
def check_session():
65-
#session.permanent = True
66-
# need to comment out this block and push to prod to use oauth on localhost
6757
if '/auth_redirect_uri/' != request.path:
6858
if 'user_id' not in session or 'access_token' not in session:
6959
auth_code_url = authwrapper.get_auth_code_url(request.url_root)

auth_wrapper.py

-4
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ class AuthWrapper:
99

1010
client_id = '7cc80ecf361d0f13'
1111
auth_redirect_postfix = 'auth_redirect_uri/'
12-
# uncomment along with other 2 lines for local oauth testing
13-
#bs_auth_redirect_url = 'https://stocktwitsanalyzer-env.us-west-2.elasticbeanstalk.com/'
1412

1513
@classmethod
1614
def get_auth_code_url(self, auth_redirect_root):
@@ -19,7 +17,6 @@ def get_auth_code_url(self, auth_redirect_root):
1917
auth_code_params = dict(
2018
response_type='code',
2119
redirect_uri=auth_redirect_root + self.auth_redirect_postfix,
22-
#redirect_uri=self.bs_auth_redirect_url + self.auth_redirect_postfix,
2320
client_id=self.client_id,
2421
scope='read'
2522
)
@@ -35,7 +32,6 @@ def get_auth_token(self, auth_code, auth_redirect_root):
3532
auth_token_params = dict(
3633
code=auth_code,
3734
redirect_uri=auth_redirect_root,
38-
#redirect_uri=self.bs_auth_redirect_url + self.auth_redirect_postfix,
3935
client_id=self.client_id,
4036
client_secret=os.environ.get('ST_API_KEY'),
4137
grant_type='authorization_code'

stocktwits_wrapper.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,13 @@ def get_ticker(self, ticker):
5454

5555
get_ticker_url = 'https://api.stocktwits.com/api/2/streams/symbol/'
5656
url_postfix = '.json'
57+
if 'access_token' in session:
58+
url_postfix = '%s?access_token=%s' % (url_postfix, session['access_token'])
5759

5860
# call ST api
5961
request_url = get_ticker_url + ticker + url_postfix
6062

61-
# TODO: every hour use ST 'cursor' parameter to only get newest messages
63+
# TODO: use 'cursor' parameter to only get newest messages
6264

6365
response = requests.get(request_url)
6466
response_json = response.json()
@@ -69,9 +71,6 @@ def get_ticker(self, ticker):
6971
session['rate_remaining'] = rate_remaining
7072
session['rate_reset'] = rate_reset # returned in true UTC time
7173

72-
# print(session['rate_remaining'])
73-
# print(session['rate_reset'])
74-
7574
self.__check_response_code(response_json, rate_remaining)
7675

7776
symbol = response_json['symbol']['symbol']

0 commit comments

Comments
 (0)