#!/usr/bin/python3 # coding: utf-8 # -*- coding: utf-8 -*- from PIL import Image from io import BytesIO from mastodon import Mastodon import requests import os import sys import time import json import argparse import math def get_parameter( parameter, file_path ): # Check if secrets file exists if not os.path.isfile(file_path): print("File %s not found, exiting."%file_path) sys.exit(0) # Find parameter in file with open( file_path ) as f: for line in f: if line.startswith( parameter ): return line.replace(parameter + ":", "").strip() # Cannot find parameter, exit print(file_path + " Missing parameter %s "%parameter) sys.exit(0) def get_media(): media_attachments = toot.media_attachments for media in media_attachments: print(media.url[-20:]) response = requests.get(media.remote_url) pattern = Image.open(BytesIO(response.content), "r").convert('RGB') pattern.save('save_media/'+media.url[-20:]) secrets_filepath = "secrets/secrets.txt" uc_client_id = get_parameter("client_id", secrets_filepath) uc_client_secret = get_parameter("client_secret", secrets_filepath) uc_access_token = get_parameter("access_token", secrets_filepath) mastodon_hostname = get_parameter("mastodon_hostname", secrets_filepath) mastodon = Mastodon( client_id = uc_client_id, client_secret = uc_client_secret, access_token = uc_access_token, api_base_url = 'https://' + mastodon_hostname, ) masto_allfav = mastodon.favourites(None, None, 40) for toot in masto_allfav: if toot.account.acct == "account@domain.com": get_media() else : if toot.account.acct == "otheraccount@domain.com": get_media()