-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathwordcounterbot.py
56 lines (45 loc) · 1.45 KB
/
wordcounterbot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import config
import re
import actions
from threading import Thread
from concurrent.futures import ThreadPoolExecutor
import threading
from datetime import datetime
from datetime import datetime
import utils
import logging
import logging.config
import time
from praw.models import Comment
utils.setup_proxy("wordscounterbot")
def checkUnreadMessages(workers=10):
lastSeenKey = "messages"
lastSeen = utils.get_last_seen(lastSeenKey)
lastMessageAt = utils.get_last_seen(lastSeenKey, True);
inbox = config.reddit.inbox
pool = ThreadPoolExecutor(max_workers=workers)
messages = list(inbox.unread(limit=None))
for item in messages:
isComment = isinstance(item, Comment)
createdAt = item.created_utc
if lastSeen >= utils.datetime_from_timestamp(createdAt):
break;
if createdAt > lastMessageAt:
logging.info(f"Found new last message: {utils.datetime_from_timestamp(createdAt)}")
lastMessageAt = createdAt;
if item.new:
logging.info(f"Sending unread item to be processed: itemCreatedAt={createdAt}")
pool.submit(actions.processUnreadItem, (item))
if messages:
utils.set_last_seen(lastSeenKey, lastMessageAt)
inbox.mark_read(messages)
return pool
# while True:
# logging.info(f"Checking for unread messages forever forever forever")
# try:
# checkUnreadMessages()
# time.sleep(5)
# except Exception as e:
# logging.info(f"Caught exception while checking unread messages: {e}")
executor = checkUnreadMessages()
executor.shutdown(wait=True)