-
Notifications
You must be signed in to change notification settings - Fork 6
/
models.py
62 lines (48 loc) · 1.96 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
61
62
#!/usr/bin/env python
# -*- coding: utf-8 -*-
__author__ = 'Tao Liu'
import os
from datetime import datetime
# 外键关联
# 创建实例,并连接数据库
from dotenv import find_dotenv, load_dotenv
# 导入要使用的字段类型
from sqlalchemy import Column, Integer, String, DateTime, Text
from sqlalchemy import create_engine, UniqueConstraint
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
load_dotenv(find_dotenv())
envget = os.environ.get
dbuser = envget('PSQL_USER') # "dbuser"
dbpassword = envget('PSQL_PASSWORD') # ,"123456"
dbhost = envget('PSQL_HOST') # ,"192.168.1.156"
dbport = envget('PSQL_PORT') # ,"5433"
dbname = envget('PSQL_DATABASE')
# echo=True 显示信息
print('mysql+pymysql://%s:%s@%s:%s/%s' % (dbuser, dbpassword, dbhost, dbport, dbname))
engine = create_engine('mysql+pymysql://%s:%s@%s:%s/%s' % (dbuser, dbpassword, dbhost, dbport, dbname),
echo=False)
Session = sessionmaker(bind=engine)
session = Session()
Base = declarative_base() # 生成orm基类
class PageInfo(Base):
__tablename__ = 'webpageinfo'
id = Column(Integer, primary_key=True, autoincrement=True)
baseurl = Column(String(128)) # 基础url
currenturl = Column(String(512), unique=True) # 当前的url
prevfetchtime = Column(DateTime) # 上次下载时间
fetchtime = Column(DateTime, default=datetime.now, onupdate=datetime.now) # 下载时间
content = Column(Text) # 内容
contentmd5 = Column(String(64)) # 内容的md5值
contenttype = Column(String(32)) # 内容的类型
domain_name = Column(String(32)) # 域名
page_title = Column(String(256)) # 页面的title信息
page_body = Column(Text) # 页面body部分信息
def init_db():
Base.metadata.create_all(engine) # 创建表结构 (这里是父类调子类)
def drop_db():
Base.metadata.drop_all(engine)
# 初始化数据库
if __name__ == '__main__':
# drop_db()
init_db()