summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--config.py1
-rw-r--r--hnnews.py45
-rw-r--r--main.py39
3 files changed, 80 insertions, 5 deletions
diff --git a/config.py b/config.py
index c1e4e7e..c550dfc 100644
--- a/config.py
+++ b/config.py
@@ -25,3 +25,4 @@ except:
UNAME = input("username or email")
PW = input("password (not concealed)")
+ADMINS = ["shrik3"]
diff --git a/hnnews.py b/hnnews.py
new file mode 100644
index 0000000..d186b84
--- /dev/null
+++ b/hnnews.py
@@ -0,0 +1,45 @@
+import requests
+baseurl = "https://hacker-news.firebaseio.com"
+
+def _get_topnews_list():
+ try:
+ resp = requests.get(f'{baseurl}/v0/topstories.json')
+ return resp.json()
+ except:
+ print("error fetching top news list, skipping")
+ return []
+
+def get_topnews(limit):
+ result = []
+ list = _get_topnews_list()
+ if len(list) < limit:
+ limit = len(list)
+ for story_id in list[:limit]:
+ try:
+ url = f'{baseurl}/v0/item/{story_id}.json'
+ resp = requests.get(url)
+ story = resp.json()
+ if story["type"] != "story":
+ continue
+ if "url" not in story.keys():
+ print("using alt url")
+ story["url"] = f'https://news.ycombinator.com/item?id={story_id}'
+ result.append(story)
+ except Exception as e:
+ print("error fetching story skipping ", e)
+ return result
+
+def get_one_item(id):
+ try:
+ url = f'{baseurl}/v0/item/{story_id}.json'
+ resp = requests.get(url)
+ story = resp.json()
+ if story["type"] != "story":
+ return None
+ if "url" not in story.keys():
+ print("using alt url")
+ story["url"] = f'https://news.ycombinator.com/item?id={story_id}'
+ return story
+ except Exception as e:
+ print("error fetching story skipping ", e)
+ return None
diff --git a/main.py b/main.py
index 73584f5..a589057 100644
--- a/main.py
+++ b/main.py
@@ -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__":