Last active
June 16, 2023 02:52
-
-
Save nickymarino/df491a505616731f8fb45533ee93c4a2 to your computer and use it in GitHub Desktop.
Bulk add items to Reader via API
This file contains hidden or 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
from typing import List, Optional | |
import requests | |
import pydantic | |
import json | |
import time | |
URL = "https://readwise.io/api/v3/save/" | |
TOKEN = "xxxxxxxx" | |
links = [ | |
... | |
] | |
def post_strings_to_endpoint(urls: list[str], endpoint_url: str, headers: dict): | |
for url in urls: | |
while True: | |
try: | |
response = requests.post( | |
endpoint_url, | |
json={ | |
"url": url, | |
}, | |
headers=headers, | |
) | |
response.raise_for_status() # Raises an exception for 4xx and 5xx status codes | |
print(f'Successfully posted: {url}') | |
break | |
except requests.exceptions.HTTPError as error: | |
if error.response.status_code == 429: | |
print( | |
f'Too many requests. Retrying in 2 minutes for: {url}' | |
) | |
time.sleep(121) # Wait for 2 minutes before retrying | |
else: | |
print( | |
f'Failed to post "{url}" with status {error.response.status_code}' | |
) | |
break | |
if __name__ == "__main__": | |
post_strings_to_endpoint( | |
urls=links, endpoint_url=URL, headers={"Authorization": f"Token {TOKEN}"} | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment