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

Commit 4be7c8d

Browse files
committed
Add reverse lookup by word
1 parent cca5bc0 commit 4be7c8d

File tree

2 files changed

+37
-12
lines changed

2 files changed

+37
-12
lines changed

stocktwits_wrapper.py

+9-9
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
import html
33
import logging
44
import os
5-
import time
65

76
from flask import abort, session
87
import requests
@@ -52,8 +51,6 @@ def get_ticker(self, ticker):
5251
def update_ticker(self, ticker):
5352
""" update the ticker in the db """
5453

55-
start_time = time.time()
56-
5754
since_param = self.__get_pagination_param(ticker)
5855

5956
# TODO: make a system account on ST and use for all automated calls.
@@ -83,12 +80,6 @@ def update_ticker(self, ticker):
8380

8481
all_words = [(word) for message in simple_messages for word in message.split()]
8582
self.__update_word_frequencies(ticker, all_words)
86-
87-
end_time = time.time()
88-
total_time = (end_time - start_time)
89-
logging.warn('%s updated in %d seconds. Requests left: %s' % (ticker,
90-
total_time,
91-
session.get('rate_remaining', 'none?')))
9283
return
9384

9485
@classmethod
@@ -167,6 +158,15 @@ def __query_ticker(self, ticker):
167158
self.mysql_conn.commit()
168159
return query_result
169160

161+
@classmethod
162+
def __query_word(self, word):
163+
self.__check_connection()
164+
with closing(self.mysql_conn.cursor()) as cursor:
165+
cursor.callproc('query_word', (word,))
166+
query_result = next(cursor.stored_results()).fetchall()
167+
self.mysql_conn.commit()
168+
return query_result
169+
170170
@classmethod
171171
def __update_word_frequencies(self, ticker, word_map):
172172
self.__check_connection()

supplemental_materials/create_wordtwits_db.sql

+28-3
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ CREATE TABLE `tickers` (
3535
UNIQUE KEY `ticker_UNIQUE` (`ticker`),
3636
UNIQUE KEY `id_UNIQUE` (`id`),
3737
UNIQUE KEY `company_name_UNIQUE` (`company_name`)
38-
) ENGINE=InnoDB AUTO_INCREMENT=217923 DEFAULT CHARSET=latin1 COMMENT='Table for all tickers';
38+
) ENGINE=InnoDB AUTO_INCREMENT=217928 DEFAULT CHARSET=latin1 COMMENT='Table for all tickers';
3939
/*!40101 SET character_set_client = @saved_cs_client */;
4040

4141
--
@@ -71,7 +71,7 @@ CREATE TABLE `word_frequencies` (
7171
UNIQUE KEY `uq_ticker_idx` (`ticker_id`,`word`),
7272
KEY `fk_ticker_id_idx` (`ticker_id`),
7373
CONSTRAINT `fk_ticker_id` FOREIGN KEY (`ticker_id`) REFERENCES `tickers` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
74-
) ENGINE=InnoDB AUTO_INCREMENT=18420 DEFAULT CHARSET=latin1 COMMENT='Word Frequency fact table';
74+
) ENGINE=InnoDB AUTO_INCREMENT=18425 DEFAULT CHARSET=latin1 COMMENT='Word Frequency fact table';
7575
/*!40101 SET character_set_client = @saved_cs_client */;
7676

7777
--
@@ -117,6 +117,31 @@ BEGIN
117117
ORDER BY frequency DESC
118118
LIMIT 50;
119119

120+
END ;;
121+
DELIMITER ;
122+
/*!50003 SET sql_mode = @saved_sql_mode */ ;
123+
/*!50003 SET character_set_client = @saved_cs_client */ ;
124+
/*!50003 SET character_set_results = @saved_cs_results */ ;
125+
/*!50003 SET collation_connection = @saved_col_connection */ ;
126+
/*!50003 DROP PROCEDURE IF EXISTS `query_word` */;
127+
/*!50003 SET @saved_cs_client = @@character_set_client */ ;
128+
/*!50003 SET @saved_cs_results = @@character_set_results */ ;
129+
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
130+
/*!50003 SET character_set_client = utf8 */ ;
131+
/*!50003 SET character_set_results = utf8 */ ;
132+
/*!50003 SET collation_connection = utf8_general_ci */ ;
133+
/*!50003 SET @saved_sql_mode = @@sql_mode */ ;
134+
/*!50003 SET sql_mode = 'NO_ENGINE_SUBSTITUTION' */ ;
135+
DELIMITER ;;
136+
CREATE DEFINER=`tdoshea90`@`%` PROCEDURE `query_word`(IN word_arg CHAR(5))
137+
BEGIN
138+
139+
SELECT tickers.ticker, word_frequencies.frequency
140+
FROM word_frequencies
141+
INNER JOIN tickers ON word_frequencies.ticker_id = tickers.id
142+
WHERE word_frequencies.word=word_arg AND frequency > 1
143+
ORDER BY frequency DESC;
144+
120145
END ;;
121146
DELIMITER ;
122147
/*!50003 SET sql_mode = @saved_sql_mode */ ;
@@ -210,4 +235,4 @@ DELIMITER ;
210235
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
211236
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
212237

213-
-- Dump completed on 2016-11-11 0:53:56
238+
-- Dump completed on 2016-11-11 1:15:40

0 commit comments

Comments
 (0)