forked from LeeMendelowitz/DCMetroMetrics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hotCarApp.py
78 lines (53 loc) · 1.89 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
"""
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
from dcmetrometrics.common.globals import DATA_DIR, REPO_DIR, DATA_DIR
OUTPUT_DIR = DATA_DIR
###############################################################
# Log the HotCarApp App to a file.
import logging
LOG_FILE_NAME = os.path.join(DATA_DIR, 'HotCarApp.log')
fh = logging.FileHandler(LOG_FILE_NAME)
sh = logging.StreamHandler(sys.stderr)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
fh.setFormatter(formatter)
sh.setFormatter(formatter)
logger = logging.getLogger('HotCarApp')
logger.addHandler(fh)
logger.addHandler(sh)
#################################################################
class HotCarApp(RestartingGreenlet):
def __init__(self, LIVE=False):
dbGlobals.connect()
RestartingGreenlet.__init__(self, LIVE=LIVE)
self.SLEEP = 40 # Run every 10 seconds
self.LIVE = LIVE
# Run forever
def _run(self):
while True:
try:
hotCars.tick(tweetLive = self.LIVE)
except Exception as e:
import traceback
tb = traceback.format_exc()
logger.error("HotCarApp Caught Error! %s\nTraceback:\n%s"%(str(e), tb))
gevent.sleep(self.SLEEP)
if __name__ == '__main__':
from time import sleep
app = HotCarApp(LIVE = False)
app.start()
app.join()