def send_tweet():
twitter = Twython(
consumer_key,
consumer_secret,
access_token,
access_token_secret
)
# Send the tweet
message = "The All Seeing Pi saw you!"
with open(output, 'rb') as photo:
twitter.update_status_with_media(status=message, media=photo)
# Set up buttons
python类Twython()的实例源码
finished_allseeingpi.py 文件源码
项目:the-all-seeing-pi
作者: raspberrypilearning
项目源码
文件源码
阅读 22
收藏 0
点赞 0
评论 0
def __init__(self):
config = self.parse_config()
self.twitter = Twython(config.get('consumer_key'), config.get('consumer_secret'),
config.get('access_token'), config.get('access_token_secret'))
def Run(self):
'''
All the high-level logic of the bot is driven from here:
- load settings
- connect to twitter
- (let your derived bot class get set up)
- either:
- wait for events from the streaming API
- do bot stuff:
- maybe create one or more tweets
- handle any mentions
- handle any streaming API events that were saved
- send tweets out
- (let your derived bot class clean up)
'''
self.LoadSettings()
# create the Twython object that's going to communicate with the
# twitter API.
appKey = self.settings.appKey
appSecret = self.settings.appSecret
accessToken = self.settings.accessToken
accessTokenSecret = self.settings.accessTokenSecret
if self.stream:
self.twitter = NanobotStreamer(appKey, appSecret, accessToken, accessTokenSecret)
self.twitter.SetOutputPath(self.botPath)
else:
self.twitter = Twython(appKey, appSecret, accessToken, accessTokenSecret)
# give the derived bot class a chance to do whatever it needs
# to do before we actually execute.
self.PreRun()
if self.stream:
if self.debug:
print "About to stream from user account."
try:
# The call to user() will sit forever waiting for events on
# our user account to stream down. Those events will be handled
# for us by the BotStreamer object that we created above
self.twitter.user()
except KeyboardInterrupt:
# disconnect cleanly from the server.
self.twitter.disconnect()
else:
self.CreateUpdate()
self.HandleMentions()
self.HandleStreamEvents()
self.SendTweets()
# if anything we did changed the settings, make sure those changes
# get written out.
self.settings.lastExecuted = str(datetime.now())
self.settings.Write()
# ...and let the derived bot class clean up as it needs to.
self.PostRun()
def twitter(query):
ckey = os.environ['consumer_key']
csecret = os.environ['consumer_secret']
atoken = os.environ['access_token']
asecret = os.environ['access_secret']
twitter = Twython(ckey, csecret, atoken, asecret)
try:
search_results = twitter.search(q=query, languages = ['pt'] ,count=100)
except TwythonError as e:
print (e)
reviews = []
tweets = []
for tweet in search_results['statuses']:
if tweet['lang'].encode("utf-8") == 'pt':
tweets.append(tweet['text'])
if tweets == []:
return [], [], []
sents = sentiment(tweets)
both = zip(tweets,sents)
overall_sentiment = []
count_pos = 0
count_neutral = 0
count_neg = 0
for i in range(len(both)):
sent_dict = {}
sent_dict['tweet'] = both[i][0]
sent_dict['sentimento'] = both[i][1]
if sent_dict['sentimento'] == 0:
sent_dict['sentimento'] = "negative"
overall_sentiment.append(-1.0)
count_neg = count_neg + 1
elif sent_dict['sentimento'] == 1:
sent_dict['sentimento'] = "neutral"
overall_sentiment.append(0.0)
count_neutral = count_neutral + 1
elif sent_dict['sentimento'] == 2:
sent_dict['sentimento'] = "positive"
overall_sentiment.append(1.0)
count_pos = count_pos + 1
reviews.append(sent_dict)
overall_sentiment = sum(overall_sentiment)/len(overall_sentiment)
data = [count_neg, count_neutral, count_pos]
return reviews, overall_sentiment, data