Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

argparser added to have cli for getting keyword, .env file added to load twitter variables #39

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
consumer_key = 'xxxx'
consumer_secret = 'xxxx'
access_token = 'xxxx'
access_token_secret = 'xxxx'
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
venv
*.pyc
.idea
3 changes: 2 additions & 1 deletion hashtags/freelance.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
import tweepy
from tweepy import OAuthHandler
from textblob import TextBlob



class TwitterClient(object):
'''
Generic Twitter Class for sentiment analysis.
Expand Down
62 changes: 41 additions & 21 deletions jobtweets.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import re
from dotenv import load_dotenv
import os
import tweepy
from tweepy import OAuthHandler
from textblob import TextBlob

from argparse import ArgumentParser


class TwitterClient(object):
'''
Generic Twitter Class for sentiment analysis.
Expand All @@ -12,10 +16,11 @@ def __init__(self):
Class constructor or initialization method.
'''

consumer_key = 'XXXXXXXXXXXX'
consumer_secret = 'XXXXXXXXXXXX'
access_token = 'XXXXXXXXXXXX'
access_token_secret = 'XXXXXXXXXXXX'
load_dotenv()
consumer_key = os.getenv('consumer_key')
consumer_secret = os.getenv('consumer_secret')
access_token = os.getenv('access_token')
access_token_secret = os.getenv('access_token_secret')


try:
Expand Down Expand Up @@ -59,7 +64,7 @@ def get_tweets(self, query, count = 10):

try:

fetched_tweets = self.api.search(q = query, count = count)
fetched_tweets = self.api.search(q=query, count=count)


for tweet in fetched_tweets:
Expand All @@ -83,28 +88,43 @@ def get_tweets(self, query, count = 10):

except tweepy.TweepError as e:
print("Error : " + str(e))



def main():
parser = ArgumentParser(description=__doc__, add_help=False)
parser.add_argument('--keyword', action='store', help='a keyword to query on')
query = parser.parse_args()
api = TwitterClient()
tweets = api.get_tweets(query = 'Job Opportunities', count = 500)
tweets = api.get_tweets(query=query.keyword, count=500)

ptweets = [tweet for tweet in tweets if tweet['sentiment'] == 'positive']

print("Positive tweets percentage: {} %".format(100*len(ptweets)/len(tweets)))
if ptweets:
print("Positive tweets percentage: {} %".format(100*len(ptweets)/len(tweets)))
print("Positive tweets:\n")
for tweet in ptweets[:10]:
print(tweet['text'])
else:
print("Positive tweets percentage: 0 %\n")

ntweets = [tweet for tweet in tweets if tweet['sentiment'] == 'negative']
if ntweets:
print("Negative tweets percentage: {} %".format(100*len(ntweets)/len(tweets)))
print("Negative tweets:")
for tweet in ntweets[:10]:
print(tweet['text'])
else:
print("Negative tweets percentage: 0 %\n")

if len(ptweets) != len(ntweets):
print("Neutral tweets percentage: {} % ".format(100*(len(tweets) - len(ntweets) - len(ptweets))/len(tweets)))
print("Neutral tweets:\n")
neutrals = [tweet for tweet in tweets if (tweet not in ptweets) and (tweet not in ntweets)]
for tweet in neutrals[:10]:
print(tweet['text'])
else:
print("Neutral tweets percentage: 0 %")

print("Negative tweets percentage: {} %".format(100*len(ntweets)/len(tweets)))

print("Neutral tweets percentage: {} % ".format(100*(len(tweets) - len(ntweets) - len(ptweets))/len(tweets)))

print("\n\nPositive tweets:")
for tweet in ptweets[:10]:
print(tweet['text'])

print("\n\nNegative tweets:")
for tweet in ntweets[:10]:
print(tweet['text'])

if __name__ == "__main__":

main()