Commits

Anonymous committed 0b05e6a

Reducción de registros en el log
Cambios menores

Comments (0)

Files changed (3)

         self.parm1 = parm1
         self.parm2 = parm2
 
-    def __call__(self, *msg):
+    def __call__(self, *msg, **kw):
+
+        if "parm1" in kw:
+            self.parm1 = kw["parm1"]
+        if "parm2" in kw:
+            self.parm2 = kw["parm2"]
 
         with DB:
             DB.execute("insert into logs(parm1, parm2, desc) values(?,?,?)",
     total = 0
     aviso = soup.find("div", id="aviso")
     if aviso is not None:
-        logger("AVISO:", aviso.text)
+        logger(aviso.text)
     else:
         info = soup.find("div", class_="stTextos")
         if info:
             m = PAT_RESULTS.search(info.text)
-            total = m.group(1) if m else 0
-            logger("  ", info.text)
+            total = m.group(1) if m else 0  #TODO: nunca encuentra nada
+            if total > 0:
+                logger("Encontrados {} libros".format(total))
+            else:
+                logger(info.text)
     query.set_total(total)
 
     books = soup.find_all("div", class_="isbnResultado")
         query.next_page()
 
         html = get_page(METHOD)  # cambia la URL
-        soup = bs4.BeautifulSoup(html, "html.parser", from_encoding="ISO-8859-1")
+        soup = bs4.BeautifulSoup(html, "html.parser",
+                                 from_encoding="ISO-8859-1")
         info = soup.find("div", class_="stTextos")
         logger("  ", info.text)
 
     sql_insert = INSERTS["libros"]
 
     for editorial, isbns in EDITORIALES.items():
+
+        log("Editorial", editorial, parm1="", parm2="")
+        print "Editorial", editorial
+
         for isbn in isbns:
             for materia, m_desc in MATERIAS.items():
 
+                log.set_parms(isbn, materia)
+
                 query = QA_IBIC_ISBN(materia, isbn, YEAR1, YEAR2,
                                      maxitems=MAXITEMS,
                                      publanguage=PUBLANG)
 
-                log.set_parms(materia, isbn)
-                log("Editorial", editorial)
+                with DB:
+                    data = ([to_unicode(x) for x in (book + [materia, m_desc])]
+                            for book in get_books(query, log))
+                    DB.executemany(sql_insert, data)
 
-                for book in get_books(query, logger=log):
-                    data = book + [materia, m_desc]
-                    data = [to_unicode(x) for x in data]
-                    DB.execute(sql_insert, data)
+#                time.sleep(2)  # Espera de 2 segundos entre peticiones
 
-                DB.commit()
-                time.sleep(2)  # Espera de 2 segundos entre peticiones
+#        time.sleep((1+2*random())*60)  # Espera entre 1 y 3 minutos
 
-        time.sleep((1+2*random())*60)  # Espera entre 1 y 3 minutos
-
-    DB.commit()
 
 
 if __name__ == "__main__":
 
     captura()
 
-    log.set_parms("", "")
-    log("FINAL")
+    log("FINAL", parm1="", parm2="")