avm99963 | 411e36a | 2020-09-27 23:32:48 +0200 | [diff] [blame] | 1 | import pyodbc |
| 2 | import configparser |
| 3 | from TableParser import TableParser |
| 4 | import datetime |
avm99963 | 8f37631 | 2020-09-28 19:23:52 +0200 | [diff] [blame] | 5 | import sys |
| 6 | import getopt |
avm99963 | 411e36a | 2020-09-27 23:32:48 +0200 | [diff] [blame] | 7 | |
avm99963 | 8f37631 | 2020-09-28 19:23:52 +0200 | [diff] [blame] | 8 | def main(argv): |
| 9 | config = configparser.ConfigParser() |
| 10 | config.read('config.ini') |
avm99963 | 411e36a | 2020-09-27 23:32:48 +0200 | [diff] [blame] | 11 | |
avm99963 | 8f37631 | 2020-09-28 19:23:52 +0200 | [diff] [blame] | 12 | db_host = config['db']['host'] |
| 13 | db_database = config['db']['database'] |
| 14 | db_user = config['db']['user'] |
| 15 | db_password = config['db']['password'] |
avm99963 | 411e36a | 2020-09-27 23:32:48 +0200 | [diff] [blame] | 16 | |
avm99963 | 8f37631 | 2020-09-28 19:23:52 +0200 | [diff] [blame] | 17 | 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 | ) |
avm99963 | 411e36a | 2020-09-27 23:32:48 +0200 | [diff] [blame] | 25 | |
avm99963 | 8f37631 | 2020-09-28 19:23:52 +0200 | [diff] [blame] | 26 | db = pyodbc.connect(connection_string) |
| 27 | db.setdecoding(pyodbc.SQL_WCHAR, encoding='utf-8') |
| 28 | db.setencoding(encoding='utf-8') |
avm99963 | 411e36a | 2020-09-27 23:32:48 +0200 | [diff] [blame] | 29 | |
avm99963 | 8f37631 | 2020-09-28 19:23:52 +0200 | [diff] [blame] | 30 | 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) |
avm99963 | 411e36a | 2020-09-27 23:32:48 +0200 | [diff] [blame] | 37 | |
avm99963 | 8f37631 | 2020-09-28 19:23:52 +0200 | [diff] [blame] | 38 | 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 | |
| 60 | if __name__ == "__main__": |
| 61 | main(sys.argv[1:]) |