wardyns-feditools/highestpost.py

59 lines
2.0 KiB
Python
Executable File

#!/usr/bin/env python
import os
import sys
import html2text
import json
import requests
parent = os.path.dirname(os.path.realpath(__file__))
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']})
if len(sys.argv) != 2:
print('Expected 1 argument (account)')
quit()
highestpost = None
oldest_status_id = None
htmlconvert = html2text.HTML2Text()
htmlconvert.ignore_links = True
htmlconvert.body_width = 0
account = sys.argv[1]
if account[0] == '@':
account = account[1:]
if len(account.split('@')) == 1:
account = account + '@' + config['instance'].split('/')[-1]
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)
quit()
#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('-------')