lucgumshoe

Untitled

Dec 9th, 2025
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 9.72 KB | None | 0 0
  1. BLUESKY_HANDLE = "" # put your handle here eg "monkey123.bsky.social"
  2. BLUESKY_APP_PASSWORD = "" # app password here eg "abcd-efgh-ijkl-mnop"
  3. BLUESKY_LOGIN_PASSWORD = "" # login password here eg "god123Iamsmartthisissecure.net"
  4.  
  5. import requests
  6. from datetime import datetime, timedelta, timezone
  7. import time
  8.  
  9. def authenticate(handle, password):
  10.     response = requests.post(
  11.         "https://bsky.social/xrpc/com.atproto.server.createSession",
  12.         json={"identifier": handle, "password": password},
  13.     )
  14.     response.raise_for_status()
  15.     return response.json()["accessJwt"]
  16.  
  17. auth_token = authenticate(BLUESKY_HANDLE, BLUESKY_APP_PASSWORD)
  18.  
  19. # Function to fetch posts
  20. def fetch_posts(handle, auth_token):
  21.     headers = {"Authorization": f"Bearer {auth_token}"}
  22.     posts = []
  23.     cursor = None
  24.  
  25.     while True:
  26.         params = {'actor': handle, 'limit': 100}
  27.         if cursor:
  28.             params['cursor'] = cursor
  29.  
  30.         response = requests.get(
  31.             "https://bsky.social/xrpc/app.bsky.feed.getAuthorFeed",
  32.             headers=headers,
  33.             params=params
  34.         )
  35.         response.raise_for_status()
  36.         data = response.json()
  37.         posts.extend(data["feed"])
  38.  
  39.         cursor = data.get("cursor")
  40.         if not cursor:
  41.             break
  42.  
  43.     return posts
  44.  
  45.  
  46. # Function to delete a post
  47. def delete_post(uri, auth_token):
  48.     headers = {"Authorization": f"Bearer {auth_token}"}
  49.  
  50.     # Extract repo (DID), collection, and recordKey from URI
  51.     try:
  52.         parts = uri.split("/")
  53.         repo = parts[2].replace("at://", "")  # Extract DID
  54.         collection = parts[3]  # Usually "app.bsky.feed.post"
  55.         record_key = parts[4]  # Unique ID for the post
  56.     except IndexError:
  57.         print(f"Invalid URI format: {uri}")
  58.         return
  59.  
  60.     payload = {
  61.         "repo": repo,
  62.         "collection": collection,
  63.         "rkey": record_key
  64.     }
  65.  
  66.     response = requests.post(
  67.         "https://bsky.social/xrpc/com.atproto.repo.deleteRecord",
  68.         json=payload,
  69.         headers=headers,
  70.     )
  71.  
  72.     if response.status_code == 200:
  73.         print(f"Deleted post: {uri}")
  74.     else:
  75.         print(f"Failed to delete post {uri}: {response.status_code} {response.text}")
  76.  
  77. def fetch_likes(auth_token, repo):
  78.     headers = {"Authorization": f"Bearer {auth_token}"}
  79.     response = requests.get(
  80.         f"https://bsky.social/xrpc/com.atproto.repo.listRecords?repo={repo}&collection=app.bsky.feed.like&limit=100",
  81.         headers=headers,
  82.     )
  83.     response.raise_for_status()
  84.     return response.json().get("records", [])
  85.  
  86.  
  87. # Function to remove a like
  88. def remove_like(uri, cid, auth_token):
  89.     headers = {"Authorization": f"Bearer {auth_token}"}
  90.  
  91.     # Extract repo (DID), collection, and recordKey from URI
  92.     try:
  93.         parts = uri.split("/")
  94.         repo = parts[2].replace("at://", "")  # Extract DID
  95.         collection = parts[3]  # Usually "app.bsky.feed.like"
  96.         record_key = parts[4]  # Unique ID for the like
  97.     except IndexError:
  98.         print(f"Invalid URI format: {uri}")
  99.         return
  100.  
  101.     payload = {
  102.         "repo": repo,
  103.         "collection": "app.bsky.feed.like",  # Likes are stored under this collection
  104.         "rkey": record_key
  105.     }
  106.  
  107.     response = requests.post(
  108.         "https://bsky.social/xrpc/com.atproto.repo.deleteRecord",
  109.         json=payload,
  110.         headers=headers,
  111.     )
  112.  
  113.     if response.status_code == 200:
  114.         print(f"Removed like: {uri}")
  115.     else:
  116.         print(f"Failed to remove like {uri}: {response.status_code} {response.text}")
  117.  
  118. # Define the age threshold for old posts
  119. days_threshold = 0
  120. cnt_posts = 0
  121.  
  122. posts = fetch_posts(BLUESKY_HANDLE, auth_token)
  123.  
  124. # Delete old posts
  125. cnt_posts = 0
  126. threshold_date = datetime.now(timezone.utc) - timedelta(days=days_threshold)
  127.  
  128. def parse_bsky_timestamp(s):
  129.     try:
  130.         return datetime.strptime(s, "%Y-%m-%dT%H:%M:%S.%fZ")
  131.     except ValueError:
  132.         return datetime.strptime(s, "%Y-%m-%dT%H:%M:%SZ")
  133.  
  134.  
  135. for post in posts:
  136.     post_date = parse_bsky_timestamp(post["post"]["record"]["createdAt"]).replace(tzinfo=timezone.utc)
  137.  
  138.  
  139.     if post_date < threshold_date and post["post"]["author"]["handle"] == BLUESKY_HANDLE:
  140.         delete_post(post["post"]["uri"], auth_token)
  141.         cnt_posts += 1
  142.         time.sleep(0.1)  # Avoid potential rate limits
  143.  
  144. print(f"Deleted {cnt_posts} posts.")
  145.  
  146. def remove_like(uri, auth_token):
  147.     headers = {"Authorization": f"Bearer {auth_token}"}
  148.  
  149.     # Extract repo (DID), collection, and recordKey from URI
  150.     try:
  151.         parts = uri.split("/")
  152.         repo = parts[2].replace("at://", "")  # Extract DID
  153.         collection = parts[3]  # Should be "app.bsky.feed.like"
  154.         record_key = parts[4]  # Unique like ID
  155.     except IndexError:
  156.         print(f"Invalid URI format: {uri}")
  157.         return
  158.  
  159.     payload = {
  160.         "repo": repo,
  161.         "collection": collection,  # Should be "app.bsky.feed.like"
  162.         "rkey": record_key  # The unique identifier for the like
  163.     }
  164.  
  165.     response = requests.post(
  166.         "https://bsky.social/xrpc/com.atproto.repo.deleteRecord",
  167.         json=payload,
  168.         headers=headers,
  169.     )
  170.  
  171.     if response.status_code == 200:
  172.         print(f"Removed like: {uri}")
  173.     else:
  174.         print(f"Failed to remove like {uri}: {response.status_code} {response.text}")
  175.  
  176.  
  177.  
  178. seen = set()
  179.  
  180. cnt_likes = 0
  181.  
  182. ignored_assertion = 0
  183.  
  184. while True:
  185.     print("****************************** batch *********************** ")
  186.     this_round = 0
  187.     likes = fetch_likes(auth_token, BLUESKY_HANDLE)
  188.     for like in likes:
  189.         uri = like["uri"]  # Get the like record's URI
  190.         # assert uri not in seen, "should not have duplicate."
  191.         if uri in seen:
  192.             ignored_assertion += 1
  193.             assert ignored_assertion < 301, "My patience wears thin.  Avoiding infinite loop"
  194.             continue  # don't abort
  195.         seen.add(uri)
  196.         remove_like(uri, auth_token)
  197.         cnt_likes += 1
  198.         this_round += 1
  199.     if this_round==0:
  200.         break
  201.  
  202. print(f"Removed {cnt_likes} likes.")
  203.  
  204.  
  205. # -------------------------------
  206. # Remove Reposts
  207. # -------------------------------
  208.  
  209. def fetch_reposts(auth_token, repo, cursor=None):
  210.     headers = {"Authorization": f"Bearer {auth_token}"}
  211.     params = {
  212.         "repo": repo,
  213.         "collection": "app.bsky.feed.repost",
  214.         "limit": 100
  215.     }
  216.     if cursor:
  217.         params["cursor"] = cursor
  218.  
  219.     r = requests.get(
  220.         "https://bsky.social/xrpc/com.atproto.repo.listRecords",
  221.         headers=headers,
  222.         params=params,
  223.     )
  224.     r.raise_for_status()
  225.     data = r.json()
  226.     return data.get("records", []), data.get("cursor")
  227.  
  228. def remove_repost(uri, auth_token):
  229.     headers = {"Authorization": f"Bearer {auth_token}"}
  230.  
  231.     try:
  232.         parts = uri.split("/")
  233.         repo = parts[2].replace("at://", "")
  234.         collection = parts[3]
  235.         record_key = parts[4]
  236.     except IndexError:
  237.         print(f"Invalid URI format: {uri}")
  238.         return
  239.  
  240.     payload = {
  241.         "repo": repo,
  242.         "collection": "app.bsky.feed.repost",
  243.         "rkey": record_key
  244.     }
  245.  
  246.     r = requests.post(
  247.         "https://bsky.social/xrpc/com.atproto.repo.deleteRecord",
  248.         headers=headers,
  249.         json=payload,
  250.     )
  251.  
  252.     if r.status_code == 200:
  253.         print(f"Deleted repost: {uri}")
  254.     else:
  255.         print(f"Failed to delete repost {uri}: {r.status_code} {r.text}")
  256.  
  257. print("\n== Deleting reposts ==")
  258.  
  259. removed_reposts = 0
  260. seen_reposts = set()
  261.  
  262. cursor = None
  263. while True:
  264.     reposts, cursor = fetch_reposts(auth_token, BLUESKY_HANDLE, cursor)
  265.  
  266.     if not reposts:
  267.         break
  268.  
  269.     for rec in reposts:
  270.         uri = rec["uri"]
  271.         if uri in seen_reposts:
  272.             continue
  273.         seen_reposts.add(uri)
  274.  
  275.         remove_repost(uri, auth_token)
  276.         removed_reposts += 1
  277.  
  278. print(f"Deleted {removed_reposts} reposts.")
  279.  
  280.  
  281. # -------------------------------
  282. # Remove Blocks
  283. # -------------------------------
  284.  
  285. def fetch_blocks(auth_token, repo, cursor=None):
  286.     headers = {"Authorization": f"Bearer {auth_token}"}
  287.  
  288.     params = {
  289.         "repo": repo,
  290.         "collection": "app.bsky.graph.block",
  291.         "limit": 100
  292.     }
  293.     if cursor:
  294.         params["cursor"] = cursor
  295.  
  296.     r = requests.get(
  297.         "https://bsky.social/xrpc/com.atproto.repo.listRecords",
  298.         headers=headers,
  299.         params=params
  300.     )
  301.     r.raise_for_status()
  302.     data = r.json()
  303.     return data.get("records", []), data.get("cursor")
  304.  
  305. def remove_block(uri, auth_token):
  306.     headers = {"Authorization": f"Bearer {auth_token}"}
  307.  
  308.     try:
  309.         parts = uri.split("/")
  310.         repo = parts[2].replace("at://", "")   # DID
  311.         collection = parts[3]                 # should be app.bsky.graph.block
  312.         record_key = parts[4]
  313.     except IndexError:
  314.         print(f"Invalid URI format: {uri}")
  315.         return
  316.  
  317.     payload = {
  318.         "repo": repo,
  319.         "collection": "app.bsky.graph.block",
  320.         "rkey": record_key
  321.     }
  322.  
  323.     r = requests.post(
  324.         "https://bsky.social/xrpc/com.atproto.repo.deleteRecord",
  325.         headers=headers,
  326.         json=payload
  327.     )
  328.  
  329.     if r.status_code == 200:
  330.         print(f"Deleted block: {uri}")
  331.     else:
  332.         print(f"Failed to delete block {uri}: {r.status_code} {r.text}")
  333.  
  334. print("\n== Deleting blocks ==")
  335.  
  336. removed_blocks = 0
  337. seen_blocks = set()
  338.  
  339. cursor = None
  340. while True:
  341.     blocks, cursor = fetch_blocks(auth_token, BLUESKY_HANDLE, cursor)
  342.  
  343.     if not blocks:
  344.         break
  345.  
  346.     for rec in blocks:
  347.         uri = rec["uri"]
  348.         if uri in seen_blocks:
  349.             continue
  350.         seen_blocks.add(uri)
  351.  
  352.         remove_block(uri, auth_token)
  353.         removed_blocks += 1
  354.  
  355. print(f"Deleted {removed_blocks} blocks.")
  356.  
  357.  
  358.  
Advertisement
Add Comment
Please, Sign In to add comment