-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcron-parse-tables.py
73 lines (60 loc) · 2.26 KB
/
cron-parse-tables.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
import pyodbc
import configparser
from TableParser import TableParser
import datetime
import sys
import getopt
def main(argv):
config = configparser.ConfigParser()
config.read('config.ini')
loadDB = True
try:
opts, args = getopt.getopt(argv, 'o:d', ['offset=', 'dryrun'])
except getopt.GetoptError:
print('Usage: python cron-parse-tables.py -o <offset_in_days>')
print(' The offset is the day relative to today that wants to be')
print(' parsed. For instance, -1 is yesterday and 1 is tomorrow.')
print('')
print('You can also use the option -d to perform a dry run (doesn\'t')
print('save anything to the database)')
sys.exit(2)
offset = 1
for opt, arg in opts:
if opt in ('-o', '--offset'):
offset = arg
elif opt in ('-d', '--dryrun'):
loadDB = False
else:
print('Parameter \'' + opt + '\' not recognized.')
print('')
print('Run python cron-parse-tables.py to get help on how to use')
print('this script.')
db = None
if loadDB:
db_host = config['db']['host']
db_database = config['db']['database']
db_user = config['db']['user']
db_password = config['db']['password']
connection_string = (
'DRIVER=MySQL ODBC 8.0 ANSI Driver;'
'SERVER=' + db_host + ';'
'DATABASE=' + db_database + ';'
'UID=' + db_user + ';'
'PWD=' + db_password + ';'
'charset=utf8mb4;'
)
db = pyodbc.connect(connection_string)
db.setdecoding(pyodbc.SQL_WCHAR, encoding='utf-8')
db.setencoding(encoding='utf-8')
offset = int(offset)
print('Parsejant les classes ' +
(('de fa ' if offset < 0 else 'de dins de ') +
str(abs(offset)) + (' dia' if abs(offset) == 1 else ' dies') if offset != 0 else 'd\'avui'))
if not loadDB:
print('NOTA: Dry run')
tomorrow = datetime.date.today() + datetime.timedelta(days=offset)
parser = TableParser('https://fme-intranet.upc.edu/appsext/mrbs/web/day.php')
for area in [2, 6]:
parser.parse(tomorrow.year, tomorrow.month, tomorrow.day, area, db)
if __name__ == "__main__":
main(sys.argv[1:])