Skip to content

Instantly share code, notes, and snippets.

@Xzenia
Last active March 19, 2020 16:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Xzenia/a98629e9daefc888c9ddb4f08eac781d to your computer and use it in GitHub Desktop.
Save Xzenia/a98629e9daefc888c9ddb4f08eac781d to your computer and use it in GitHub Desktop.
from twython import Twython
import json
import argparse
from bs4 import BeautifulSoup
class Tweet():
screen_name = None
display_name = None
date = None
text = None
favorite_count = None
url = None
def retrieve_tweets(search_string, result_count, popular, lang):
#Open the json file containing the Consumer Key and Secret.
#You may need to get that one from Twitter themselves...or not.
with open("twitter_credentials.json", "r") as file:
credentials = json.load(file)
#credentials now represents the json file that was retrieved.
#Now it can be used to retrieve the Consumer key and secret as if it was an array and put it in Twython.
python_tweets = Twython(credentials['CONSUMER_KEY'], credentials["CONSUMER_SECRET"])
#Checks if -p or --popular was inputted
if (popular == True):
result_type = 'popular'
else:
result_type = 'new'
#Form the query.
query_eng = {'q' : search_string,
'result_type' : result_type,
'count' : result_count,
'lang' : lang
}
tweetList = []
#Gathering tweets.
for status in python_tweets.search(**query_eng, tweet_mode='extended')['statuses']:
tweet = Tweet()
tweet.display_name = status['user']['name']
tweet.screen_name = status['user']['screen_name']
tweet.date = status['created_at']
tweet.favorite_count = status['favorite_count']
tweet_html = python_tweets.html_for_tweet(status,use_display_url=True)
parsed_html = BeautifulSoup(tweet_html, features='lxml')
tweet.text = parsed_html.getText()
tweet.url = f"https://twitter.com/{status['user']['screen_name']}/status/{status['id']}"
tweetList.append(tweet)
return tweetList
def get_args():
parser = argparse.ArgumentParser(description = 'Retrieves tweets related to provided key word.')
parser.add_argument( '-s', '--search', help='keyword to search', default=False)
parser.add_argument( '-c', '--count', help='number of results that will appear [10 by default]', default=False)
parser.add_argument( '-p', '--popular', help='whether or not the script will get popular tweets. It will get new tweets instead if this is not specified.', action="store_true")
parser.add_argument( '-l', '--lang', help='Language to search for. [Set to eng by default]', default=False)
return parser.parse_args()
def main():
#Default values.
search_string = "Los Angeles"
count = 10
popular = False
lang = "en"
arg = get_args()
if (arg.popular):
popular = True
if (arg.count):
count = arg.count
if (arg.lang):
lang = arg.lang
if (arg.search):
search_string = arg.search
tweetList = retrieve_tweets(search_string, count, popular, lang)
#Compiling all gathered tweets into one string.
for tweet in tweetList:
print(f"\nUser Screen Name: {tweet.screen_name}\nName: {tweet.display_name}\nDate: {tweet.date}\nTweet: {tweet.text}\nFavorite Count: {tweet.favorite_count}\nTweet URL: {tweet.url}")
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment