Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #
- # Bluesky followers script -- writes a target account's followers (handle, display name, bio) to stdout in CSV form
- #
- import atproto
- import argparse
- import os
- def clean_text(text):
- if text is None:
- return ""
- text = ' '.join(text.splitlines())
- text.replace("\"", " ")
- return text
- parser = argparse.ArgumentParser()
- parser.add_argument("--username", type=str, default=None, help="your Bsky account handle/did, if not present, tries env variable BSKY_USERNAME.")
- parser.add_argument("--pwd", type=str, default=None, help="your Bsky account password, if not present, tries env variable BSKY_PWD.")
- parser.add_argument("--target", type=str, default=None, help="returns CSV with followers of the target account (your account if absent)")
- args = parser.parse_args()
- __username__ = args.username
- __pwd__ = args.pwd
- __targetacct__ = args.target
- if __username__ is None:
- __username__ = os.environ.get('BSKY_USERNAME')
- if __pwd__ is None:
- __pwd__ = os.environ.get('BSKY_PWD')
- if __targetacct__ is None:
- __targetacct__ = __username__
- if __username__ is None or __pwd__ is None:
- print("please supply a Bluesky login via command line options or env flags")
- exit(1)
- client = atproto.Client()
- client.login(__username__, __pwd__)
- crsr = None
- all_flwrs = []
- while True:
- data = client.get_followers(actor = __targetacct__, limit = 100, cursor = crsr)
- flwrs = data.followers
- if len(flwrs) == 0:
- break
- all_flwrs = all_flwrs + flwrs
- crsr = data.cursor
- if crsr is None:
- break
- for flwr in all_flwrs:
- filter_display = clean_text(flwr.display_name)
- filter_desc = clean_text(flwr.description)
- print(f"{flwr.handle},\"{filter_display}\",\"{filter_desc}\"")
- # <<<END FIRST SCRIPT>>>
- # Note that there is currently a 100 followers request/day rate limit in the API
- #
- # Bluesky block script -- block users from a file list; you can provide a separate list of users to ignore
- #
- from atproto import Client, models
- import argparse
- import os
- import time
- def handle_from_line(line):
- if line.find(",") < 0:
- return line
- ss = line.split(",")
- return ss[0]
- parser = argparse.ArgumentParser()
- parser.add_argument("--username", type=str, default=None, help="your Bsky account handle/did, if not present, tries env variable BSKY_USERNAME.")
- parser.add_argument("--pwd", type=str, default=None, help="your Bsky account password, if not present, tries env variable BSKY_PWD.")
- parser.add_argument("--target", type=str, default=None, help="a file containing a list of bsky handles to block (CSV is fine, first field is taken)")
- parser.add_argument("--mercy", type=str, default=None, help="a file containing a list of bsky handles to show mercy to (CSV is fine, first field is taken)")
- args = parser.parse_args()
- __username__ = args.username
- __pwd__ = args.pwd
- __target__ = args.target
- if __username__ is None:
- __username__ = os.environ.get('BSKY_USERNAME')
- if __pwd__ is None:
- __pwd__ = os.environ.get('BSKY_PWD')
- if __target__ is None:
- print("please supply a target list of accounts to block")
- exit(2)
- if __username__ is None or __pwd__ is None:
- print("please supply a Bluesky login via command line options or env flags")
- exit(1)
- client = Client()
- client.login(__username__, __pwd__)
- mercy = []
- if args.mercy is not None:
- with open(args.mercy, 'r') as mfile:
- for line in mfile:
- handle = handle_from_line(line)
- mercy.append(handle)
- print(f"{len(mercy)} handles found in the mercy list.")
- else:
- input("No mercy list provided, you may break the script or press 'enter' to continue anyway.")
- with open(__target__, 'r') as file:
- for line in file:
- handle = handle_from_line(line)
- if any(handle == x for x in mercy):
- print(f"*** handle {handle} found in the mercy list!")
- continue
- try:
- did = client.resolve_handle(handle).did
- except:
- continue
- if did is None:
- continue
- print(f"blocking {handle} -- {did}...")
- block_record = models.AppBskyGraphBlock.Record(
- subject=did,
- created_at=client.get_current_time_iso()
- )
- uri = client.app.bsky.graph.block.create(client.me.did, block_record).uri
- time.sleep(1)
- # <<<END SECOND SCRIPT>>>
- # Note that there is a combined 5000 requests/hour rate limit on interactions (blocks, likes, skeets/re-skeets, etc.)
- # in the API. if you are active on bsky at the same time the block script is running, sleep(1) may be insufficient
- #
- # If you would like a script for the bluesky accounts a target account is following (rather than the
- # accounts following the target), it's exactly like the first script, except change the line:
- #
- # data = client.get_followers(actor = __targetacct__, limit = 100, cursor = crsr)
- #
- # to:
- #
- # data = client.get_follows(actor = __targetacct__, limit = 100, cursor = crsr)
- #
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement