-
Notifications
You must be signed in to change notification settings - Fork 2
/
botrunner-heroku.py
87 lines (75 loc) · 2.6 KB
/
botrunner-heroku.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import logging
import time
import sys
import redisconnector
import random
import twitterconnector
import datetime
## log to stdout, get caught by heroku's log service
logging.basicConfig(
stream=sys.stdout,
level=logging.DEBUG,
format='%(asctime)s %(levelname)8s %(name)s - %(message)s',
datefmt='%H:%M:%S'
)
## set up redis
redis = redisconnector.get_redis()
# get the next due timestamp
nextrun_key = 'NextRun'
nextrun = None
lineptr_key = 'LinePtr'
line_ptr = None
try:
nextrun = redis.get( nextrun_key )
line_ptr = redis.get( lineptr_key )
except Exception as e:
logging.info( e )
if line_ptr is None:
line_ptr = 0
else:
line_ptr = int( line_ptr )
line_ptr += 1
POST_CHANCE = 0.8
SLEEP_START_HOUR = 22
SLEEP_END_HOUR = 8
override_nextrun = len(sys.argv) > 1 and sys.argv[1] == "override_nextrun"
now = time.time()
if override_nextrun or (not nextrun) or (now >= long(nextrun)):
logging.info( "Running mundanebond..." )
if random.random() > POST_CHANCE:
# chance not met
logging.info( "POST_CHANCE (%2.1f) not met." % POST_CHANCE )
else:
# read all lines into a list, get the next one
fh = open( 'lines_shuffled.txt' )
lines = fh.read().splitlines()
if line_ptr >= len(lines):
line_ptr = 0
line = lines[ line_ptr ]
# tweet
tc = twitterconnector.TwitterConnector( creds_path="twitter_creds" )
tc.tweet( line )
logging.info( "Tweet: %s" % line )
## set next tweet time
hours = random.randrange( 6, 24 )
delta = datetime.timedelta( hours=hours )
delta -= datetime.timedelta( minutes=2 ) # make sure we will run on the actual hour, set time a couple of minutes before.
dt_now = datetime.datetime.now()
dt_nextrun = dt_now + delta
# move next run times during sleep hours to the next day
nexthour = dt_nextrun.hour
sleep_length = (24 - SLEEP_START_HOUR) + SLEEP_END_HOUR
if (nexthour > SLEEP_START_HOUR) or (nexthour < SLEEP_END_HOUR):
# offset by sleep time, move to next morning
dt_nextrun += datetime.timedelta( hours=sleep_length )
# store next run time as timestamp
nextrun = time.mktime( dt_nextrun.timetuple() )
logging.info( "Next run at %s, will store %d" % (dt_nextrun,nextrun) )
try:
redis.set( nextrun_key, "%d" % nextrun )
redis.set( lineptr_key, "%d" % line_ptr )
except Exception as e:
logging.info( e )
else:
dt_nextrun = datetime.datetime.fromtimestamp( float(nextrun) )
logging.info( "Not due yet, now=%s and nextrun=%s (%s)" % (now,nextrun,dt_nextrun) )