Skip to content

Commit

Permalink
Merge pull request #14 from MLH-Fellowship/tweets
Browse files Browse the repository at this point in the history
Twitter Module
  • Loading branch information
grimmmyshini authored Oct 12, 2020
2 parents 9efe95e + 42dc4fc commit e7f86a4
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 8 deletions.
2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/twitstat.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

69 changes: 69 additions & 0 deletions apps/tweets.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import os

import tweepy


class Twitter:
def __init__(self):

# Authorization
self.TWITTER_CONSUMER_API_KEY = os.getenv("TWITTER_CONSUMER_API_KEY")
self.TWITTER_CONSUMER_SECRET_KEY = os.getenv("TWITTER_CONSUMER_SECRET_KEY")
self.TWITTER_ACCESS_TOKEN = os.getenv("TWITTER_ACCESS_TOKEN")
self.TWITTER_ACCESS_TOKEN_SECRET = os.getenv("TWITTER_ACCESS_TOKEN_SECRET")

auth = tweepy.OAuthHandler(
self.TWITTER_CONSUMER_API_KEY, self.TWITTER_CONSUMER_SECRET_KEY
)
auth.set_access_token(
self.TWITTER_ACCESS_TOKEN, self.TWITTER_ACCESS_TOKEN_SECRET
)

self.TWITTER_API = tweepy.API(auth, wait_on_rate_limit=True)

def get_top_trends(self) -> list:
"""Returns a list of top trends at the specified location"""
# WOEID = 1 for global trending
latitude = os.getenv("LATITUDE")
longitude = os.getenv("LONGITUDE")

locations = self.TWITTER_API.trends_closest(latitude, longitude)
woeid = locations[0]["woeid"]

trends = self.TWITTER_API.trends_place(woeid)
trends_dict = trends[0]["trends"]

return [trends_dict[0]]

def get_trending_tweets(self, find_word):
query = find_word + " -filter:retweet" + " -filter:media" + " -filter:links"
tweet_count_limit = 1000
tweet_counter = 0
tweets_list = list()

while tweet_counter < tweet_count_limit:
for tweet in tweepy.Cursor(
self.TWITTER_API.search,
q=query,
count=tweet_count_limit,
lang="en",
result_type="mixed",
).items():
tweets = dict()
tweets["id"] = tweet.id
tweets["tweets"] = tweet.text
tweets["interactions"] = tweet.favorite_count + tweet.retweet_count

tweets_list.append(tweets)

tweet_counter += 1

return tweets_list


if __name__ == "__main__":
api = Twitter()
top_trends = api.get_top_trends()
print(top_trends)
trending_tweets = api.get_trending_tweets(top_trends[0]["name"])
print(trending_tweets)
15 changes: 9 additions & 6 deletions requirements/base.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,6 @@ pytz==2020.1 # https://github.com/stub42/pytz
python-slugify==4.0.1 # https://github.com/un33k/python-slugify
Pillow==7.2.0 # https://github.com/python-pillow/Pillow
argon2-cffi==20.1.0 # https://github.com/hynek/argon2_cffi
tweepy~=3.9.0 # https://github.com/tweepy/tweepy
python-dotenv~=0.14.0 # https://github.com/theskumar/python-dotenv
nltk~=3.5 # https://github.com/nltk/nltk
pandas~=1.1.3 # https://github.com/pandas-dev/pandas
scikit-learn~=0.23.2 # https://github.com/scikit-learn/scikit-learn
textblob~=0.15.3 # https://github.com/sloria/TextBlob

# Flask
# ------------------------------------------------------------------------------
Expand All @@ -17,3 +11,12 @@ itsdangerous==1.1.0 # https://github.com/pallets/itsdangerous
Jinja2==2.11.2 # https://github.com/pallets/jinja
MarkupSafe==1.1.1 # https://github.com/pallets/markupsafe
Werkzeug==1.0.1 # https://github.com/pallets/werkzeug

# Others
# ------------------------------------------------------------------------------
tweepy==3.9.0 # https://github.com/tweepy/tweepy
python-dotenv==0.14.0 # https://github.com/theskumar/python-dotenv
nltk==3.5 # https://github.com/nltk/nltk
pandas==1.1.3 # https://github.com/pandas-dev/pandas
scikit-learn==0.23.2 # https://github.com/scikit-learn/scikit-learn
textblob==0.15.3 # https://github.com/sloria/TextBlob

0 comments on commit e7f86a4

Please sign in to comment.