-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit.py
More file actions
560 lines (472 loc) · 22.8 KB
/
init.py
File metadata and controls
560 lines (472 loc) · 22.8 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
"""
Instagram Saved Posts Organizer
================================
Reads your official Instagram data export, classifies each post using
Claude AI (caption + image), then creates collections and moves posts.
SETUP:
pip install -r requirements.txt
playwright install chromium
USAGE:
1. Place saved_posts.json in the same folder as this script
2. Run: python instagram_organizer.py
3. Log in manually when the browser opens, then press Enter
"""
import asyncio
import base64
import json
import os
import re
import time
import random
import anthropic
import requests
from pathlib import Path
from dotenv import load_dotenv
from playwright.async_api import async_playwright
# ─── CONFIG ──────────────────────────────────────────────────────────────────
load_dotenv()
ANTHROPIC_API_KEY = os.environ.get("ANTHROPIC_API_KEY")
SAVED_POSTS_FILE = "saved_posts.json"
PROGRESS_FILE = "classification_progress.json" # resume from here if interrupted
MOVED_FILE = "moved_posts.json" # track which posts have been moved
# Delay between post fetches (seconds)
FETCH_DELAY = (4.0, 8.0)
ACTION_DELAY = (3.0, 6.0)
SCROLL_DELAY = (2.0, 4.0)
# How long to wait (seconds) on rate limit before retrying
RATE_LIMIT_WAITS = [60, 120, 300]
# ── Customize your classification rules here ──────────────────────────────────
COLLECTIONS = [
"Fashion & Style",
"Art & Design",
"Books & Reading",
"Food & Recipes",
"Travel & Places",
"Engineering & Tech",
"News & Articles",
"Education & Scholarships",
"Fitness & Health",
"Pets",
"Miscellaneous",
]
CLASSIFICATION_INSTRUCTIONS = f"""
You are helping organize Instagram saved posts into collections.
Analyze the post caption and image, then assign ONE collection from this EXACT list:
{chr(10).join(f'- {c}' for c in COLLECTIONS)}
Rules:
- You MUST pick one of the collections above. Do NOT create new ones.
- If unsure, use "Miscellaneous".
- Be consistent: similar posts always go to the same collection.
Return ONLY a JSON object, no markdown, no explanation outside it:
{{"collection": "Collection Name", "reason": "short explanation"}}
"""
# ─── HELPERS ─────────────────────────────────────────────────────────────────
def sleep(range_tuple):
time.sleep(random.uniform(*range_tuple))
def load_export(path: str) -> list[dict]:
"""Parse saved_posts.json from Instagram data export."""
with open(path, "r", encoding="utf-8") as f:
data = json.load(f)
posts = []
for item in data.get("saved_saved_media", []):
saved_on = item.get("string_map_data", {}).get("Saved on", {})
url = saved_on.get("href", "")
author = item.get("title", "")
if url:
posts.append({"url": url, "author": author, "caption": "", "image_url": None})
return posts
def load_progress() -> dict:
if Path(PROGRESS_FILE).exists():
with open(PROGRESS_FILE, "r") as f:
return json.load(f)
return {}
def save_progress(progress: dict):
with open(PROGRESS_FILE, "w") as f:
json.dump(progress, f, indent=2, ensure_ascii=False)
# ─── ENRICHMENT ──────────────────────────────────────────────────────────────
async def enrich_post(page, post: dict) -> dict:
"""
Try oEmbed first (lightweight, no auth), then fall back to full page visit.
"""
# Strategy 1: oEmbed (public, no login required)
try:
r = requests.get(
f"https://www.instagram.com/oembed/?url={post['url']}",
timeout=10,
headers={"User-Agent": "Mozilla/5.0"}
)
if r.status_code == 200:
data = r.json()
post["caption"] = data.get("title", "")
post["image_url"] = data.get("thumbnail_url", "")
if post["caption"] or post["image_url"]:
return post
except Exception:
pass
# Strategy 2: Full page visit with og: meta tags + rate-limit retry
for attempt, wait_s in enumerate([0] + RATE_LIMIT_WAITS):
if wait_s:
print(f" ⏳ Rate limited. Waiting {wait_s}s (attempt {attempt}/3)...")
await page.wait_for_timeout(wait_s * 1000)
try:
await page.goto(post["url"], wait_until="domcontentloaded", timeout=20000)
await page.wait_for_timeout(2000)
og_desc = await page.query_selector("meta[property='og:description']")
if og_desc:
post["caption"] = (await og_desc.get_attribute("content") or "").strip()
og_img = await page.query_selector("meta[property='og:image']")
if og_img:
post["image_url"] = (await og_img.get_attribute("content") or "").strip()
break
except Exception as e:
if attempt == len(RATE_LIMIT_WAITS):
print(f" ⚠️ Giving up on {post['url']}")
continue
return post
# ─── CLASSIFICATION ───────────────────────────────────────────────────────────
client = anthropic.Anthropic(api_key=ANTHROPIC_API_KEY)
def encode_image(url: str) -> str | None:
try:
r = requests.get(url, timeout=10, headers={"User-Agent": "Mozilla/5.0"})
r.raise_for_status()
return base64.standard_b64encode(r.content).decode("utf-8")
except Exception:
return None
def classify_post(caption: str, image_url: str | None, author: str) -> dict:
content = []
if image_url:
img_b64 = encode_image(image_url)
if img_b64:
content.append({
"type": "image",
"source": {"type": "base64", "media_type": "image/jpeg", "data": img_b64}
})
text = f"Posted by: @{author}\nCaption: {caption.strip() if caption else '(none)'}"
content.append({"type": "text", "text": text})
try:
response = client.messages.create(
model="claude-sonnet-4-20250514",
max_tokens=200,
system=CLASSIFICATION_INSTRUCTIONS,
messages=[{"role": "user", "content": content}]
)
raw = response.content[0].text.strip().replace("```json", "").replace("```", "").strip()
return json.loads(raw)
except Exception as e:
print(f" ⚠️ Classification error: {e}")
return {"collection": "Miscellaneous", "reason": "classification failed"}
# ─── INSTAGRAM AUTOMATION ────────────────────────────────────────────────────
async def wait_for_login(page):
await page.goto("https://www.instagram.com/accounts/login/", wait_until="domcontentloaded")
print("\n🔐 Please log in to Instagram in the browser window.")
print(" After you're logged in and see your feed, press Enter here...")
input()
await page.wait_for_timeout(3000)
async def get_username(page) -> str:
try:
links = await page.query_selector_all("a[href^='/']")
skip = {"explore", "reels", "direct", "stories", "accounts",
"your_activity", "notifications", "inbox", ""}
for link in links:
href = await link.get_attribute("href")
if href:
seg = href.strip("/")
if seg and "/" not in seg and seg not in skip:
return seg
except Exception:
pass
return input(" Enter your Instagram username: ").strip()
async def get_existing_collections(page, username: str) -> set[str]:
collections = set()
try:
await page.goto(f"https://www.instagram.com/{username}/saved/", wait_until="domcontentloaded")
await page.wait_for_timeout(3000)
links = await page.query_selector_all("a[href*='/saved/']")
for link in links:
name = (await link.inner_text()).strip()
if name:
collections.add(name)
except Exception as e:
print(f" ⚠️ Could not fetch collections: {e}")
return collections
async def create_collection(page, username: str, name: str):
print(f" ➕ Creating collection: '{name}'")
try:
await page.goto(f"https://www.instagram.com/{username}/saved/", wait_until="domcontentloaded")
await page.wait_for_timeout(3000)
new_btn = await page.query_selector(
"div[role='button']:has-text('Nueva colección'), "
"div[role='button']:has-text('New collection'), "
"button:has-text('Nueva colección'), "
"button:has-text('New collection')"
)
if new_btn:
await new_btn.click()
await page.wait_for_timeout(1500)
# Step 1: type the collection name
input_el = await page.query_selector("input[type='text'], input[placeholder]")
if input_el:
await input_el.fill(name)
await page.wait_for_timeout(500)
# Step 2: click "Siguiente" / "Next" to advance the modal
siguiente = await page.query_selector(
"button:has-text('Siguiente'), button:has-text('Next')"
)
if siguiente:
await siguiente.click()
await page.wait_for_timeout(1500)
# Step 3: click "Crear" / "Create" to confirm
submit = await page.query_selector(
"button:has-text('Crear'), button:has-text('Create'), "
"button:has-text('Listo'), button:has-text('Done'), "
"button[type='submit']"
)
if submit:
await submit.click()
await page.wait_for_timeout(2000)
print(f" ✅ Created '{name}'")
return
else:
print(f" ⚠️ Could not find confirm button for '{name}'")
else:
print(f" ⚠️ Could not find name input for '{name}'")
else:
print(f" ⚠️ Could not find 'New collection' button — create '{name}' manually.")
except Exception as e:
print(f" ⚠️ Error creating '{name}': {e}")
async def add_post_to_collection(page, post_url: str, collection_name: str):
for attempt, wait_s in enumerate([0] + RATE_LIMIT_WAITS):
if wait_s:
print(f" ⏳ Rate limited. Waiting {wait_s}s (attempt {attempt}/3)...")
await page.wait_for_timeout(wait_s * 1000)
try:
await page.goto(post_url, wait_until="domcontentloaded", timeout=20000)
break
except Exception as e:
if attempt == len(RATE_LIMIT_WAITS):
print(f" ⚠️ Giving up on {post_url}")
return
continue
try:
await page.wait_for_timeout(2500)
# Step 1: Find and click the bookmark icon
# "Eliminar" = already saved, "Guardar" = not saved yet
bookmark = None
for sel in [
"svg[aria-label='Eliminar']", # already saved (Spanish)
"svg[aria-label='Remove']", # already saved (English)
"svg[aria-label='Guardar']", # not yet saved (Spanish)
"svg[aria-label='Save']", # not yet saved (English)
]:
el = await page.query_selector(sel)
if el:
bookmark = el
break
if not bookmark:
print(f" ⚠️ Could not find bookmark button")
return
# Hover over the bookmark to trigger the collection dropdown
parent = await bookmark.evaluate_handle("el => el.closest('button') || el.parentElement")
await parent.hover()
await page.wait_for_timeout(2000)
# Dump what appeared after hover to find collection elements
all_texts = await page.eval_on_selector_all(
"div, span, li",
"els => els.map(e => e.textContent.trim()).filter(t => t.length > 1 && t.length < 80)"
)
unique = list(dict.fromkeys(all_texts))
known = ["Miscellaneous", "Fashion", "Art", "Books", "Food", "Travel", "Engineering", "News", "Education", "Fitness", "Pets"]
matches = [t for t in unique if any(k in t for k in known)]
if matches:
print(f" 🔍 Found after hover: {matches[:5]}")
else:
# Nothing found on hover — try clicking instead
await parent.click()
await page.wait_for_timeout(2000)
all_texts = await page.eval_on_selector_all(
"div, span, li",
"els => els.map(e => e.textContent.trim()).filter(t => t.length > 1 && t.length < 80)"
)
unique = list(dict.fromkeys(all_texts))
matches = [t for t in unique if any(k in t for k in known)]
print(f" 🔍 Found after click: {matches[:5]}")
print(f" 📋 Page sample: {unique[:15]}")
# Find the scrollable dropdown container and scroll it to find the collection
# The dropdown contains the visible collection names — find its container
option = None
# Identify the dropdown container by finding an element that contains known collection names
dropdown = await page.evaluate_handle("""() => {
const known = ['Pets', 'Food & Recipes', 'Books & Reading', 'Art & Design', 'Travel & Places'];
const all = document.querySelectorAll('div, ul');
for (const el of all) {
const text = el.innerText || '';
const matches = known.filter(k => text.includes(k));
if (matches.length >= 3 && el.scrollHeight > el.clientHeight) {
return el;
}
}
return null;
}""")
dropdown_el = dropdown.as_element()
if dropdown_el:
print(f" 📦 Dropdown container found, scrolling...")
# Get the bounding box to mouse-wheel scroll over it
box = await dropdown_el.bounding_box()
if box:
cx = box["x"] + box["width"] / 2
cy = box["y"] + box["height"] / 2
await page.mouse.move(cx, cy)
await page.wait_for_timeout(300)
for scroll_attempt in range(15):
# Check if collection is now visible in DOM
els = await page.query_selector_all(
f"span:has-text('{collection_name}'), div:has-text('{collection_name}'), li:has-text('{collection_name}')"
)
for el in els:
try:
text = (await el.inner_text()).strip()
if collection_name in text and len(text) < len(collection_name) + 10:
option = el
break
except Exception:
continue
if option:
break
# Mouse wheel scroll over the dropdown
if box:
await page.mouse.wheel(0, 80)
await page.wait_for_timeout(300)
else:
print(f" ⚠️ Could not find scrollable dropdown container")
# Fallback: try clicking directly without scroll
els = await page.query_selector_all(
f"span:has-text('{collection_name}'), div:has-text('{collection_name}')"
)
for el in els:
try:
text = (await el.inner_text()).strip()
if collection_name in text and len(text) < len(collection_name) + 10:
option = el
break
except Exception:
continue
if option:
await option.click()
await page.wait_for_timeout(1000)
done = await page.query_selector("button:has-text('Listo'), button:has-text('Done')")
if done:
await done.click()
await page.wait_for_timeout(1000)
print(f" ✅ → '{collection_name}'")
else:
# Dump everything on the page to find where collections are rendered
texts = await page.eval_on_selector_all(
"div, span, button, li",
"els => els.map(e => e.textContent.trim()).filter(t => t.length > 1 && t.length < 80)"
)
unique = list(dict.fromkeys(texts))
# Look for any of our known collection names
known = ["Miscellaneous", "Fashion", "Art", "Books", "Food", "Travel", "Engineering", "News", "Education", "Fitness", "Pets"]
matches = [t for t in unique if any(k in t for k in known)]
print(f" ⚠️ Collection not found.")
print(f" 🔍 Collection-like text on page: {matches[:10]}")
print(f" 📋 All page text (first 20): {unique[:20]}")
await page.keyboard.press("Escape")
pass # continue to next post
except Exception as e:
print(f" ⚠️ Error: {e}")
# ─── MAIN ─────────────────────────────────────────────────────────────────────
async def main():
print("=" * 60)
print(" Instagram Saved Posts Organizer")
print("=" * 60)
if not ANTHROPIC_API_KEY:
print("\n❌ ANTHROPIC_API_KEY not set. Check your .env file.")
return
if not Path(SAVED_POSTS_FILE).exists():
print(f"\n❌ '{SAVED_POSTS_FILE}' not found in current directory.")
return
# Load posts from export
posts = load_export(SAVED_POSTS_FILE)
print(f"\n📂 Loaded {len(posts)} saved posts from export.")
# Resume support
progress = load_progress()
already_done = set(progress.keys())
remaining = [p for p in posts if p["url"] not in already_done]
print(f" Already classified: {len(already_done)} | Remaining: {len(remaining)}")
async with async_playwright() as p:
browser = await p.chromium.launch(headless=False, slow_mo=50)
context = await browser.new_context(
viewport={"width": 1280, "height": 900},
user_agent="Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 "
"(KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36"
)
page = await context.new_page()
await wait_for_login(page)
username = await get_username(page)
print(f" Detected username: @{username}")
# ── Step 1: Enrich + Classify ─────────────────────────────────────────
if remaining:
print(f"\n🔍 Enriching & classifying {len(remaining)} posts...")
for i, post in enumerate(remaining, 1):
print(f" [{i + len(already_done)}/{len(posts)}] {post['url']}")
post = await enrich_post(page, post)
status = []
if post["caption"]: status.append(f"caption: {post['caption'][:50]}")
if post["image_url"]: status.append("image ✓")
if status:
print(f" 📎 {' | '.join(status)}")
else:
print(f" ⚠️ No data found — will classify by author name only")
result = classify_post(post["caption"], post["image_url"], post["author"])
post["collection"] = result.get("collection", "Miscellaneous")
post["reason"] = result.get("reason", "")
print(f" → {post['collection']} ({post['reason']})")
progress[post["url"]] = post
save_progress(progress) # save after every post — safe to Ctrl+C and resume
sleep(FETCH_DELAY)
else:
print("\n✅ All posts already classified.")
# ── Step 2: Summary ───────────────────────────────────────────────────
all_posts = list(progress.values())
collection_map: dict[str, list] = {}
for post in all_posts:
col = post.get("collection", "Miscellaneous")
collection_map.setdefault(col, []).append(post)
print("\n📊 Classification Summary:")
for col, col_posts in sorted(collection_map.items(), key=lambda x: -len(x[1])):
print(f" {col}: {len(col_posts)} posts")
print(f"\n Collections: {len(collection_map)} | Total posts: {len(all_posts)}")
proceed = input("\n▶️ Proceed to create collections and move posts on Instagram? (y/n): ")
if proceed.lower() != "y":
print("Exiting. Run again anytime to continue.")
await browser.close()
return
# ── Step 3: Create collections ────────────────────────────────────────
print("\n📁 Creating collections on Instagram...")
existing = await get_existing_collections(page, username)
print(f" Existing: {existing or 'none'}")
for col_name in collection_map:
if col_name not in existing:
await create_collection(page, username, col_name)
sleep(ACTION_DELAY)
# ── Step 4: Move posts ────────────────────────────────────────────────
print("\n📌 Moving posts into collections...")
moved = set()
if Path(MOVED_FILE).exists():
with open(MOVED_FILE) as f:
moved = set(json.load(f))
remaining_posts = [p for p in all_posts if p["url"] not in moved]
print(f" Already moved: {len(moved)} | Remaining: {len(remaining_posts)}")
for i, post in enumerate(remaining_posts, 1):
col = post.get("collection", "Miscellaneous")
print(f" [{i}/{len(all_posts)}] → '{col}'")
await add_post_to_collection(page, post["url"], col)
moved.add(post["url"])
with open(MOVED_FILE, "w") as f:
json.dump(list(moved), f)
sleep(ACTION_DELAY)
print("\n🎉 Done!")
await browser.close()
if __name__ == "__main__":
asyncio.run(main())