Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- BLUESKY_HANDLE = "" # put your handle here eg "monkey123.bsky.social"
- BLUESKY_APP_PASSWORD = "" # app password here eg "abcd-efgh-ijkl-mnop"
- BLUESKY_LOGIN_PASSWORD = "" # login password here eg "god123Iamsmartthisissecure.net"
- import requests
- from datetime import datetime, timedelta, timezone
- import time
- def authenticate(handle, password):
- response = requests.post(
- "https://bsky.social/xrpc/com.atproto.server.createSession",
- json={"identifier": handle, "password": password},
- )
- response.raise_for_status()
- return response.json()["accessJwt"]
- auth_token = authenticate(BLUESKY_HANDLE, BLUESKY_APP_PASSWORD)
- # Function to fetch posts
- def fetch_posts(handle, auth_token):
- headers = {"Authorization": f"Bearer {auth_token}"}
- posts = []
- cursor = None
- while True:
- params = {'actor': handle, 'limit': 100}
- if cursor:
- params['cursor'] = cursor
- response = requests.get(
- "https://bsky.social/xrpc/app.bsky.feed.getAuthorFeed",
- headers=headers,
- params=params
- )
- response.raise_for_status()
- data = response.json()
- posts.extend(data["feed"])
- cursor = data.get("cursor")
- if not cursor:
- break
- return posts
- # Function to delete a post
- def delete_post(uri, auth_token):
- headers = {"Authorization": f"Bearer {auth_token}"}
- # Extract repo (DID), collection, and recordKey from URI
- try:
- parts = uri.split("/")
- repo = parts[2].replace("at://", "") # Extract DID
- collection = parts[3] # Usually "app.bsky.feed.post"
- record_key = parts[4] # Unique ID for the post
- except IndexError:
- print(f"Invalid URI format: {uri}")
- return
- payload = {
- "repo": repo,
- "collection": collection,
- "rkey": record_key
- }
- response = requests.post(
- "https://bsky.social/xrpc/com.atproto.repo.deleteRecord",
- json=payload,
- headers=headers,
- )
- if response.status_code == 200:
- print(f"Deleted post: {uri}")
- else:
- print(f"Failed to delete post {uri}: {response.status_code} {response.text}")
- def fetch_likes(auth_token, repo):
- headers = {"Authorization": f"Bearer {auth_token}"}
- response = requests.get(
- f"https://bsky.social/xrpc/com.atproto.repo.listRecords?repo={repo}&collection=app.bsky.feed.like&limit=100",
- headers=headers,
- )
- response.raise_for_status()
- return response.json().get("records", [])
- # Function to remove a like
- def remove_like(uri, cid, auth_token):
- headers = {"Authorization": f"Bearer {auth_token}"}
- # Extract repo (DID), collection, and recordKey from URI
- try:
- parts = uri.split("/")
- repo = parts[2].replace("at://", "") # Extract DID
- collection = parts[3] # Usually "app.bsky.feed.like"
- record_key = parts[4] # Unique ID for the like
- except IndexError:
- print(f"Invalid URI format: {uri}")
- return
- payload = {
- "repo": repo,
- "collection": "app.bsky.feed.like", # Likes are stored under this collection
- "rkey": record_key
- }
- response = requests.post(
- "https://bsky.social/xrpc/com.atproto.repo.deleteRecord",
- json=payload,
- headers=headers,
- )
- if response.status_code == 200:
- print(f"Removed like: {uri}")
- else:
- print(f"Failed to remove like {uri}: {response.status_code} {response.text}")
- # Define the age threshold for old posts
- days_threshold = 0
- cnt_posts = 0
- posts = fetch_posts(BLUESKY_HANDLE, auth_token)
- # Delete old posts
- cnt_posts = 0
- threshold_date = datetime.now(timezone.utc) - timedelta(days=days_threshold)
- def parse_bsky_timestamp(s):
- try:
- return datetime.strptime(s, "%Y-%m-%dT%H:%M:%S.%fZ")
- except ValueError:
- return datetime.strptime(s, "%Y-%m-%dT%H:%M:%SZ")
- for post in posts:
- post_date = parse_bsky_timestamp(post["post"]["record"]["createdAt"]).replace(tzinfo=timezone.utc)
- if post_date < threshold_date and post["post"]["author"]["handle"] == BLUESKY_HANDLE:
- delete_post(post["post"]["uri"], auth_token)
- cnt_posts += 1
- time.sleep(0.1) # Avoid potential rate limits
- print(f"Deleted {cnt_posts} posts.")
- def remove_like(uri, auth_token):
- headers = {"Authorization": f"Bearer {auth_token}"}
- # Extract repo (DID), collection, and recordKey from URI
- try:
- parts = uri.split("/")
- repo = parts[2].replace("at://", "") # Extract DID
- collection = parts[3] # Should be "app.bsky.feed.like"
- record_key = parts[4] # Unique like ID
- except IndexError:
- print(f"Invalid URI format: {uri}")
- return
- payload = {
- "repo": repo,
- "collection": collection, # Should be "app.bsky.feed.like"
- "rkey": record_key # The unique identifier for the like
- }
- response = requests.post(
- "https://bsky.social/xrpc/com.atproto.repo.deleteRecord",
- json=payload,
- headers=headers,
- )
- if response.status_code == 200:
- print(f"Removed like: {uri}")
- else:
- print(f"Failed to remove like {uri}: {response.status_code} {response.text}")
- seen = set()
- cnt_likes = 0
- ignored_assertion = 0
- while True:
- print("****************************** batch *********************** ")
- this_round = 0
- likes = fetch_likes(auth_token, BLUESKY_HANDLE)
- for like in likes:
- uri = like["uri"] # Get the like record's URI
- # assert uri not in seen, "should not have duplicate."
- if uri in seen:
- ignored_assertion += 1
- assert ignored_assertion < 301, "My patience wears thin. Avoiding infinite loop"
- continue # don't abort
- seen.add(uri)
- remove_like(uri, auth_token)
- cnt_likes += 1
- this_round += 1
- if this_round==0:
- break
- print(f"Removed {cnt_likes} likes.")
- # -------------------------------
- # Remove Reposts
- # -------------------------------
- def fetch_reposts(auth_token, repo, cursor=None):
- headers = {"Authorization": f"Bearer {auth_token}"}
- params = {
- "repo": repo,
- "collection": "app.bsky.feed.repost",
- "limit": 100
- }
- if cursor:
- params["cursor"] = cursor
- r = requests.get(
- "https://bsky.social/xrpc/com.atproto.repo.listRecords",
- headers=headers,
- params=params,
- )
- r.raise_for_status()
- data = r.json()
- return data.get("records", []), data.get("cursor")
- def remove_repost(uri, auth_token):
- headers = {"Authorization": f"Bearer {auth_token}"}
- try:
- parts = uri.split("/")
- repo = parts[2].replace("at://", "")
- collection = parts[3]
- record_key = parts[4]
- except IndexError:
- print(f"Invalid URI format: {uri}")
- return
- payload = {
- "repo": repo,
- "collection": "app.bsky.feed.repost",
- "rkey": record_key
- }
- r = requests.post(
- "https://bsky.social/xrpc/com.atproto.repo.deleteRecord",
- headers=headers,
- json=payload,
- )
- if r.status_code == 200:
- print(f"Deleted repost: {uri}")
- else:
- print(f"Failed to delete repost {uri}: {r.status_code} {r.text}")
- print("\n== Deleting reposts ==")
- removed_reposts = 0
- seen_reposts = set()
- cursor = None
- while True:
- reposts, cursor = fetch_reposts(auth_token, BLUESKY_HANDLE, cursor)
- if not reposts:
- break
- for rec in reposts:
- uri = rec["uri"]
- if uri in seen_reposts:
- continue
- seen_reposts.add(uri)
- remove_repost(uri, auth_token)
- removed_reposts += 1
- print(f"Deleted {removed_reposts} reposts.")
- # -------------------------------
- # Remove Blocks
- # -------------------------------
- def fetch_blocks(auth_token, repo, cursor=None):
- headers = {"Authorization": f"Bearer {auth_token}"}
- params = {
- "repo": repo,
- "collection": "app.bsky.graph.block",
- "limit": 100
- }
- if cursor:
- params["cursor"] = cursor
- r = requests.get(
- "https://bsky.social/xrpc/com.atproto.repo.listRecords",
- headers=headers,
- params=params
- )
- r.raise_for_status()
- data = r.json()
- return data.get("records", []), data.get("cursor")
- def remove_block(uri, auth_token):
- headers = {"Authorization": f"Bearer {auth_token}"}
- try:
- parts = uri.split("/")
- repo = parts[2].replace("at://", "") # DID
- collection = parts[3] # should be app.bsky.graph.block
- record_key = parts[4]
- except IndexError:
- print(f"Invalid URI format: {uri}")
- return
- payload = {
- "repo": repo,
- "collection": "app.bsky.graph.block",
- "rkey": record_key
- }
- r = requests.post(
- "https://bsky.social/xrpc/com.atproto.repo.deleteRecord",
- headers=headers,
- json=payload
- )
- if r.status_code == 200:
- print(f"Deleted block: {uri}")
- else:
- print(f"Failed to delete block {uri}: {r.status_code} {r.text}")
- print("\n== Deleting blocks ==")
- removed_blocks = 0
- seen_blocks = set()
- cursor = None
- while True:
- blocks, cursor = fetch_blocks(auth_token, BLUESKY_HANDLE, cursor)
- if not blocks:
- break
- for rec in blocks:
- uri = rec["uri"]
- if uri in seen_blocks:
- continue
- seen_blocks.add(uri)
- remove_block(uri, auth_token)
- removed_blocks += 1
- print(f"Deleted {removed_blocks} blocks.")
Advertisement
Add Comment
Please, Sign In to add comment