Skip to content

Instantly share code, notes, and snippets.

@akki2825
Last active January 27, 2017 15:29
Show Gist options
  • Save akki2825/2283eca6d8bb9f861517382765363c60 to your computer and use it in GitHub Desktop.
Save akki2825/2283eca6d8bb9f861517382765363c60 to your computer and use it in GitHub Desktop.
import tweepy
from textblob import TextBlob
import csv
def Tweep():
consumer_key = ""
consumer_secret = ""
access_token = ""
access_token_secret = ""
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
return tweepy.API(auth)
def labeling(text):
blob = TextBlob(text).sentiment
if blob.subjectivity == 0:
return 0 # If no subjectivity we wont use the tweet
else:
return 'Positive' if blob.polarity > 0 else 'Negative'
def give_me_csv(user, topic):
list_of_tweets = user.search(topic, count=100)
filename = 'twitter_sentiment_%s.csv' % topic.replace(' ','')
with open(filename, 'w') as file:
fieldnames = ['tweet', 'sentiment_score']
writer = csv.DictWriter(file, fieldnames=fieldnames)
writer.writeheader()
for tweet in list_of_tweets:
tweet_text = tweet.text.encode('utf-8')
score = labeling(tweet.text)
if score != 0:
writer.writerow({
'tweet' : tweet_text,
'sentiment_score' : score
})
def main():
# Login twitter
user = Tweep()
# Ask topic
topic = raw_input('Enter a topic to search: ')
# Generate file
give_me_csv(user, topic)
if __name__ == '__main__':
main()
wp_embed_register_handler( 'gist', '/https?:\/\/gist\.github\.com\/([a-z0-9]+)(\?file=.*)?/i', 'bhww_embed_handler_gist' );
function bhww_embed_handler_gist( $matches, $attr, $url, $rawattr ) {
$embed = sprintf(
'<script src="https://gist.github.com/%1$s.js%2$s"></script>',
esc_attr($matches[1]),
esc_attr($matches[2])
);
return apply_filters( 'embed_gist', $embed, $matches, $attr, $url, $rawattr );
}
@jhazelwo
Copy link

Change your access tokens!! Public gists are indexed and searchable!

@akki2825
Copy link
Author

Thanks !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment