Merge pull request #15 from delefme/time-selection

Merge time selection feature into the master branch
diff --git a/.gitmodules b/.gitmodules
index 91a3700..e69de29 100644
--- a/.gitmodules
+++ b/.gitmodules
@@ -1,3 +0,0 @@
-[submodule "backend"]
-	path = backend
-	url = https://github.com/delefme/covid-tracability-backend
diff --git a/TableParser.py b/TableParser.py
index 537f393..faeefc3 100644
--- a/TableParser.py
+++ b/TableParser.py
@@ -25,29 +25,38 @@
         table = soup.find(id="day_main")
 
         hores = []
+        implicitClasses = []
+
         for h in range(8,22):
             newhour = ""
             if h < 10:
                 newhour += "0"
-            newhour += str(h);
+            newhour += str(h)
 
-            hores.append(newhour + ":00");
-            hores.append(newhour + ":30");
+            hores.append(newhour + ":00")
+            implicitClasses.append([])
+            hores.append(newhour + ":30")
+            implicitClasses.append([])
 
         p = re.compile(r"Aula (\S+) ?\(\d*\)", re.IGNORECASE)
 
+        horaActual = 0
         for hora in hores:
             td_hora = table.find(text=hora).findNext('td')
             column = 1
 
             while hora not in td_hora.get_text():
+                while column in implicitClasses[horaActual]:
+                    column += 1
 
                 classes = td_hora['class'];
                 if td_hora.has_attr('class') and not td_hora['class'][0] in self.EMPTY_CELL_CLASSES:
                     assignatura = td_hora.get_text().strip()
+                    degree = td_hora.get("class")[0]
                     aulaRaw = table.find_all("th")[column].get_text().strip()
                     aula = p.match(aulaRaw).group(1)
-                    durada = int(td_hora.get("rowspan"))*30
+                    files = int(td_hora.get("rowspan"))
+                    durada = files*30
 
                     timeSplit = hora.split(':')
 
@@ -60,22 +69,29 @@
                     print(("Afegint " if db != None else "") + assignatura
                             + ", " + hora
                             + ", " + str(durada) + "mins"
-                            + ", " + aula)
+                            + ", " + aula
+                            + ", " + degree)
 
                     if db != None:
                         cursor1 = db.cursor()
-                        cursor1.execute("SELECT id FROM classes WHERE calendar_name = ? AND room = ? AND begins = ? AND ends = ?",
-                                assignatura, aula, begins, ends)
+                        cursor1.execute("SELECT id FROM classes WHERE calendar_name = ? AND room = ? AND begins = ? AND ends = ? AND degree = ?",
+                                assignatura, aula, begins, ends, degree)
                         row = cursor1.fetchone()
                         if row:
                             print("[WARNING] Ja estava a la DB (id " + str(row.id) + ")")
                         else:
                             cursor2 = db.cursor()
-                            cursor2.execute("INSERT INTO classes (calendar_name, room, begins, ends) VALUES (?, ?, ?, ?)",
-                                    assignatura, aula, begins, ends)
+                            cursor2.execute("INSERT INTO classes (calendar_name, room, begins, ends, degree) VALUES (?, ?, ?, ?, ?)",
+                                    assignatura, aula, begins, ends, degree)
+
+                    for i in range(1, files - 1):
+                        if i < len(implicitClasses):
+                            implicitClasses[horaActual + i].append(column)
 
                 td_hora = td_hora.findNext('td')
                 column = column + 1
 
+            horaActual = horaActual + 1
+
         if db != None:
             db.commit()
diff --git a/backend b/backend
deleted file mode 160000
index c16e8d1..0000000
--- a/backend
+++ /dev/null
@@ -1 +0,0 @@
-Subproject commit c16e8d15baa438954f63c81372037e5e89af88ea
diff --git a/cron-parse-tables.py b/cron-parse-tables.py
index 7ed1dee..f98816a 100644
--- a/cron-parse-tables.py
+++ b/cron-parse-tables.py
@@ -9,47 +9,59 @@
     config = configparser.ConfigParser()
     config.read('config.ini')
 
-    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')
+    loadDB = True
 
     try:
-        opts, args = getopt.getopt(argv, 'o:', ['offset='])
+        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)