-
Notifications
You must be signed in to change notification settings - Fork 17
/
models.py
60 lines (42 loc) · 1.31 KB
/
models.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
# -*- coding: utf-8 -*-
# @File : models.py
# @Date : 2018-09-13
# @Author : Peng Shiyu
import logging
import os
from datetime import datetime
from spiders.utils import get_md5
from peewee import *
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
db_path = os.path.join(BASE_DIR, "article.db")
db = SqliteDatabase(db_path)
class BaseModel(Model):
class Meta:
database = db
class ArticleModel(BaseModel):
title = CharField()
article_url = CharField()
source = CharField()
source_url = CharField()
summary = CharField()
tag = CharField()
publish_time = DateTimeField()
image = CharField()
create_time = DateTimeField
md5 = CharField(unique=True)
if not ArticleModel.table_exists():
ArticleModel.create_table()
class ArticleModelUtils(object):
@classmethod
def insert(cls, item):
content = "{}{}".format(item.get("title"), item.get("source"))
item["md5"] = get_md5(content)
item["create_time"] = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
is_insert = False
try:
ArticleModel.create(**item)
logging.debug("ArticleModel insert successful")
is_insert = True
except IntegrityError as e:
logging.debug("ArticleModel {}".format(e))
return is_insert