diff options
| author | Tianhao Wang <shrik3@riseup.net> | 2023-03-28 18:48:37 +0200 |
|---|---|---|
| committer | Tianhao Wang <shrik3@riseup.net> | 2023-03-28 18:48:37 +0200 |
| commit | 5331214b8654d9b8e2441f9ca568a19b6afa7347 (patch) | |
| tree | d0f11a5e8d1e899278d4c585ca87efec6456bc3c /main.py | |
| parent | b99178fd7b03a7ebd1f65b8b60814a32f13f2333 (diff) | |
post HN news ..
Diffstat (limited to 'main.py')
| -rw-r--r-- | main.py | 39 |
1 files changed, 34 insertions, 5 deletions
@@ -5,6 +5,7 @@ import re import random import os from patterns import * +import hnnews def login_refresh_token(): @@ -15,7 +16,7 @@ def login_refresh_token(): def restore_session(): - return Mastodon(client_id=config.CLIENTID, access_token=config.TOKEN) + return Mastodon(client_id=config.CLIENTID, access_token=config.TOKEN, feature_set='pleroma') class VnilCat: @@ -23,6 +24,7 @@ class VnilCat: self.session = restore_session() self.tl_lastseen_sid = None self.config = config + self.news = [] try: self.session.account_verify_credentials() except: @@ -48,20 +50,43 @@ class VnilCat: return status['account']['acct'] != self.config.UNAME def is_mine(self, status): - return status['account']['acct'] != self.config.UNAME + return status['account']['acct'] == self.config.UNAME def reply_meow(self, ori_status): print("replying meow to ", ori_status["id"]) self.session.status_reply( to_status=ori_status, status=random.choice(cat_sounds)) + def post_hn_news(self, amount=3): + if amount <= 0: + return + if amount > len(self.news): + self.news = hnnews.get_topnews(20) + if amount > len(self.news): + amount = len(self.news) + status = "Hear ye, hear ye \n\n" + for i in range(amount): + n = self.news.pop(random.randrange(len(self.news))) + status += n["title"] + status += "\n" + status += n["url"] + status += "\n\n" + self.session.toot(status) + + def handle_command(self, status, content): print("we have a command", content) cmd = content[1:].strip().split() + actor = status["account"]["acct"] + # handle cmds + if cmd[0] == "news": + if actor not in config.ADMINS: + return + self.post_hn_news() def handle_home_status(self, status): self.tl_lastseen_sid = status["id"] - if not self.is_mine(status): + if self.is_mine(status): return acc = status["account"] content = cleanhtml(status["content"]) @@ -74,7 +99,7 @@ class VnilCat: print("fail to post") def scantimeline(self): - # print("scanning timeline, lastseen=",self.tl_lastseen_sid) + print("scanning timeline, lastseen=",self.tl_lastseen_sid) tl = self.session.timeline_home(since_id=self.tl_lastseen_sid) # important! make sure to iterate from older status to newer # otherwise the 'last_seen' won't be updated correctly @@ -132,17 +157,21 @@ def init_bot(): to_file=config.CLIENTID) session = login_refresh_token() - +# TODO use sched def run(): cat = VnilCat(config) + epoch = 1 while True: try: cat.handle_notification() cat.scantimeline() + if epoch % 360 == 1 : + cat.post_hn_news(3) except Exception as e: print("something wrong...") print(e) time.sleep(config.POLL_INTERVAL) + epoch += 1 if __name__ == "__main__": |
