This repository has been archived by the owner on Dec 4, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathheroku_timing.py
49 lines (41 loc) · 1.56 KB
/
heroku_timing.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
__author__ = 'Almenon'
from time import time,sleep,localtime
import bot
from requests.exceptions import ConnectionError
from requests.exceptions import ReadTimeout
import logging
from pymongo import MongoClient
from os import environ
client = MongoClient(environ['mongodb_uri'])
db = client.heroku_m17k26m5
saved_time = db.last_time # type: collection
last_time = saved_time.find_one()['time']
last_day = localtime().tm_mday
bot.login("Python:episodeInfo:v2.0 (by /u/Almenon)")
while True:
try:
# scan posts / mentions / messages / replies
bot.scan(last_time)
# save time
last_time = time()
saved_time.update_one({},{'$set':{'time':last_time}})
# reset post limits each new day
if localtime().tm_mday != last_day:
for key in bot.num_posts: bot.num_posts[key] = 0
last_day = localtime().tm_mday
except (ConnectionError, ReadTimeout) as e:
print("there was an error connecting to reddit. Check if it's down or if there is no internet connection")
logging.exception(e)
last_time = time()
saved_time.update_one({},{'$set':{'time':last_time}})
sleep(1200) # sleep for 20 min
except Exception as e:
logging.exception(e)
last_time = time()
saved_time.update_one({},{'$set':{'time':last_time}})
if isinstance(e, KeyboardInterrupt) or isinstance(e, SystemExit):
raise
sleep(180)
logging.info("sleeping for 3 minutes")
print("sleeping")
sleep(180) # limit is 2 seconds. 30 is polite time.