-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtag_to_collection.py
More file actions
67 lines (51 loc) · 1.98 KB
/
Copy pathtag_to_collection.py
File metadata and controls
67 lines (51 loc) · 1.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
"""
Add a tag to an entire Raindrop collection
"""
import os
import time
import requests
from dotenv import load_dotenv
# 1. Load environment variables from .env
load_dotenv()
# 2. Read configuration
API_TOKEN = os.getenv('RAINDROP_API_TOKEN')
COLLECTION_ID = os.getenv('RAINDROP_COLLECTION_ID')
TAG_TO_ADD = os.getenv('RAINDROP_TAG')
# 3. Verify that everything is configured
if not API_TOKEN:
raise ValueError("RAINDROP_API_TOKEN not found in .env")
if not COLLECTION_ID:
raise ValueError("RAINDROP_COLLECTION_ID not found in .env")
if not TAG_TO_ADD:
raise ValueError("RAINDROP_TAG not found in .env")
print("Configuration loaded:")
print(f" Collection ID: {COLLECTION_ID}")
print(f" Tag to add: {TAG_TO_ADD}")
# 4. Set headers for API
headers = {'Authorization': f'Bearer {API_TOKEN}'}
# 5. Retrieve raindrops from the collection
print(f"\nRetrieving raindrops from collection {COLLECTION_ID}...")
response = requests.get(f'https://api.raindrop.io/rest/v1/raindrops/{COLLECTION_ID}', headers=headers)
response.raise_for_status()
raindrops = response.json()['items']
# 6. Add tag to all raindrops
print(f"Adding tag '{TAG_TO_ADD}' to raindrops...")
success_count = 0
for i, raindrop in enumerate(raindrops, 1):
try:
# Check if the tag is already present
current_tags = raindrop.get('tags', [])
if TAG_TO_ADD not in current_tags:
# Add the tag
new_tags = current_tags + [TAG_TO_ADD]
# Update the raindrop
update_response = requests.put(f'https://api.raindrop.io/rest/v1/raindrop/{raindrop["_id"]}', headers=headers, json={'tags': new_tags})
update_response.raise_for_status()
success_count += 1
if i % 10 == 0:
print(f" Processed {i} raindrops...")
except Exception as e:
print(f" Error processing raindrop {i}: {e}")
time.sleep(0.1) # Rate limiting
print(f"\nTag '{TAG_TO_ADD}' added to {success_count} raindrops")
print(f"Collection ID: {COLLECTION_ID}")