blob: 7ed1deee5d9b33bbc53674e6b5548834adcdcdd0 [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
avm999638f376312020-09-28 19:23:52 +020012 db_host = config['db']['host']
13 db_database = config['db']['database']
14 db_user = config['db']['user']
15 db_password = config['db']['password']
avm99963411e36a2020-09-27 23:32:48 +020016
avm999638f376312020-09-28 19:23:52 +020017 connection_string = (
18 'DRIVER=MySQL ODBC 8.0 ANSI Driver;'
19 'SERVER=' + db_host + ';'
20 'DATABASE=' + db_database + ';'
21 'UID=' + db_user + ';'
22 'PWD=' + db_password + ';'
23 'charset=utf8mb4;'
24 )
avm99963411e36a2020-09-27 23:32:48 +020025
avm999638f376312020-09-28 19:23:52 +020026 db = pyodbc.connect(connection_string)
27 db.setdecoding(pyodbc.SQL_WCHAR, encoding='utf-8')
28 db.setencoding(encoding='utf-8')
avm99963411e36a2020-09-27 23:32:48 +020029
avm999638f376312020-09-28 19:23:52 +020030 try:
31 opts, args = getopt.getopt(argv, 'o:', ['offset='])
32 except getopt.GetoptError:
33 print('Usage: python cron-parse-tables.py -o <offset_in_days>')
34 print(' The offset is the day relative to today that wants to be')
35 print(' parsed. For instance, -1 is yesterday and 1 is tomorrow.')
36 sys.exit(2)
avm99963411e36a2020-09-27 23:32:48 +020037
avm999638f376312020-09-28 19:23:52 +020038 offset = 1
39 for opt, arg in opts:
40 if opt in ('-o', '--offset'):
41 offset = arg
42 else:
43 print('Parameter \'' + opt + '\' not recognized.')
44 print('')
45 print('Run python cron-parse-tables.py to get help on how to use')
46 print('this script.')
47
48 offset = int(offset)
49
50 print('Parsejant les classes ' +
51 (('de fa ' if offset < 0 else 'de dins de ') +
52 str(abs(offset)) + (' dia' if abs(offset) == 1 else ' dies') if offset != 0 else 'd\'avui'))
53
54 tomorrow = datetime.date.today() + datetime.timedelta(days=offset)
55
56 parser = TableParser('https://fme-intranet.upc.edu/appsext/mrbs/web/day.php')
57 for area in [2, 6]:
58 parser.parse(tomorrow.year, tomorrow.month, tomorrow.day, area, db)
59
60if __name__ == "__main__":
61 main(sys.argv[1:])