Tweepy - 如何在删除推文时通知

时间:2018-02-21 21:55:04

标签: python-3.x twitter tweepy

我了解到Twitter已经停止为已删除的推文提供JSON。我正试图通过使用轮询方法来查看推文是否被删除。

但我的代码仍然失败。如果你能帮助我找出我所缺少的东西,我将不胜感激。

import sys
import json
import tweepy
from tweepy import Stream
from tweepy.streaming import StreamListener
import datetime
import time
from polling import TimeoutException, poll

# Go to http://apps.twitter.com and create an app.
# The consumer key and secret will be generated for you after
consumer_key        = 'xx'
consumer_secret     = 'xx'
access_token        = 'xx'
access_token_secret = 'xx'

# Set up the authorisation to use the Twitter API
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)


# Handle the output generated from the stream
class listener(StreamListener):
    tweetcount = 0

    def on_data(self, data):

        # Convert the message to JSON
        json_data = json.loads(data)

        # text_file = open(json_data['id_str'] + ".json", "w")
        # text_file.write(data)
        # text_file.close()


        if 'id_str' not in json_data:
            # If this isn't a status, do nothing.
            print("no ID")
        else:
            #print("Twitter Id ",json_data['id_str'])
            #print("User Id ",json_data['user']['id_str'])

            if json_data['user']['id_str'] == '51241574': #Associated Press
                tweetcount = json_data['user']['statuses_count']
                tweet = api.get_status(json_data['id'])
                print("Tweet Count ",tweetcount)
                print("Account Name  ", json_data['user']['name'])
                print(tweet.text)

            else:
                pass

        # if 'delete' in json_data:
        #     print ("DELETED!")
        #     if json_data['delete']['status']['user_id'] == '51241574':
        #         deleted_tweet_id =json_data['delete']['status']['id']
        #         tweetcount -= 1
        #         print("New Count is ",tweetcount)
        #         print(deleted_tweet_id)
        #         deleted_tweet =api.get_status(deleted_tweet_id)
        #         print(deleted_tweet.text)
        #
        #     else:
        #         pass

        return True

    def on_error(self, status):
        print("Error status is ",status)


#   Start consuming from the stream.  This will get all the Tweets & Deletions from the users the user is following.
twitterStream = Stream(auth, listener())
twitterStream.filter(follow=['51241574'], async=True)



   # polling method to check if tweet is deleted

try:
    user = api.get_user('AP')
    poll(lambda: user.statuses_count >= listener.tweetcount > 0, timeout=30, step=1)
    print("Tweet was deleted,New Tweet count is ", user.statuses_count)

except Exception as ex:
    template = "An exception of type {0} occurred. Arguments:\n{1!r}"
    message = template.format(type(ex).__name__, ex.args)
    print (message)

当触发侦听器事件时,应用程序会在tweet count变量中显示该值,并根据从查询api中检索到的值对其进行检查。

0 个答案:

没有答案