ActiPubTagBooster/reboostTag.py

96 lines
2.6 KiB
Python
Raw Normal View History

2019-06-15 18:31:53 +00:00
#!/usr/bin/python3
# -*- coding: utf-8 -*-
2019-06-15 18:57:44 +00:00
"""
"""
2019-06-15 18:31:53 +00:00
import os
2019-06-15 18:57:44 +00:00
from os.path import isfile, isdir, dirname, join, realpath
2019-06-15 18:31:53 +00:00
import sys
import io
import json
from toot.console import main
2019-06-15 18:57:44 +00:00
# Command line Arguments
2019-06-15 18:31:53 +00:00
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")
2019-06-15 18:57:44 +00:00
parser.add_argument("-p", "--cache-path", type=str, default="/var/cache/reboostTag", help="Which path to use for storing ")
2019-06-15 18:31:53 +00:00
args = parser.parse_args()
2019-06-15 18:57:44 +00:00
# Variables
tag = args.tag # The tag to reboost
count = args.count # The number of toots to load
cache_path = args.cache_path # The tag file cache path
most_recent = None # The first item id in the loaded list
# last_seen_file is the cache file for a given tag
last_seen_file = join(cache_path, "reboost.{}.lastseen".format( tag ))
# Exit if cache path doesn't exist
if( not isdir( cache_path) ):
print( "Critical error: please create directory {}".format( cache_path) )
os._exit(2)
2019-06-15 18:31:53 +00:00
# Touch last seen file if not exists
2019-06-15 18:57:44 +00:00
if( isfile( last_seen_file ) ):
2019-06-15 18:31:53 +00:00
wrapper = open(last_seen_file, 'r',encoding="utf-8")
last_seen = next(wrapper)
else:
2019-06-15 18:57:44 +00:00
try:
open(last_seen_file, 'w').close()
last_seen = 0
except IOError as e:
print("Couldn't open or write to file (%s)." % e)
2019-06-15 18:31:53 +00:00
# redirect sys.stdout to a buffer
stdout = sys.stdout
sys.stdout = io.StringIO()
2019-06-15 18:57:44 +00:00
# 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
2019-06-15 18:31:53 +00:00
main()
2019-06-15 18:57:44 +00:00
# get main() output and restore sys.stdout
2019-06-15 18:31:53 +00:00
output = sys.stdout.getvalue()
sys.stdout = stdout
# Convert the received JSON to a list
itemList = json.loads( output)
# Loop through items
2019-06-15 18:57:44 +00:00
print ('Running with last_seen being: {}'.format(last_seen))
2019-06-15 18:31:53 +00:00
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
2019-06-15 18:57:44 +00:00
#main()
2019-06-15 18:31:53 +00:00
# We've seen you already that toot, break
else:
break;
2019-06-15 18:57:44 +00:00
# Update the cache file with new value, if exists
2019-06-15 18:31:53 +00:00
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()