ActiPubTagBooster/reboostTag.py

80 lines
2.0 KiB
Python
Raw Normal View History

2019-06-15 18:31:53 +00:00
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import os
import sys
import io
import json
from toot.console import main
from argparse import ArgumentParser
parser = ArgumentParser()
parser.add_argument("-c", "--count", type=int, help="How many toots to read")
parser.add_argument("-t", "--tag", type=str, help="Which tag to follow")
args = parser.parse_args()
tag = args.tag
count = args.count
toot_exec = "./toot-cli"
# Handle the last seen file
last_seen_file = "/tmp/reboost.{}.lastseen".format( tag )
# Touch last seen file if not exists
if( os.path.isfile( last_seen_file ) ):
wrapper = open(last_seen_file, 'r',encoding="utf-8")
last_seen = next(wrapper)
else:
open(last_seen_file, 'w').close()
last_seen = 0
# Build a fake list of args to run the toot main() function on
sim_args = [sys.argv[0], 'timeline', '-t', tag, '-c', str(count), '--json', '-1']
sys.argv = sim_args
# redirect sys.stdout to a buffer
stdout = sys.stdout
sys.stdout = io.StringIO()
# Call the toot library to get a list of toots
main()
# get output and restore sys.stdout
output = sys.stdout.getvalue()
sys.stdout = stdout
# Convert the received JSON to a list
itemList = json.loads( output)
# Contains the first item id in the list, to be stored
most_recent = None
print ('Running with last_seen being: {}'.format(last_seen))
# Loop through items
for item in itemList:
# Set the most recent id
if None == most_recent :
most_recent = item['id']
# if never seen, and therefore recent, reboost
if( last_seen != item['id'] ):
print("Reboosting item#{}".format(item['id']) )
# Build a fake list of args to run the toot main() function on
sim_args = [sys.argv[0], 'reblog', str(item['id'])]
sys.argv = sim_args
main()
# We've seen you already that toot, break
else:
break;
if( last_seen != most_recent) :
print( "Saving most_recent as last_seen: {}".format(most_recent))
handle = open(last_seen_file, 'w')
handle.write(most_recent)
handle.close()