fixed watcher

This commit is contained in:
iou1name 2019-01-17 11:18:15 -05:00
parent 036200f1f7
commit 2b56976c03

View File

@ -25,13 +25,14 @@ def setup(bot):
"api_url TEXT PRIMARY KEY," "api_url TEXT PRIMARY KEY,"
"name TEXT DEFAULT 'Anonymous'," "name TEXT DEFAULT 'Anonymous',"
"last_post INTEGER," "last_post INTEGER,"
"time_since TEXT)") "time_since TEXT,"
"channel TEXT)")
con.commit() con.commit()
else: else:
for thread in watching: for thread in watching:
if get_thread_url(thread[0]) in bot.memory["watcher"].keys(): if get_thread_url(thread[0]) in bot.memory["watcher"].keys():
continue continue
t = WatcherThread(bot, thread[0], thread[1], thread[2], thread[3]) t = WatcherThread(bot, *thread)
t.start() t.start()
bot.memory["watcher"][get_thread_url(thread[0])] = t bot.memory["watcher"][get_thread_url(thread[0])] = t
con.close() con.close()
@ -79,7 +80,7 @@ def get_thread_url(api_url):
@commands("watch") @commands("watch")
@example(".watch https://boards.4chan.org/qst/thread/2") @example(".watch https://boards.4chan.org/qst/thread/2 OPName")
def watch(bot, trigger): def watch(bot, trigger):
""" """
A thread watcher for 4chan. A thread watcher for 4chan.
@ -101,12 +102,14 @@ def watch(bot, trigger):
last_post = get_last_post(thread, name) last_post = get_last_post(thread, name)
time_since = get_time() time_since = get_time()
t = WatcherThread(bot, api_url, name, last_post, time_since) t = WatcherThread(bot, api_url, name,last_post, time_since,trigger.channel)
t.start() t.start()
bot.memory["watcher"][url] = t bot.memory["watcher"][url] = t
bot.db.execute("INSERT INTO watcher(api_url, name, last_post, time_since)" bot.db.execute(
" VALUES(?,?,?,?)", (api_url, name, last_post, time_since)) "INSERT INTO watcher(api_url, name, last_post, time_since, channel) "
"VALUES(?,?,?,?,?)",
(api_url, name, last_post, time_since, trigger.channel))
bot.msg("[\x0304Watcher\x03] Watching thread: \x0307" + url) bot.msg("[\x0304Watcher\x03] Watching thread: \x0307" + url)
@ -136,12 +139,13 @@ def removeThread(bot, url):
class WatcherThread(threading.Thread): class WatcherThread(threading.Thread):
def __init__(self, bot, api_url, name, last_post, time_since): def __init__(self, bot, api_url, name, last_post, time_since, channel):
threading.Thread.__init__(self) threading.Thread.__init__(self)
self.stop = threading.Event() self.stop = threading.Event()
self.period = 20 self.period = 20
self._bot = bot self.bot = bot
self.channel = channel
self.api_url = api_url self.api_url = api_url
self.name = name self.name = name
self.last_post = last_post self.last_post = last_post
@ -161,10 +165,10 @@ class WatcherThread(threading.Thread):
continue continue
if res.status_code == 404: if res.status_code == 404:
msg = "[\x0304Watcher\x03] Thread deleted: " \ msg = "[\x0304Watcher\x03] Thread deleted: "
+ f"\x0307{get_thread_url(self.api_url)}" msg += f"\x0307{get_thread_url(self.api_url)}"
self._bot.msg(msg) self.bot.msg(self.channel, msg)
removeThread(self.bot, api_url) removeThread(self.bot, self.api_url)
self.stop.set() self.stop.set()
continue continue
@ -173,18 +177,18 @@ class WatcherThread(threading.Thread):
thread = res.json() thread = res.json()
if thread["posts"][0].get("closed"): if thread["posts"][0].get("closed"):
msg = "[\x0304Watcher\x03] Thread closed: " \ msg = "[\x0304Watcher\x03] Thread closed: "
+ f"\x0307{get_thread_url(self.api_url)}" msg += f"\x0307{get_thread_url(self.api_url)}"
self._bot.msg(msg) self.bot.msg(self.channel, msg)
removeThread(self.bot, api_url) removeThread(self.bot, self.api_url)
self.stop.set() self.stop.set()
continue continue
new_last_post = get_last_post(thread, self.name) new_last_post = get_last_post(thread, self.name)
if new_last_post > self.last_post: if new_last_post > self.last_post:
self.last_post = new_last_post self.last_post = new_last_post
msg = "[\x0304Watcher\x03] New post from \x0308" \ print(self.last_post)
+ f"{self.name}\x03: \x0307{get_thread_url(self.api_url)}" msg = "[\x0304Watcher\x03] New post from \x0308"
+ f"#{self.last_post}" msg += f"{self.name}\x03: \x0307{get_thread_url(self.api_url)}"
self._bot.msg(msg) msg += f"#{self.last_post}"
self.bot.msg(self.channel, msg)