-
Notifications
You must be signed in to change notification settings - Fork 2
/
lib_bd.py
executable file
·81 lines (63 loc) · 2.59 KB
/
lib_bd.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# -*- coding: utf-8 -*-
# library to manage SQLITE3 database
# Nicolas Lorenzon - nicolas@lorenzon.ovh - http://www.lorenzon.ovh
import sqlite3
class storage(object):
def __init__(self):
self.connexion = None
self.connexionBase()
# connect to sqlite3 database
def connexionBase(self):
self.connexion = sqlite3.connect(":memory:", timeout=180)
self.connexion.execute("CREATE TABLE 'urls' ('id' INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL , 'url' TEXT UNIQUE , 'crawled' INTEGER DEFAULT 0);")
# format text before insert in database
def prepareExpression(self, expression):
return expression.replace("\"", "\"\"").strip()
# get all records from database
def lectureTous(self):
#conn = connexionBase(basedonnees)
requete = "SELECT * FROM urls;"
cursor = self.connexion.execute(requete)
tableau = list()
for ligne in cursor:
tableau.append(ligne)
return tableau
# get all records not crawled
def lectureToCrawl(self):
requete = "SELECT * FROM urls WHERE crawled = 0;"
cursor = self.connexion.execute(requete)
tableau = list()
for ligne in cursor:
tableau.append(ligne)
return tableau
# ajout d'une url dans la basedonnees
def addUrl(self,url) :
try:
requete = "INSERT INTO urls (url) VALUES (\"" + str(url) + "\");"
self.connexion.execute(requete)
return 1
except:
#print("on a pas réussi l'ajout : " + str(url))
return 0
# un seul enregistrement
def lectureUneUrl(self):
# en cas d'erreur de lock de la base
requete = "SELECT * FROM urls WHERE crawled = 0 LIMIT 1;"
cursor = self.connexion.execute(requete)
tableau = cursor.fetchone()
if tableau is not None :
url = tableau[1]
# mise à jour
self.connexion.execute( "UPDATE urls SET crawled = 1 WHERE url = \"" + self.prepareExpression(url) + "\" ;")
else :
url = None
return url
# mise à jour pour dire qu'on a crawlé l'url
def majUrl(self,url) :
#conn = connexionBase(basedonnees)
self.connexion.execute( " UPDATE urls SET crawled = 1 WHERE url = \"" + self.prepareExpression(url) + "\" ;")
def supprimeTout(self) :
#conn = connexionBase(basedonnees)
self.connexion.execute("DELETE FROM urls ;")
self.connexion.execute("VACUUM")
self.connexion.execute("UPDATE SQLITE_SEQUENCE SET SEQ=0 WHERE NAME='urls';")