#!/usr/bin/env python3 from mastodon import Mastodon import os import requests import datetime #import json def mastocats(tag = 'mastocats', server = 'mastodon.social', maxres = 10): print("Tag: %s" % tag) print("Server: %s" % server) print("Downloading toots") def default(o): if isinstance(o, (datetime.date, datetime.datetime)): return o.isoformat() mastodon = Mastodon(api_base_url = "https://" + server) toots = [] last_id = None while True: statuses = list(mastodon.timeline_hashtag(tag, local=False, max_id=last_id)) if not statuses: break if len(toots) >= maxres: break toots += list(map( lambda s: { 'id': s['id'], 'created': s['created_at'], 'media': s['media_attachments'], 'account': s['account']['url'] } , statuses)) last_id = statuses[-1]['id'] print(len(toots)) #print(toots) #print(json.dumps(toots, indent=4, default=default)) print("Downloading media") os.makedirs('media', exist_ok=True) for toot in toots: for media in toot['media']: ext = os.path.splitext(media['preview_url'])[1] acc = toot['account'].replace("http://","").replace("https://","").replace("/","_") fname = "|".join([toot['created'].strftime('%Y%m%d-%H%M%S'), acc, str(toot['media'].index(media) + 1)]) + ext img_name = os.path.join('media', fname) if not os.path.isfile(img_name): print(img_name) img_data = requests.get(media['preview_url']).content with open(img_name, 'wb') as handler: handler.write(img_data) print("") if __name__ == "__main__": for tag in ['mastocats', 'catsofmastodon', 'cats', 'caturday', 'catstodon']: for server in ['mastodon.social', 'mastodon.bida.im', 'chaos.social']: mastocats(tag, server, 100)