Skip to content

Instantly share code, notes, and snippets.

@charlesroper
Created April 19, 2023 15:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save charlesroper/2c37e977260f550db66c6fcf838cf075 to your computer and use it in GitHub Desktop.
Save charlesroper/2c37e977260f550db66c6fcf838cf075 to your computer and use it in GitHub Desktop.
# Import requests library
import requests
import time
import constants
# Define the collection ID and the access token
# collection_id = 33417911
collection_id = None
live_run = False
access_token = constants.RAINDROP_TOKEN
# Define the URL for the API call to get all tags
url = "https://api.raindrop.io/rest/v1/tags"
if collection_id:
url = f"{url}/{collection_id}"
# Define the headers for the API call
headers = {
"Authorization": f"Bearer {access_token}",
"Content-Type": "application/json",
}
# Make the GET request and get the response
response = requests.get(url, headers=headers)
# Check the status code and get the list of tags
if response.status_code == 200:
tags = response.json()["items"]
else:
print(f"Something went wrong. Status code: {response.status_code}")
exit()
# Define a function to convert a tag to CamelCase
def camel_case(tag):
# Split the tag by space and capitalize each word
tag = tag.replace("-", " ").replace(".", " ")
# Title Case the tag and remove spaces
tag = tag.title().replace(" ", "")
# Join the words without space and return the result
return tag
# Loop through each tag and check if it contains a space
for tag in tags:
# Get the tag name
tag_name = tag["_id"]
# If the tag name contains a space, rename it to CamelCase
if (" " in tag_name) or ("-" in tag_name) or ("." in tag_name):
# Define the new tag name using the camel_case function
new_tag_name = camel_case(tag_name)
# Define the body for the API call to update the tag
body = {"replace": new_tag_name, "tags": [tag_name]}
# Make the PUT request and get the response
if live_run:
response = requests.put(url, headers=headers, json=body)
# Throttle the requests to 1 per second
time.sleep(1)
# Check the status code and print the result
if response.status_code == 200:
print(f"Tag '{tag_name}' renamed to '{new_tag_name}'.")
else:
print(f"Something went wrong. Status code: {response.status_code}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment