Last active
May 22, 2024 10:11
-
-
Save ImSingee/cd8f7fca106bd0142810985e6f32f539 to your computer and use it in GitHub Desktop.
Clear large Raindrop collections via API
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
collection_id = '...' # -99 to empty trash | |
token = '...' | |
from requests import Session | |
session = Session() | |
session.headers = { | |
'Authorization': f'Bearer {token}' | |
} | |
import time | |
def session_do(method, *args, **kwargs): | |
response = session.request(method,*args,**kwargs) | |
if response.status_code == 502: | |
print("502 sleep 1s and continue") | |
time.sleep(1) | |
return session_do(method, *args, **kwargs) | |
if response.status_code == 504: | |
print("504 sleep 10s and continue") | |
time.sleep(10) | |
return session_do(method, *args, **kwargs) | |
if response.status_code == 429: | |
wait = int(response.headers['retry-after']) | |
print(f'rate limit, sleep {wait} seconds') | |
if wait >= 0: | |
time.sleep(wait+1) | |
return session_do(method, *args, **kwargs) | |
return response | |
def session_get(*args, **kwargs): | |
return session_do('GET', *args, **kwargs) | |
def session_delete(*args, **kwargs): | |
return session_do('DELETE', *args, **kwargs) | |
def remove_raindrops(ids): | |
result = session_delete(f'https://api.raindrop.io/rest/v1/raindrops/{collection_id}', json={'ids': ids}) | |
result.raise_for_status() | |
return result.json() # {"result": true, "modified": 330} | |
def get_raindrops(): | |
result = session_get(f'https://api.raindrop.io/rest/v1/raindrops/{collection_id}?perpage=50') | |
result.raise_for_status() | |
result = result.json() | |
all = [item['_id'] for item in result['items']] | |
return result['count'], all | |
def do(i=1): | |
count, ids = get_raindrops() | |
print(f'{i} remove, remains {count}') | |
if count == 0: | |
print('empty, stop') | |
return False | |
remove_raindrops(ids) | |
return True | |
i = 1 | |
while do(i): | |
i += 1 | |
def remove_raindrops_by_search(search): | |
result = session_delete(f'https://api.raindrop.io/rest/v1/raindrops/{collection_id}', json={'search': search}) | |
result.raise_for_status() | |
return result.json() # {"result": true, "modified": 330} | |
# remove_raindrops_by_search('#Buzzing') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment