[英]How do I delete all tweets using python-twitter?
I'm using python-twitter in my Web Application to post tweets like this: 我在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")
How do I delete all tweets that have been posted? 如何删除所有已发布的推文? I can't find documentation how to do it.
我找不到文档该怎么做。
twitter_api.PostUpdate("Hello World")
should return a Status
object. twitter_api.PostUpdate("Hello World")
应该返回一个Status
对象。 That Status
object also contains information about the status which, according to their source is present as an attribute . 该
Status
对象还包含有关状态的信息, 根据其来源将其作为属性出现 。
twitter_api.destroyStatus
is apparently the method they have which wraps around the POST statuses/destroy
twitter request. twitter_api.destroyStatus
显然是他们拥有的方法,用于包装POST statuses/destroy
Twitter请求。 To destroy a status, it takes as an argument the status.id
. 要销毁状态,需要使用
status.id
作为参数。
So: 所以:
status = twitter_api.PostUpdate("hello world")
twitter_api.destroyStatus(status.id)
should be sufficient. 应该足够了。 There doesn't seem to be a way to bulk-delete content, you'll have to fetch the content first and then delete it status-by-status.
似乎没有一种方法可以批量删除内容,您必须先获取内容,然后逐个状态将其删除。
Fetching a sequence (which I guess implies it is iterable) from your timeline is done with twitter_api.GetUserTimeline
with a limit of 200
tweets each time. 从时间轴获取一个序列 (我猜想它是可迭代的)是通过
twitter_api.GetUserTimeline
完成的,每次限制为200
条推文。 This should allow you to grab tweets, check if the there's a result and if iterate through them and delete them with destroyStatus
. 这应该允许您抓取tweet,检查是否有结果,以及是否遍历它们,然后使用
destroyStatus
删除它们。
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. Windows用户需要在控制台中运行命令“ chcp 65001”,然后才能从命令提示符运行脚本。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.