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

如何使用python twitter删除所有推文?,python,twitter,python-twitter,Python,Twitter,Python Twitter,我在我的Web应用程序中使用以下内容发布推文: 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"

我在我的Web应用程序中使用以下内容发布推文:

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")

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

twitter\u api.PostUpdate(“Hello World”)
应该返回一个
状态
对象。该
Status
对象还包含有关以下状态的信息:

显然,这是他们围绕twitter请求的方法。要销毁状态,它将
status.id
作为参数

因此:

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

从你的时间线中获取一个序列(我猜这意味着它是可编辑的)是以每次
200
tweets的限制完成的。这应该允许您抓取tweet,检查是否有结果,是否对它们进行迭代,并使用
destroystation
删除它们

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”。

如何获取此Twitter帐户中发布的所有推文?请注意,它们可能不是由我的python应用程序发布的。它们以前可能是通过其他Twitter客户端发布的。@SaqibAli显然,这似乎是可用的,似乎您每次最多可以获得
count
推特(其中
count==200
)。当你在200条tweet中进行迭代时,如果你为每条tweet启动一个新的线程并在它自己的线程中删除该tweet,那么它会更快。
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()