summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--hnnews.py4
-rw-r--r--main.py63
2 files changed, 40 insertions, 27 deletions
diff --git a/hnnews.py b/hnnews.py
index d186b84..68e333a 100644
--- a/hnnews.py
+++ b/hnnews.py
@@ -22,7 +22,7 @@ def get_topnews(limit):
if story["type"] != "story":
continue
if "url" not in story.keys():
- print("using alt url")
+ # print("using alt url")
story["url"] = f'https://news.ycombinator.com/item?id={story_id}'
result.append(story)
except Exception as e:
@@ -37,7 +37,7 @@ def get_one_item(id):
if story["type"] != "story":
return None
if "url" not in story.keys():
- print("using alt url")
+ # print("using alt url")
story["url"] = f'https://news.ycombinator.com/item?id={story_id}'
return story
except Exception as e:
diff --git a/main.py b/main.py
index a589057..7124564 100644
--- a/main.py
+++ b/main.py
@@ -14,7 +14,6 @@ def login_refresh_token():
password=config.PW, to_file=config.TOKEN)
return session
-
def restore_session():
return Mastodon(client_id=config.CLIENTID, access_token=config.TOKEN, feature_set='pleroma')
@@ -30,7 +29,6 @@ class VnilCat:
except:
try:
self.session = login_refresh_token()
-
except:
exit()
# try to fetch the lastest status from the timeline
@@ -41,10 +39,12 @@ class VnilCat:
try:
tl = self.session.timeline_home(limit=1)
if (len(tl)) != 0:
- print("set init status id to begin with...", tl[0]["id"])
+ print("[booting] set init status id to...", tl[0]["id"])
self.tl_lastseen_sid = tl[0]["id"]
except Exception as e:
- print("can't init timeline, continue anyways", e)
+ print("[booting] can't init timeline, continue anyways", e)
+ print(f"Cat booted, all system green, my name is {self.config.UNAME}, prepare to die, human")
+ print("-------------")
def not_mine(self, status):
return status['account']['acct'] != self.config.UNAME
@@ -58,6 +58,7 @@ class VnilCat:
to_status=ori_status, status=random.choice(cat_sounds))
def post_hn_news(self, amount=3):
+ print("posting news")
if amount <= 0:
return
if amount > len(self.news):
@@ -74,8 +75,19 @@ class VnilCat:
self.session.toot(status)
+ def catch_birds(self, status, content):
+ if re_contains_bird.search(content) is not None and len(content) < 10:
+ print("i see a bird", status["id"], " from ", status["account"]["acct"])
+ try:
+ s = self.session.status_reply(
+ to_status=status, status=random.choice(bird_sounds))
+ return True
+ except:
+ print("fail to post")
+ return False
+
def handle_command(self, status, content):
- print("we have a command", content)
+ print("i see a command", content)
cmd = content[1:].strip().split()
actor = status["account"]["acct"]
# handle cmds
@@ -83,23 +95,22 @@ class VnilCat:
if actor not in config.ADMINS:
return
self.post_hn_news()
+ else:
+ print("I don't understand")
def handle_home_status(self, status):
self.tl_lastseen_sid = status["id"]
if self.is_mine(status):
+ print("this one is from myself, skipping")
return
acc = status["account"]
content = cleanhtml(status["content"])
- if re_contains_bird.search(content) is not None and len(content) < 10:
- print("i see a bird", status["id"])
- try:
- s = self.session.status_reply(
- to_status=status, status=random.choice(bird_sounds))
- except:
- print("fail to post")
+ # only one action is taken. if one succeed then return
+ if self.catch_birds(status,content):
+ return
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
@@ -149,6 +160,19 @@ class VnilCat:
if n['type'] == "follow":
self.handle_follow(n)
+ def run(self):
+ self.epoch = 3
+ while True:
+ try:
+ self.handle_notification()
+ self.scantimeline()
+ if self.epoch % 360 == 1 :
+ self.post_hn_news(3)
+ except Exception as e:
+ print("something wrong...")
+ print(e)
+ time.sleep(self.config.POLL_INTERVAL)
+ self.epoch += 1
# TODO re-organize auth mgmt. But it involves a lot of mamual testing so I'll
# leave it to the future...
@@ -160,18 +184,7 @@ def init_bot():
# 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
+ cat.run()
if __name__ == "__main__":