Skip to content

Instantly share code, notes, and snippets.

@RaphaelWimmer
Created January 23, 2024 20:03
Show Gist options
  • Save RaphaelWimmer/89c47bba16035a4886f8b9d412b4d334 to your computer and use it in GitHub Desktop.
Save RaphaelWimmer/89c47bba16035a4886f8b9d412b4d334 to your computer and use it in GitHub Desktop.
get images from #antifastickers Mastodon posts
#!/usr/bin/env python3
# get_antifastickers.py
# download the first image from each #antifasticker post found on Mastodon
# make sure to create the download directory ("./img") first
# CC-0 / Public Domain
import mastodon
import urllib.request
ACCESS_TOKEN = "..."
API_BASE_URL = "https://example.com"
DL_DIR="./img"
m = mastodon.Mastodon(access_token=ACCESS_TOKEN, api_base_url=API_BASE_URL)
offset = 0
downloaded = 0
while True:
res = m.search(q="#antifasticker", result_type="statuses", offset=offset, min_id=0)
stats = res['statuses']
if len(stats) == 0:
break
for stat in stats:
if 'media_attachments' in stat.keys() and len(stat['media_attachments']) > 0:
#url = stat['media_attachments'][0]['remote_url']
url = stat['media_attachments'][0]['url'] # from local server
if url:
filename = DL_DIR + "/" + str(stat['id']) + "-" + url.split("/")[-1]
local_filename, headers = urllib.request.urlretrieve(url, filename=filename)
print(stat['id'], url, "->", local_filename)
downloaded += 1
else:
print("No URL:", stat)
offset += len(stats)
print(f"got {downloaded} images")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment