Last active
February 22, 2024 15:27
-
-
Save financial-python/19e120b2a989c993031343bd8b986e53 to your computer and use it in GitHub Desktop.
Get Stock Tickers from Twitter using Python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import tweepy as tw #An easy-to-use Python library for accessing the Twitter API | |
import string | |
from collections import Counter | |
TWITTER_CONSUMER_KEY = '[FROM_TWITTER_DEVELOPER_CONSOLE]' | |
TWITTER_CONSUMER_SECRET = '[FROM_TWITTER_DEVELOPER_CONSOLE]' | |
TWITTER_ACCESS_TOKEN = '[FROM_TWITTER_DEVELOPER_CONSOLE]' | |
TWITTER_ACCESS_TOKEN_SECRET = '[FROM_TWITTER_DEVELOPER_CONSOLE]' | |
TWITTER_LIST_ID = '[LIST_ID]' #get from the url of the twitter list: https://twitter.com/i/lists/[LIST_ID] | |
TWEET_COUNT = 100 #however many you want to retrieve | |
#method to check if a word contains a number or not | |
def containsNumber(word): | |
for character in word: | |
if character.isdigit(): | |
return True | |
return False | |
#method to remove all punc | |
def remove_punctuation(word): | |
punc_remove_list = [char for char in word if char not in string.punctuation] #[K,E,R,N] | |
word_no_punc = ''.join(punc_remove_list) | |
return word_no_punc | |
def main(): | |
#authenticate twitter | |
twitter_auth = tw.OAuthHandler(TWITTER_CONSUMER_KEY, TWITTER_CONSUMER_SECRET) | |
twitter_auth.set_access_token(TWITTER_ACCESS_TOKEN, TWITTER_ACCESS_TOKEN_SECRET) | |
#create twitter api object | |
twitter_api = tw.API(twitter_auth, wait_on_rate_limit=True) | |
#get tweets from twitter api | |
#https://docs.tweepy.org/en/stable/api.html#tweepy.API.list_timeline | |
tweets = twitter_api.list_timeline(list_id=TWITTER_LIST_ID, count=TWEET_COUNT) | |
#loop through tweets and add stock tickers to a list | |
twitter_stocks = [] | |
for tweet in tweets: | |
text = tweet._json["text"] #the text of the tweet | |
tweet_words = text.split() #split the text into a list of strings | |
#loop through each word in the list | |
for word in tweet_words: | |
#check if word meets the criteria of a stock ticker | |
if word.startswith('$') and word.isupper() and not containsNumber(word): | |
word = remove_punctuation(word) #remove punctuation from the stock ticker | |
twitter_stocks.append(word) | |
#check how often a stock ticker appears in our list | |
ticker_count = [] | |
count = Counter(twitter_stocks) #creates map of ticker to # of appearances in list | |
for key, value in count.items(): | |
if value >= 3: #only take the tickers that appear at least 3 times | |
ticker_count.append({'ticker': key, 'count': value}) | |
#order the list of ticker/count objects by the count DESC | |
ticker_count = sorted(ticker_count, key=lambda x: x['count'], reverse=True) | |
#put the ticker symbol in a final list of tickers | |
final_ticker_list = [] | |
for ticker_object in ticker_count: | |
final_ticker_list.append(ticker_object['ticker']) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment