Skip to content

Instantly share code, notes, and snippets.

@soeirosantos
Last active January 11, 2019 04:40
Show Gist options
  • Save soeirosantos/4b670ff1fcff6ce6bace5715d64f9834 to your computer and use it in GitHub Desktop.
Save soeirosantos/4b670ff1fcff6ce6bace5715d64f9834 to your computer and use it in GitHub Desktop.
from tweepy import OAuthHandler, API
from nltk.corpus import stopwords
from textblob import TextBlob
import re
import json
class TwitterClient(object):
def __init__(self):
config = None
with open('config.json') as c:
config = json.load(c)
self.auth = OAuthHandler(config['api_key'], config['secret_key'])
self.auth.set_access_token(config['access_token'], config['secret_token'])
self.api = API(self.auth)
def get_tweets(self, query, count = 10):
return self.api.search(q = query, count = count)
def clean(text):
return ' '.join([w for w in re.sub(r"(@[A-Za-z0-9]+)|([^0-9A-Za-z \t])|(\w+:\/\/\S+)", " ", text).split() \
if w not in stopwords.words('english')])
def get_sentiment(text):
analysis = TextBlob(clean(text))
return analysis.sentiment.polarity
def overall_sentiment_about(query, count = 10):
client = TwitterClient()
tweets = client.get_tweets(query, count)
return sum([get_sentiment(tweet.text) for tweet in tweets])
if __name__ == '__main__':
import sys
if len(sys.argv) < 2:
print("Please, provide the term to query")
exit(1)
query = sys.argv[1]
count = 10
if len(sys.argv) > 2:
count = sys.argv[2]
print(overall_sentiment_about(query, count))

Installation and configuration

$ python -m venv env
$ source ./env/bin/activate
$ pip install --upgrade pip
$ pip install -r requirements.txt
$ python -m textblob.download_corpora
$ python
import nltk
nltk.download() # follow the steps in the GUI view
exit()
$ python 0-twitter_analysis.py python 2000
{
"api_key": "xxx",
"secret_key": "xxx",
"access_token": "xxx",
"secret_token": "xxx"
}
astroid==2.1.0
certifi==2018.11.29
chardet==3.0.4
idna==2.8
isort==4.3.4
lazy-object-proxy==1.3.1
mccabe==0.6.1
nltk==3.4
oauthlib==2.1.0
pylint==2.2.2
PySocks==1.6.8
requests==2.21.0
requests-oauthlib==1.1.0
singledispatch==3.4.0.3
six==1.12.0
textblob==0.15.2
tweepy==3.7.0
typed-ast==1.1.1
urllib3==1.24.1
wrapt==1.10.11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment