wardyns-feditools/highestpost.py

55 lines
1.8 KiB
Python
Raw Normal View History

2022-11-23 01:07:54 -08:00
#!/usr/bin/python3.8
from mastodon import Mastodon
import os
import sys
import html2text
if os.path.exists('./.creds/') == False:
os.mkdir('./.creds')
if os.path.exists('./.creds/client.secret') == False:
instance = input('Please enter your instance: ')
if not instance[:4] == 'http':
instance = 'https://' + instance
Mastodon.create_app('Wardyns fedi tools', api_base_url = instance, to_file = './.creds/client.secret')
mastodon = Mastodon(
client_id = './.creds/client.secret',
)
if os.path.exists('./.creds/user.secret') == False:
username = input('Enter your username: ')
password = input('Enter your password: ')
mastodon.log_in(username=username, password=password, scopes=['read', 'write'], to_file='./.creds/user.secret')
mastodon = Mastodon(
access_token = './.creds/user.secret'
)
if len(sys.argv) < 2:
print('Expected 1 argument (account)')
quit()
else:
highestpost = None
oldest_status_id = None
htmlconvert = html2text.HTML2Text()
htmlconvert.ignore_links = True
htmlconvert.body_width = 0
account = sys.argv[1]
accid = mastodon.account_search(account)[0]['id']
while True:
statuses = mastodon.account_statuses(accid, max_id=oldest_status_id, limit=1000)
try:
oldest_status_id = statuses[-1]['id']
except IndexError:
print('Reached end of posts')
quit()
for status in statuses:
statusscore = status['reblogs_count'] + status['favourites_count']
if status['reblogged'] == False:
if highestpost == None or statusscore > highestpost['score']:
highestpost = {'score' : status['reblogs_count'] + status['favourites_count'], 'post' : status}
content = str(htmlconvert.handle(status['content']))
print(content)
print('Favorites: ' + str(status['favourites_count']) + ' Boosts: ' + str(status['reblogs_count']))
print('link: ' + status['url'])
print('-------')