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

Commit 0ddcb64

Browse files
committed
Pagination working
1 parent ec763a1 commit 0ddcb64

File tree

2 files changed

+79
-18
lines changed

2 files changed

+79
-18
lines changed

create_wordtwits_db.sql

+48-4
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,12 @@ CREATE TABLE `tickers` (
3030
`id` int(11) NOT NULL AUTO_INCREMENT,
3131
`sector` varchar(64) DEFAULT NULL,
3232
`industry` varchar(64) DEFAULT NULL,
33+
`last_message` int(11) NOT NULL DEFAULT '-1',
3334
PRIMARY KEY (`id`),
3435
UNIQUE KEY `ticker_UNIQUE` (`ticker`),
3536
UNIQUE KEY `id_UNIQUE` (`id`),
3637
UNIQUE KEY `company_name_UNIQUE` (`company_name`)
37-
) ENGINE=InnoDB AUTO_INCREMENT=13258 DEFAULT CHARSET=latin1 COMMENT='Dimension table for tickers';
38+
) ENGINE=InnoDB AUTO_INCREMENT=13275 DEFAULT CHARSET=latin1 COMMENT='Table for all tickers';
3839
/*!40101 SET character_set_client = @saved_cs_client */;
3940

4041
--
@@ -54,12 +55,55 @@ CREATE TABLE `word_frequencies` (
5455
UNIQUE KEY `uq_ticker_idx` (`ticker_id`,`word`),
5556
KEY `fk_ticker_id_idx` (`ticker_id`),
5657
CONSTRAINT `fk_ticker_id` FOREIGN KEY (`ticker_id`) REFERENCES `tickers` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
57-
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=latin1 COMMENT='Word Frequency fact table';
58+
) ENGINE=InnoDB AUTO_INCREMENT=27 DEFAULT CHARSET=latin1 COMMENT='Word Frequency fact table';
5859
/*!40101 SET character_set_client = @saved_cs_client */;
5960

6061
--
6162
-- Dumping routines for database 'WordTwitsDatabase'
6263
--
64+
/*!50003 DROP PROCEDURE IF EXISTS `get_last_message` */;
65+
/*!50003 SET @saved_cs_client = @@character_set_client */ ;
66+
/*!50003 SET @saved_cs_results = @@character_set_results */ ;
67+
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
68+
/*!50003 SET character_set_client = utf8 */ ;
69+
/*!50003 SET character_set_results = utf8 */ ;
70+
/*!50003 SET collation_connection = utf8_general_ci */ ;
71+
/*!50003 SET @saved_sql_mode = @@sql_mode */ ;
72+
/*!50003 SET sql_mode = 'NO_ENGINE_SUBSTITUTION' */ ;
73+
DELIMITER ;;
74+
CREATE DEFINER=`tdoshea90`@`%` PROCEDURE `get_last_message`(IN ticker_arg CHAR(5), OUT last_message_out INT)
75+
BEGIN
76+
77+
SET last_message_out = (SELECT IFNULL((SELECT last_message FROM tickers WHERE ticker=ticker_arg), -1));
78+
79+
END ;;
80+
DELIMITER ;
81+
/*!50003 SET sql_mode = @saved_sql_mode */ ;
82+
/*!50003 SET character_set_client = @saved_cs_client */ ;
83+
/*!50003 SET character_set_results = @saved_cs_results */ ;
84+
/*!50003 SET collation_connection = @saved_col_connection */ ;
85+
/*!50003 DROP PROCEDURE IF EXISTS `update_last_message` */;
86+
/*!50003 SET @saved_cs_client = @@character_set_client */ ;
87+
/*!50003 SET @saved_cs_results = @@character_set_results */ ;
88+
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
89+
/*!50003 SET character_set_client = utf8 */ ;
90+
/*!50003 SET character_set_results = utf8 */ ;
91+
/*!50003 SET collation_connection = utf8_general_ci */ ;
92+
/*!50003 SET @saved_sql_mode = @@sql_mode */ ;
93+
/*!50003 SET sql_mode = 'NO_ENGINE_SUBSTITUTION' */ ;
94+
DELIMITER ;;
95+
CREATE DEFINER=`tdoshea90`@`%` PROCEDURE `update_last_message`(IN ticker_arg CHAR(5), IN last_message_arg INT)
96+
BEGIN
97+
98+
UPDATE tickers SET last_message=last_message_arg
99+
WHERE ticker=ticker_arg;
100+
101+
END ;;
102+
DELIMITER ;
103+
/*!50003 SET sql_mode = @saved_sql_mode */ ;
104+
/*!50003 SET character_set_client = @saved_cs_client */ ;
105+
/*!50003 SET character_set_results = @saved_cs_results */ ;
106+
/*!50003 SET collation_connection = @saved_col_connection */ ;
63107
/*!50003 DROP PROCEDURE IF EXISTS `update_word_frequencies` */;
64108
/*!50003 SET @saved_cs_client = @@character_set_client */ ;
65109
/*!50003 SET @saved_cs_results = @@character_set_results */ ;
@@ -70,7 +114,7 @@ CREATE TABLE `word_frequencies` (
70114
/*!50003 SET @saved_sql_mode = @@sql_mode */ ;
71115
/*!50003 SET sql_mode = 'NO_ENGINE_SUBSTITUTION' */ ;
72116
DELIMITER ;;
73-
CREATE PROCEDURE `update_word_frequencies`(ticker_arg CHAR(5), word_arg VARCHAR(32), count_arg INT)
117+
CREATE DEFINER=`tdoshea90`@`%` PROCEDURE `update_word_frequencies`(IN ticker_arg CHAR(5), IN word_arg VARCHAR(32), IN count_arg INT)
74118
BEGIN
75119

76120
# 1. Get ticker or insert
@@ -97,4 +141,4 @@ DELIMITER ;
97141
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
98142
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
99143

100-
-- Dump completed on 2016-11-06 12:57:07
144+
-- Dump completed

stocktwits_wrapper.py

+31-14
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,20 @@ def get_ticker(self, ticker):
5353
self.__check_rate_reset()
5454

5555
get_ticker_url = 'https://api.stocktwits.com/api/2/streams/symbol/'
56-
url_postfix = '.json'
56+
url_postfix = '.json?'
57+
pagination_since = self.__get_last_message(ticker)
58+
# only time we don't have access_token is on localhost so kind of an unnecessary check
5759
if 'access_token' in session:
58-
url_postfix = '%s?access_token=%s' % (url_postfix, session['access_token'])
60+
url_postfix = '%s&access_token=%s' % (url_postfix, session['access_token'])
5961

60-
# call ST api
61-
request_url = get_ticker_url + ticker + url_postfix
62+
# TODO: only messages 'since' show up.
63+
# TODO: store last 30 messages in DB or always fetch last 30 and do fake pagination here?
64+
# TODO: or use one of the embedded widget things
65+
if pagination_since != -1:
66+
url_postfix = '%s&since=%s' % (url_postfix, str(pagination_since))
6267

63-
# TODO: use 'cursor' parameter to only get newest messages
68+
# call ST api
69+
request_url = '%s%s%s' % (get_ticker_url, ticker, url_postfix)
6470

6571
response = requests.get(request_url)
6672
response_json = response.json()
@@ -78,8 +84,16 @@ def get_ticker(self, ticker):
7884

7985
st_compliant_posts = []
8086
simple_messages = []
87+
# index = 1
8188
for message in response_json['messages']:
8289

90+
# TODO: first message is new 'last_message' to use for pagination
91+
# TODO: turn this on once automation is set up
92+
# self.__update_last_message(ticker, message['id'])
93+
94+
# print('%d:%s:%s' % (index, message['id'], message['body']))
95+
# index = index + 1
96+
8397
# 1. https://stocktwits.com/developers/docs/display_requirement
8498
st_compliant_posts.append(self.__build_st_compliant_post(message))
8599

@@ -94,7 +108,7 @@ def get_ticker(self, ticker):
94108
word_map = self.__build_word_map(simple_messages)
95109

96110
# TODO: find the right place for this
97-
# self.__update_db('AMZN', 'DERP', 10)
111+
# self.__update_word_frequencies('AMZN', 'DERP', 10)
98112

99113
# TODO: send to DB and update, return results from DB and st_compliant_posts.
100114

@@ -192,22 +206,25 @@ def __check_response_code(self, response_json, rate_remaining):
192206
abort(response_code)
193207

194208
@classmethod
195-
def __update_db(self, ticker, word, count):
209+
def __update_word_frequencies(self, ticker, word, count):
196210
with closing(self.mysql_conn.cursor()) as cursor:
197211
# ticker, word, count
198212
cursor.callproc('update_word_frequencies', (ticker, word, count))
199213
self.mysql_conn.commit()
200214

201215
@classmethod
202-
def __query_db(self):
203-
query = 'SELECT tickers.ticker, word_frequencies.word, word_frequencies.frequency \
204-
FROM word_frequencies \
205-
INNER JOIN tickers ON word_frequencies.ticker_id = tickers.id;'
216+
def __get_last_message(self, ticker):
217+
with closing(self.mysql_conn.cursor()) as cursor:
218+
# ticker, last_message return value
219+
result_args = cursor.callproc('get_last_message', (ticker, 0))
220+
return result_args[1]
206221

222+
@classmethod
223+
def __update_last_message(self, ticker, last_message):
207224
with closing(self.mysql_conn.cursor()) as cursor:
208-
cursor.execute(query)
209-
for (ticker, word, frequency) in cursor:
210-
print('%s\t%s\t%s' % (ticker, word, frequency))
225+
# ticker, last_message
226+
cursor.callproc('update_last_message', (ticker, last_message))
227+
self.mysql_conn.commit()
211228

212229

213230
class GetTickerResponse:

0 commit comments

Comments
 (0)