Created
September 19, 2025 13:49
-
-
Save extratone/9cf876cda331e9d012d562cd8428d599 to your computer and use it in GitHub Desktop.
A script querying the mastodon API for the raw file URLs to all of a user's audio attachments.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import requests | |
| import sys | |
| # --- Configuration --- | |
| INSTANCE = "mastodon.social" | |
| USER_HANDLE = "[email protected]" | |
| ACCESS_TOKEN = "YOUR_ACCESS_TOKEN" | |
| # --- End Configuration --- | |
| def main(): | |
| """ | |
| Main function to orchestrate fetching the user ID and processing statuses. | |
| """ | |
| if ACCESS_TOKEN == "YOUR_ACCESS_TOKEN": | |
| print("Error: Please replace 'YOUR_ACCESS_TOKEN' with your actual access token.", file=sys.stderr) | |
| sys.exit(1) | |
| headers = {'Authorization': f'Bearer {ACCESS_TOKEN}'} | |
| # Step 1: Look up the Account ID from the handle | |
| try: | |
| print(f"Looking up Account ID for {USER_HANDLE}...", file=sys.stderr) | |
| lookup_url = f"https://{INSTANCE}/api/v1/accounts/lookup" | |
| params = {'acct': USER_HANDLE} | |
| response = requests.get(lookup_url, headers=headers, params=params) | |
| response.raise_for_status() # Raises an exception for bad status codes (4xx or 5xx) | |
| account_id = response.json().get('id') | |
| if not account_id: | |
| print(f"Error: Could not find Account ID for {USER_HANDLE}.", file=sys.stderr) | |
| sys.exit(1) | |
| print(f"Found Account ID: {account_id}", file=sys.stderr) | |
| print("---", file=sys.stderr) | |
| except requests.exceptions.RequestException as e: | |
| print(f"Error during account lookup: {e}", file=sys.stderr) | |
| sys.exit(1) | |
| # Step 2: Fetch all statuses and extract audio URLs | |
| all_audio_urls = [] | |
| # Initial URL for the first page of statuses, requesting the max limit | |
| next_page_url = f"https://{INSTANCE}/api/v1/accounts/{account_id}/statuses?limit=40" | |
| while next_page_url: | |
| try: | |
| print(f"Fetching page: {next_page_url}", file=sys.stderr) | |
| response = requests.get(next_page_url, headers=headers) | |
| response.raise_for_status() | |
| statuses = response.json() | |
| if not statuses: | |
| # No more statuses on this page, we are done. | |
| break | |
| for status in statuses: | |
| # A status might not have any attachments | |
| if 'media_attachments' in status and status['media_attachments']: | |
| for attachment in status['media_attachments']: | |
| if attachment.get('type') == 'audio': | |
| audio_url = attachment.get('url') | |
| if audio_url: | |
| print(audio_url) # Print URL directly to stdout | |
| all_audio_urls.append(audio_url) | |
| # The 'requests' library conveniently parses the Link header for us | |
| if 'next' in response.links: | |
| next_page_url = response.links['next']['url'] | |
| else: | |
| # No 'next' link means we've reached the last page | |
| next_page_url = None | |
| except requests.exceptions.RequestException as e: | |
| print(f"Error fetching statuses: {e}", file=sys.stderr) | |
| break | |
| print("---", file=sys.stderr) | |
| print(f"Script finished. Found {len(all_audio_urls)} audio attachments.", file=sys.stderr) | |
| if __name__ == "__main__": | |
| main() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The Sassa change Banking details tool offers real-time updates on grant applications in 2025. Our website uses the official SASSA API to provide accurate information about your social grant status.