Various Mastodon Bots

rbu_rss.py 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. #!/usr/bin/python3
  2. from mastodon import Mastodon
  3. import os
  4. from logger import Logger
  5. from parser import Parser
  6. from format import formatStatus
  7. from util import getAbsolutePath
  8. class RBU_RSS(Mastodon):
  9. SECRET_DIR = getAbsolutePath(__file__, 'secrets/')
  10. email = 'zach@refactorcamp.org'
  11. password = os.environ['STALLMINATOR_PASSWORD']
  12. clientIDFile = 'rbu_clientcred.secret'
  13. accessTokenFile = 'rbu_usercred.secret'
  14. instanceURL = 'https://refactorcamp.org'
  15. blogs = {'https://www.ribbonfarm.com/feed/': '@vgr',
  16. 'http://feeds.akkartik.name/kartiks-scrapbook': '@akkartik@mastodon.social',
  17. 'https://meaningness.com/rss.xml': '@meaningness',
  18. 'https://swellandcut.com/feed/': '@msweet',
  19. 'https://www.spectology.com/feed.xml' : '@adrianryan',
  20. 'https://clutchofthedeadhand.com/rss/': '@johnhenry',
  21. 'http://meltingasphalt.com/feed/' : "Kevin Simler",
  22. 'https://subpixel.space/feed.xml' : '@telos',
  23. 'https://kneelingbus.net/feed/' : "Drew Austin",
  24. 'https://omniorthogonal.blogspot.com/feeds/posts/default' : "omniorthogonal",
  25. 'http://thesublemon.tumblr.com/rss' : "thesublemon",
  26. 'https://us1.campaign-archive.com/feed?u=78cbbb7f2882629a5157fa593&id=6b80b6e8da'
  27. : "@vgr",
  28. 'http://zenpundit.com/?feed=rss2' : "zenpundit",
  29. 'https://srconstantin.wordpress.com/feed/' : "srconstantin" ,
  30. 'https://putanumonit.com/feed/' : 'putanumonit',
  31. 'https://feeds.feedburner.com/Growwiser' : 'grow wiser',
  32. 'https://josephckelly.com/feed/' : 'Joseph Kelly',
  33. 'https://gravityandlevity.wordpress.com/feed/' : 'Brian Skinner',
  34. 'https://www.nousmachina.net/rss/' : '@hewhocutsdown',
  35. 'https://www.howell.io/rss.xml' : '@dehowell' ,
  36. 'https://scottwernerd.com/feed/' : '@scottwerner',
  37. 'http://secondforge.com/atom.xml' : '@jamescgibson',
  38. 'https://ariel-greenwood-e07u.squarespace.com/new-blog?format=rss':
  39. 'Ariel Greenwood',
  40. 'http://www.thehidinghand.com/feed' : '@stefanozorzi',
  41. 'http://msls.net/feed/' : '@bkam',
  42. 'https://medium.com/feed/@j_camachor/' : '@jcamachor@mastodon.social',
  43. 'https://www.digital-orrery.com/index.xml' : '@neocopinus',
  44. 'https://goingferalco.wordpress.com/feed/' : '@james',
  45. 'https://zfadd.is/feed' : '@zacharius'}
  46. logDir = getAbsolutePath(__file__, 'log/')
  47. logfile = 'out.txt'
  48. def __init__(self):
  49. #super().__init__(
  50. # access_token = self.SECRET_DIR + self.accessTokenFile,
  51. # api_base_url = self.instanceURL)
  52. self.logger = Logger(self.logDir, self.logfile)
  53. self.__log_in_new_client()
  54. def run(self):
  55. self.__logAndPostFeeds()
  56. self.__logAndBoostHashtagSinceLastTime('heyfeedfox')
  57. def __log_in_new_client(self):
  58. super().__init__(
  59. client_id = self.SECRET_DIR + self.clientIDFile,
  60. api_base_url = self.instanceURL)
  61. self.log_in(
  62. self.email,
  63. self.password,
  64. to_file = self.SECRET_DIR + self.accessTokenFile)
  65. def __logAndPostFeeds(self):
  66. for feed in self.blogs:
  67. parser = Parser([feed])
  68. rssEntries = parser.getNewPosts()
  69. for entry in rssEntries:
  70. self.__postRSSEntry(entry, self.blogs[feed])
  71. self.logger.logBlog(entry.title, self.blogs[feed], entry.link)
  72. def __postRSSEntry(self, entry, author):
  73. status = formatStatus(entry,'\n'+ author)
  74. self.status_post(
  75. status,
  76. spoiler_text=entry.title,
  77. visibility="public")
  78. def __logAndBoostHashtagSinceLastTime(self, hashtag):
  79. latestID = self.__findLastRecordedHashtagID(hashtag)
  80. tagged_posts = self.timeline_hashtag(hashtag,
  81. local='true',
  82. since_id = latestID)
  83. for post in tagged_posts:
  84. if post.in_reply_to_id:
  85. post = self.__getParentPost(post)
  86. self.status_reblog(post.id)
  87. self.logger.logToot(post.content, post.account.acct)
  88. if len(tagged_posts) > 0:
  89. lastID = tagged_posts[0].id
  90. self.__saveLastRecordedHashtagID(hashtag, lastID)
  91. def __getParentPost(self, post):
  92. parentID = post.in_reply_to_id
  93. parentPost = self.status(parentID)
  94. return parentPost
  95. def __findLastRecordedHashtagID(self, hashtag):
  96. hashfile = getAbsolutePath(__file__, hashtag)
  97. try:
  98. with open(hashfile) as file:
  99. id = file.read()
  100. except:
  101. id = None
  102. return id
  103. def __saveLastRecordedHashtagID(self, hashtag, id):
  104. hashfile = getAbsolutePath(__file__, hashtag)
  105. with open(hashfile, "w") as file:
  106. file.write(str(id))
  107. if __name__ == '__main__':
  108. rbu_rss = RBU_RSS()
  109. rbu_rss.run()