Created
June 21, 2018 02:14
-
-
Save emanuelfeld/378e3b6218baa6df0d3c501fd9a489cc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
import time | |
from config import API_KEY, API_SECRET, ACCESS_TOKEN, USER_ID | |
# Get your authentication information from https://disqus.com/api/applications/ | |
AUTH = { | |
'api_key': API_KEY, | |
'api_secret': API_SECRET, | |
'access_token': ACCESS_TOKEN | |
} | |
KEEP_FIRST_N_POSTS = 50 # 0 to delete everything | |
def pause_requests(r): | |
ratelimit_remaining = r.headers.get('X-Ratelimit-Remaining') | |
ratelimit_reset = r.headers.get('X-Ratelimit-Reset') | |
if ratelimit_remaining == '0' and ratelimit_reset: | |
seconds = float(ratelimit_reset) - time.time() + 60.0 | |
print('waiting…', seconds / 60.0, 'minutes') | |
time.sleep(seconds) | |
else: | |
time.sleep(0.5) | |
print('getting posts…') | |
posts = [] | |
payload = AUTH.copy() | |
payload['user'] = USER_ID | |
payload['limit'] = 100 | |
# payload['include'] = 'deleted' # to overwrite already "removed" posts | |
while True: | |
r = requests.get('https://disqus.com/api/3.0/users/listPosts.json', params=payload) | |
data = r.json() | |
res = data['response'] | |
posts = posts + [post['id'] for post in res] | |
pause_requests(r) | |
if data['cursor']['hasNext']: | |
payload['cursor'] = data['cursor']['next'] | |
else: | |
break | |
print('removing posts from view…') | |
payload = AUTH.copy() | |
for i in range(KEEP_FIRST_N_POSTS, len(posts), 20): | |
post_ids = posts[i : min(i + 20, len(posts))] | |
payload['post'] = post_ids | |
r = requests.post('https://disqus.com/api/3.0/posts/remove.json', params=payload) | |
print('deleted:', i + len(post_ids) - KEEP_FIRST_N_POSTS, 'of', len(posts) - KEEP_FIRST_N_POSTS) | |
pause_requests(r) | |
print('overwriting post text…') | |
payload = AUTH.copy() | |
payload['message'] = '.' | |
for i in range(KEEP_FIRST_N_POSTS, len(posts)): | |
payload['post'] = posts[i] | |
r = requests.post('https://disqus.com/api/3.0/posts/update.json', params=payload) | |
print('overwrote:', i + 1 - KEEP_FIRST_N_POSTS, 'of', len(posts) - KEEP_FIRST_N_POSTS) | |
pause_requests(r) | |
print('done') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment