blob: f98816ad3a074ab3c8ea0dcc5f0559a945b5c155 [file] [log] [blame]
avm99963411e36a2020-09-27 23:32:48 +02001import pyodbc
2import configparser
3from TableParser import TableParser
4import datetime
avm999638f376312020-09-28 19:23:52 +02005import sys
6import getopt
avm99963411e36a2020-09-27 23:32:48 +02007
avm999638f376312020-09-28 19:23:52 +02008def main(argv):
9 config = configparser.ConfigParser()
10 config.read('config.ini')
avm99963411e36a2020-09-27 23:32:48 +020011
avm99963677dfd82020-10-06 11:27:19 +020012 loadDB = True
avm99963411e36a2020-09-27 23:32:48 +020013
avm999638f376312020-09-28 19:23:52 +020014 try:
avm99963677dfd82020-10-06 11:27:19 +020015 opts, args = getopt.getopt(argv, 'o:d', ['offset=', 'dryrun'])
avm999638f376312020-09-28 19:23:52 +020016 except getopt.GetoptError:
17 print('Usage: python cron-parse-tables.py -o <offset_in_days>')
18 print(' The offset is the day relative to today that wants to be')
19 print(' parsed. For instance, -1 is yesterday and 1 is tomorrow.')
avm99963677dfd82020-10-06 11:27:19 +020020 print('')
21 print('You can also use the option -d to perform a dry run (doesn\'t')
22 print('save anything to the database)')
avm999638f376312020-09-28 19:23:52 +020023 sys.exit(2)
avm99963411e36a2020-09-27 23:32:48 +020024
avm999638f376312020-09-28 19:23:52 +020025 offset = 1
26 for opt, arg in opts:
27 if opt in ('-o', '--offset'):
28 offset = arg
avm99963677dfd82020-10-06 11:27:19 +020029 elif opt in ('-d', '--dryrun'):
30 loadDB = False
avm999638f376312020-09-28 19:23:52 +020031 else:
32 print('Parameter \'' + opt + '\' not recognized.')
33 print('')
34 print('Run python cron-parse-tables.py to get help on how to use')
35 print('this script.')
36
avm99963677dfd82020-10-06 11:27:19 +020037 db = None
38
39 if loadDB:
40 db_host = config['db']['host']
41 db_database = config['db']['database']
42 db_user = config['db']['user']
43 db_password = config['db']['password']
44
45 connection_string = (
46 'DRIVER=MySQL ODBC 8.0 ANSI Driver;'
47 'SERVER=' + db_host + ';'
48 'DATABASE=' + db_database + ';'
49 'UID=' + db_user + ';'
50 'PWD=' + db_password + ';'
51 'charset=utf8mb4;'
52 )
53
54 db = pyodbc.connect(connection_string)
55 db.setdecoding(pyodbc.SQL_WCHAR, encoding='utf-8')
56 db.setencoding(encoding='utf-8')
57
avm999638f376312020-09-28 19:23:52 +020058 offset = int(offset)
59
60 print('Parsejant les classes ' +
61 (('de fa ' if offset < 0 else 'de dins de ') +
62 str(abs(offset)) + (' dia' if abs(offset) == 1 else ' dies') if offset != 0 else 'd\'avui'))
avm99963677dfd82020-10-06 11:27:19 +020063 if not loadDB:
64 print('NOTA: Dry run')
avm999638f376312020-09-28 19:23:52 +020065
66 tomorrow = datetime.date.today() + datetime.timedelta(days=offset)
67
68 parser = TableParser('https://fme-intranet.upc.edu/appsext/mrbs/web/day.php')
69 for area in [2, 6]:
70 parser.parse(tomorrow.year, tomorrow.month, tomorrow.day, area, db)
71
72if __name__ == "__main__":
73 main(sys.argv[1:])