-
Notifications
You must be signed in to change notification settings - Fork 0
/
batch.py
98 lines (90 loc) · 3.46 KB
/
batch.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
88
89
90
91
92
93
94
95
96
97
98
# -*- coding: utf-8 -*-
"""
Batch script that retrieves and stores data in database.
"""
import logging
from logging.handlers import TimedRotatingFileHandler
import os
import time
import signal
import sys
from sqlalchemy.orm import scoped_session, sessionmaker
from pattern.web import SearchEngineLimitError
import config
from dataMining import tweetSearch
from database import db, db_functions
def main():
"""
Main function in batch script, where eternal while loop is running.
Script uses modules dataMining and database to retrieve and store data.
"""
# Logger
logHandler = TimedRotatingFileHandler(os.path.join(
os.path.dirname(__file__),
"logs", "batch.log"),
when="midnight")
logFormatter = logging.Formatter('%(asctime)s: %(levelname)s; %(message)s')
logHandler.setFormatter(logFormatter)
logger = logging.getLogger('batch logger')
logger.addHandler(logHandler)
logger.setLevel(logging.DEBUG)
# Signals
def signal_handler(sig, frame):
"""
Method that handles received kill signals.
"""
logger.warning('Caught signal: %s, killing process.', sig)
sys.exit(0)
signal.signal(signal.SIGINT, signal_handler)
# Search setup
countries = tweetSearch.getCountries()
words = tweetSearch.getSearchWords()
# Database setup
create_tables = False
if not os.path.isfile(config.DATABASE_LOCATION):
create_tables = True
engine = db.init_db(config.DATABASE_LOCATION)
if create_tables:
db.create_tables(engine)
db_session = scoped_session(sessionmaker(autocommit=False,
autoflush=False,
bind=engine))
logger.info('Started script (PID: %s).' % os.getpid())
def get_and_store_data(search_terms, search_function, save_function):
"""
Method used when getting and storing data. Method is made in a
semi-generic manner, so that it the batch script can evolve into
a dynamic script that can be tailored in the configuration file.
"""
skipped_terms = []
for terms in search_terms:
results = None
try:
results = search_function(terms)
except SearchEngineLimitError:
logger.warning('Search engine limit exceeded,'
' sleeping for %s seconds.' %
config.SLEEP_TIME)
time.sleep(config.SLEEP_TIME)
skipped_terms.append(terms)
if results:
# Save tweets
logger.info('Found %s results when searching for %s.' %
(len(results), ' and '.join(terms)))
save_function(db_session, results, terms)
if skipped_terms:
get_and_store_data(skipped_terms,
search_function,
save_function)
# Starting datamining loop
while 1:
try:
search_terms = [(a, b) for b in words for a in countries]
search_function = tweetSearch.getTweets
save_function = db_functions.saveTweets
get_and_store_data(search_terms, search_function, save_function)
except:
logger.exception('Exception caught!')
raise
if __name__ == '__main__':
main()