-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.py
72 lines (59 loc) · 2.66 KB
/
db.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
# -*- coding:utf-8 -*-
from mylogging import MyLogger
import dbConnect
dbLogFile = 'log/db.log'
dbLogger = MyLogger(dbLogFile)
dbi = dbConnect.DBConnect()
class DB():
def makeNamuwikiTable(self):
makeTableQuery = """
CREATE TABLE IF NOT EXISTS `namuwiki_db`.`namuwiki` (
`id` INT NOT NULL AUTO_INCREMENT,
`title` VARCHAR(1300) CHARACTER SET 'utf8mb4' COLLATE 'utf8mb4_unicode_ci' NULL DEFAULT NULL,
`url` VARCHAR(1300) CHARACTER SET 'utf8mb4' COLLATE 'utf8mb4_unicode_ci' NOT NULL,
`content` LONGTEXT CHARACTER SET 'utf8mb4' COLLATE 'utf8mb4_unicode_ci' NULL DEFAULT NULL,
`image` VARCHAR(1300) CHARACTER SET 'utf8mb4' COLLATE 'utf8mb4_unicode_ci' NULL DEFAULT NULL,
`editdate` VARCHAR(45) CHARACTER SET 'utf8mb4' COLLATE 'utf8mb4_unicode_ci' NULL DEFAULT NULL,
`crawltime` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`html` LONGTEXT CHARACTER SET 'utf8mb4' COLLATE 'utf8mb4_unicode_ci' NULL DEFAULT NULL,
`urlhash` CHAR(32) CHARACTER SET 'utf8mb4' COLLATE 'utf8mb4_unicode_ci' NOT NULL,
PRIMARY KEY (`id`),
UNIQUE INDEX `urlhash_UNIQUE` (`urlhash` ASC))
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8mb4
COLLATE = utf8mb4_unicode_ci;
"""
dbLogger.debug("makeNamuwikiDB")
try:
return dbi.query(makeTableQuery)
except Exception as e:
return dbLogger.error(e)
def insertNamuwikiDB(self, dbTuple):
insertDBQuery = """
INSERT INTO namuwiki (title, url, content, image, editdate, crawltime, html, urlhash)\
VALUES (%s, %s, %s, %s, %s, NOW(), %s, md5(%s))
ON DUPLICATE KEY UPDATE
title=%s, url=%s, content=%s, image=%s, editdate=%s, crawltime=NOW(), html=%s
"""
dbLogger.debug('insertNamuwikiDB')
try:
return dbi.insert(insertDBQuery, dbTuple + dbTuple[:-1])
except Exception as e:
return dbLogger.error(e)
def selectRecentUrl(self):
selectRecentUrl = """
SELECT url FROM namuwiki ORDER BY id DESC
"""
dbLogger.debug("selectRecentUrl")
try:
return dbi.select(selectRecentUrl)
except Exception as e:
return dbLogger.error(e)
def recentCrawlCheck(self, _url):
recentCrawlCheck = """
SELECT id FROM namuwiki WHERE url=%s AND crawltime > NOW() - INTERVAL 1 DAY
"""
try:
return dbi.select(recentCrawlCheck, (_url))
except Exception as e:
return dbLogger.error(e)