60 lines
1.8 KiB
Python
60 lines
1.8 KiB
Python
#!/usr/bin/python3
|
|
# coding: utf-8
|
|
# -*- coding: utf-8 -*-
|
|
|
|
from PIL import Image
|
|
from io import BytesIO
|
|
from mastodon import Mastodon
|
|
import requests, os, sys, time, json, argparse, 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:
|
|
if os.path.isfile('./save_media/'+media.remote_url[-20:]):
|
|
sys.exit(0)
|
|
|
|
if media.remote_url[-3:] != "mp4":
|
|
print(media.remote_url[-20:])
|
|
response = requests.get(media.remote_url)
|
|
pattern = Image.open(BytesIO(response.content), "r").convert('RGB')
|
|
pattern.save('save_media/'+media.remote_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)
|
|
while masto_allfav != []:
|
|
for toot in masto_allfav:
|
|
if toot.account.acct == "account@mastodon.tld":
|
|
get_media()
|
|
masto_allfav = mastodon.fetch_next(masto_allfav)
|
|
time.sleep(10)
|
|
|