wardyns-feditools/highestpost.py

59 lines
2.0 KiB
Python
Raw Normal View History

2022-11-23 01:07:54 -08:00
#!/usr/bin/python3.8
import os
import sys
import html2text
2023-01-26 19:40:26 -08:00
import json
import requests
parent = os.path.dirname(os.path.realpath(__file__))
2022-11-23 01:07:54 -08:00
2023-01-26 19:40:26 -08:00
if not os.path.exists(os.path.join(parent, 'config.json')):
generate_config("Wardyn's feditools", "read write follow push")
with open(os.path.join(parent, 'config.json'), 'r') as config_file:
config = json.load(config_file)
session = requests.Session()
session.headers.update({"Authorization" : "Bearer " + config['user_token']})
2022-11-23 01:07:54 -08:00
2023-01-26 19:40:26 -08:00
if len(sys.argv) != 2:
print('Expected 1 argument (account)')
quit()
2022-11-23 01:07:54 -08:00
2023-01-26 19:40:26 -08:00
highestpost = None
oldest_status_id = None
htmlconvert = html2text.HTML2Text()
htmlconvert.ignore_links = True
htmlconvert.body_width = 0
account = sys.argv[1]
2022-11-23 01:07:54 -08:00
2023-01-26 19:40:26 -08:00
if account[0] == '@':
account = account[1:]
if len(account.split('@')) == 1:
account = account + '@' + config['instance'].split('/')[-1]
2022-11-23 01:07:54 -08:00
2023-01-26 19:40:26 -08:00
searchlist = session.get(config['instance'] + '/api/v2/search?', params={'q':account, 'type':'accounts'}).json()['accounts']
acc_id = None
for user in searchlist:
if user['fqn'].lower() == account.lower():
acc_id = user['id']
acc_name = user['fqn']
break
if not acc_id:
print("Could not find user: " + account)
2022-11-23 01:07:54 -08:00
quit()
2023-01-26 19:40:26 -08:00
#accid = session.get(config['instance'] + '/api/v2/search?', params={'q':account, 'type':'accounts'}).json()['accounts'][0]['id']
while True:
statuses = session.get(config['instance'] + '/api/v1/accounts/' + acc_id + '/statuses', params={"max_id":oldest_status_id, "limit":40, "exclude_reblogs":True}).json()
try:
oldest_status_id = statuses[-1]['id']
except IndexError:
print('Reached end of posts')
quit()
for status in statuses:
status_score = status['reblogs_count'] + status['favourites_count']
if highestpost == None or status_score > 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('-------')