-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFDataBase.py
More file actions
69 lines (56 loc) · 2.33 KB
/
FDataBase.py
File metadata and controls
69 lines (56 loc) · 2.33 KB
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
import math
import re
import sqlite3
import time
from flask import flash, url_for
class FDataBase:
def __init__(self, db):
self.__db = db
self.__cur = db.cursor()
def getMenu(self):
sql_com = 'SELECT * from mainmenu'
try:
self.__cur.execute(sql_com)
res = self.__cur.fetchall()
if res: return res
except:
print("DB ERROR!")
return []
def addPost(self, title, post, url):
try:
#проверка, была ли статья ранее загружена (уже есть такоц url)
self.__cur.execute(f"SELECT COUNT() FROM posts WHERE url LIKE '{url}' ")
res = self.__cur.fetchone()
if res[0] > 0:
print(f"Такая запись есть в базе")
return False
tm = math.floor(time.time())
self.__cur.execute("INSERT INTO posts VALUES(NULL, ?, ?, ?, ?) ", (title, post, url, tm))
self.__db.commit()
except sqlite3.Error as e:
flash("Ошибка добавления поста в БД - " + str(e))
return False
return True
def getPost(self, alias):
try:
self.__cur.execute(f"SELECT title, text FROM posts WHERE url LIKE '{alias}' LIMIT 1")
res = self.__cur.fetchone()
if res:
# путь, где находятся картинки
base = url_for("static", filename = "Images_html")
print(f"base -> {base}")
# поиск всех тегов img и замена в них путей к файлам картинок
text = re.sub(r"(?P<tag><img\s+[^>]*src=)(?P<quote>[\"'])(?P<url>.+?)(?P=quote)\s*?>",
"\\g<tag>"+base+"/\g<url>", res['text'])
return res['title'], text
except sqlite3.Error as e:
print("Ошибка поиска поста - " + str(e))
return False, False
def getPostTitle(self):
try:
self.__cur.execute("SELECT id, title, url, text FROM posts ORDER BY time DESC ")
res = self.__cur.fetchall()
if res: return res
except sqlite3.Error as e:
print("Ошибка БД - " + str(e))
return []