Python command line used in a crontab to monitor toots using a specific tag and auto boosting them
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

reboostTag.py 2.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #!/usr/bin/python3
  2. # -*- coding: utf-8 -*-
  3. """
  4. """
  5. import os
  6. from os.path import isfile, isdir, dirname, join, realpath
  7. import sys
  8. import io
  9. import json
  10. from datetime import datetime
  11. from toot.console import main
  12. # Command line Arguments
  13. from argparse import ArgumentParser
  14. parser = ArgumentParser()
  15. parser.add_argument("-d", "--dry-run", default=False, action="store_true", help="Don't reboost")
  16. parser.add_argument("-c", "--count", type=int, help="How many toots to read")
  17. parser.add_argument("-t", "--tag", type=str, help="Which tag to follow")
  18. parser.add_argument("-p", "--cache-path", type=str, default="/var/cache/ActiPubTagBooster", help="Which path to use for storing ")
  19. args = parser.parse_args()
  20. # Variables
  21. dry_run = args.dry_run # The safe mode
  22. tag = args.tag # The tag to reboost
  23. count = args.count # The number of toots to load
  24. cache_path = args.cache_path # The tag file cache path
  25. most_recent = None # The first item id in the loaded list
  26. # last_seen_file is the cache file for a given tag
  27. last_seen_file = join(cache_path, "reboost.{}.lastseen".format( tag ))
  28. # Exit if cache path doesn't exist
  29. if( not isdir( cache_path) ):
  30. print( "Critical error: please create directory {}".format( cache_path) )
  31. os._exit(2)
  32. # Touch last seen file if not exists
  33. if( isfile( last_seen_file ) ):
  34. wrapper = open(last_seen_file, 'r',encoding="utf-8")
  35. last_seen = next(wrapper)
  36. else:
  37. try:
  38. open(last_seen_file, 'w').close()
  39. last_seen = 0
  40. except IOError as e:
  41. print("Couldn't open or write to file (%s)." % e)
  42. # redirect sys.stdout to a buffer
  43. stdout = sys.stdout
  44. sys.stdout = io.StringIO()
  45. # Build a fake list of args to run the toot main() function on
  46. sim_args = [sys.argv[0], 'timeline', '-t', tag, '-c', str(count), '--json', '-1']
  47. sys.argv = sim_args
  48. main()
  49. # get main() output and restore sys.stdout
  50. output = sys.stdout.getvalue()
  51. sys.stdout = stdout
  52. # Convert the received JSON to a list
  53. itemList = json.loads( output)
  54. # Loop through items
  55. #print ('Running with last_seen being: {}'.format(last_seen))
  56. for item in itemList:
  57. # Set the most recent id
  58. if None == most_recent :
  59. most_recent = item['id']
  60. # if never seen, and therefore recent, reboost
  61. if( last_seen != item['id'] and dry_run != True ):
  62. print("{}; Reboosting item #{} by {}".format(datetime.now().strftime("%y/%m/%d %H:%M:%S"),item['id'], item['account']['acct']) )
  63. # Build a fake list of args to run the toot main() function on
  64. sim_args = [sys.argv[0], 'reblog', str(item['id'])]
  65. sys.argv = sim_args
  66. main()
  67. # We've seen you already that toot, break
  68. else:
  69. break;
  70. # Update the cache file with new value, if exists
  71. if( last_seen != most_recent) :
  72. print( "Saving most_recent as last_seen: {}".format(most_recent))
  73. handle = open(last_seen_file, 'w')
  74. handle.write(most_recent)
  75. handle.close()