forked from LeeMendelowitz/DCMetroMetrics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hotCarApp.py
80 lines (63 loc) · 2.33 KB
/
hotCarApp.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
"""
apps.hotCarApp
Define the HotCarApp as a restartingGreenlet.
This app will use the Twitter API to search for tweets about #wmata
#hotcar's. These tweets and the hotcar data are stored in a database.
Tweet acknowledgements are posted to the @MetroHotCars twitter account.
"""
# TEST CODE
if __name__ == "__main__":
import test.setup
import gevent
import os
import sys
from datetime import datetime
from dcmetrometrics.common.restartingGreenlet import RestartingGreenlet
from dcmetrometrics.common import dbGlobals
from dcmetrometrics.hotcars import hotCars
OUTPUT_DIR = os.environ['OPENSHIFT_DATA_DIR']
REPO_DIR = os.environ['OPENSHIFT_REPO_DIR']
SCRIPT_DIR = os.path.join(REPO_DIR, 'scripts')
class HotCarApp(RestartingGreenlet):
def __init__(self, LIVE=False):
RestartingGreenlet.__init__(self, LIVE=LIVE)
self.SLEEP = 40 # Run every 10 seconds
self.LIVE = LIVE
self.dbg = dbGlobals.DBGlobals()
self.db = self.dbg.getDB()
# Run forever
def _run(self):
while True:
try:
self.tick()
except Exception as e:
import traceback
logFileName = os.path.join(OUTPUT_DIR, 'runHotCarApp.log')
logFile = open(logFileName, 'a')
logFile.write('HotCarApp: Caught Exception! %s\n'%str(e))
tb = traceback.format_exc()
logFile.write('Traceback:\n%s\n\n'%tb)
logFile.close()
gevent.sleep(self.SLEEP)
def tick(self):
logFileName = os.path.join(OUTPUT_DIR, 'runHotCarApp.log')
with open(logFileName, 'a') as logFile:
n = datetime.now()
timeStr = n.strftime('%d-%B-%Y %H:%M:%S')
msg = '*'*50 + '\n'
msg += '%s Running Hot Car App\n'%timeStr
logFile.write(msg)
logFile.flush()
# Run the tick
self.runOnce(tweetLive=self.LIVE, log=logFile)
logFile.close()
def runOnce(self, tweetLive=False, log=sys.stdout):
# Establish connection with the database
# Run one cycle of the hotcar app
hotCars.tick(self.db, tweetLive=tweetLive, log=log)
if __name__ == '__main__':
from time import sleep
app = HotCarApp(LIVE=False)
while True:
app.runOnce(tweetLive=False, log=sys.stdout)
sleep(40)