如何使用python-twitter删除所有推文?

时间:2016-07-09 22:08:45

标签: python twitter python-twitter

我在我的网络应用程序中使用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")

如何删除已发布的所有推文?我无法找到如何操作的文档。

2 个答案:

答案 0 :(得分:2)

twitter_api.PostUpdate("Hello World")应返回Status个对象。该Status对象还包含有关状态的信息,according to their source is present as an attribute

twitter_api.destroyStatus显然是他们拥有的POST statuses/destroy Twitter请求的方法。要销毁状态,需要status.id作为参数。

所以:

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

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

从时间轴中获取序列(我猜这是可迭代的)是使用twitter_api.GetUserTimeline完成的,每次都有200条限制。这应该允许您抓取推文,检查是否有结果,如果迭代它们并使用destroyStatus删除它们。

答案 1 :(得分: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用户需要在其控制台中运行命令“chcp 65001”。