2

我在我的 Web 应用程序中使用python-twitter来发布这样的推文:

import twitter
twitter_api = twitter.Api(
    consumer_key="BlahBlahBlah",
    consumer_secret="BlahBlahBlah",
    access_token_key="BlahBlahBlah",
    access_token_secret="BlahBlahBlah",
)
twitter_api.PostUpdate("Hello World")

如何删除所有已发布的推文?我找不到文档如何做到这一点。

4

2 回答 2

3

twitter_api.PostUpdate("Hello World")应该返回一个Status对象。该Status对象还包含有关状态的信息,根据其来源,该信息作为属性存在

twitter_api.destroyStatus显然是他们拥有的方法,它围绕着POST statuses/destroytwitter 请求。要破坏状态,它将 . 作为参数status.id

所以:

status = twitter_api.PostUpdate("hello world")
twitter_api.destroyStatus(status.id)

应该足够了。似乎没有办法批量删除内容,您必须先获取内容,然后逐个状态删除它。

从您的时间线中获取一个序列(我猜这意味着它是可迭代的)是通过每次推文twitter_api.GetUserTimeline的限制来完成的。200这应该允许您抓取推文,检查是否有结果以及是否遍历它们并使用destroyStatus.

于 2016-07-09T22:22:19.593 回答
0
import time
import re
import twitter
try:
    # UCS-4
    HIGHPOINTS = re.compile(u'[\U00010000-\U0010ffff]')
except re.error:
    # UCS-2
    HIGHPOINTS = re.compile(u'[\uD800-\uDBFF][\uDC00-\uDFFF]')

class TwitterPurger():
    '''
    Purges the a Twitter account of all all tweets and favorites
    '''
    MAX_CALLS_PER_HOUR = 99
    SECONDS_IN_AN_HOUR = 3600
    def __init__(self):
        self.api_call_count = 0

    def increment_or_sleep(self):
        '''
        Increments the call count or sleeps if the max call count per hour
        has been reached
        '''
        self.api_call_count = self.api_call_count + 1
        if self.api_call_count > TwitterPurger.MAX_CALLS_PER_HOUR:
            time.sleep(TwitterPurger.SECONDS_IN_AN_HOUR)
            self.api_call_count = 0

    def delete_everything(self, screen_name, consumer_key,
                          consumer_secret, access_token, access_token_secret):
        '''
        Deletes all statuses and favorites from a Twitter account
        '''
        api = twitter.Api(consumer_key=consumer_key, consumer_secret=consumer_secret,
                          access_token_key=access_token, access_token_secret=access_token_secret)

        var_time_line_statuses = api.GetUserTimeline(screen_name=screen_name, include_rts=True)
        self.increment_or_sleep()

        while len(var_time_line_statuses) > 0:
            for status in var_time_line_statuses:
                print('Deleting status {id}: {text}'
                      .format(id=str(status.id),
                              text=HIGHPOINTS.sub('_', status.text)))
                api.DestroyStatus(status.id)
            var_time_line_statuses = api.GetUserTimeline(screen_name=screen_name, include_rts=True)
            self.increment_or_sleep()

        user_favorites = api.GetFavorites(screen_name=screen_name)
        self.increment_or_sleep()

        while len(user_favorites) > 0:
            for favorite in user_favorites:
                print('Deleting favorite {id}: {text}'
                      .format(id=str(favorite.id),
                              text=HIGHPOINTS.sub('_', favorite.text)))
                api.DestroyFavorite(status=favorite)
            user_favorites = api.GetFavorites(screen_name=screen_name)
            self.increment_or_sleep()

Windows users will need to run the command "chcp 65001" in their console before running the script from the command prompt.

于 2017-09-08T19:40:09.327 回答