Skip to content
This repository has been archived by the owner on Aug 20, 2023. It is now read-only.

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeffz615 committed Apr 13, 2020
1 parent 5e1a32e commit a7b7da3
Show file tree
Hide file tree
Showing 34 changed files with 2,037 additions and 0 deletions.
4 changes: 4 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
pixivpy
apscheduler
pillow
pycryptodome
8 changes: 8 additions & 0 deletions server.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# -*- coding: utf-8 -*-
from src import pixiv
from src import route
from src import config

if __name__ == "__main__":
pixiv.pixivCron() # 启用定时下载器
route.app.run(config.HOST, config.PORT, debug=config.DEBUG) # 开启网页服务器
3 changes: 3 additions & 0 deletions src/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# -*- coding: utf-8 -*-

__all__ = ['config', 'db', 'define', 'logger', 'pixiv', 'route', 'rsaUnit', 'deleteUnit']
44 changes: 44 additions & 0 deletions src/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# -*- coding: utf-8 -*-

from . import define
from os import path, makedirs
import logging


# 服务器设置
PORT = 5555
HOST = "0.0.0.0"
DEBUG = False

# 日志文件
LOGON = True
LOGFILE = path.join(define.PATHDIR, "PixivFlask.log")
LOGLEVEL = logging.WARNING

# 图片存放位置
# 与static文件夹的相对路径
# 请确保在static文件夹内,否则无法访问
IMGPATH = "img"

# 系统管理员密码
FLASK_PASSWORD = "passwd"

# pixiv账号密码
PIXIV_USERNAME = "username"
PIXIV_PASSWORD = "password"

# 是否启用代理图传站(pixiv.cat)
USECAT = True

# 排行榜每日下载时间设置
# P站排行榜刷新时间为日本的中午12:00,即北京时间上午11:00
APSTIME = {"hour": 12, "minute": 0}

# 选择下载的类型
# MODELIST = ["day", "week", "month", "day_male", "day_female",
# "week_original", "week_rookie", "day_manga", "day_r18", "day_male_r18",
# "day_female_r18", "week_r18", "week_r18g"]
CHOICEMODE = ["day", "day_male"]

# COUNT阈值,合集中超过多少张不下载,0为无限制
MAXCOUNT = 3
174 changes: 174 additions & 0 deletions src/db.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
# -*- coding: utf-8 -*-
import sqlite3
import time
from . import define
from .logger import logger
import os
import logging
import json


DBFILE = os.path.join(define.PATHDIR, 'db.sqlite3')


# 检测是否为合法输入
def checkTable(t: str) -> bool:
if t in define.MODELIST or t == "illustJsonMsg":
return True
logger.error("checkTable检测到非法输入 " + t)
return False


# 连接数据库
def dbconnect() -> sqlite3.Connection:
conn = sqlite3.connect(DBFILE)
logger.info("打开数据库成功")
if not conn.execute("SELECT COUNT(*) FROM sqlite_master where type='table' and name='illustJsonMsg';").fetchall()[0][0]:
logger.info("正在创建数据表")
create_tb_cmd = '''
CREATE TABLE IF NOT EXISTS illustJsonMsg
(`illust` INTEGER NOT NULL PRIMARY KEY,
`time` INTEGER,
`json` JSON);
'''
conn.execute(create_tb_cmd)
create_index_cmd = '''
CREATE UNIQUE INDEX `index_illust` on `illustJsonMsg` (`illust`,`time`);
'''
conn.execute(create_index_cmd)
for t in define.MODELIST:
create_tb_cmd = '''
CREATE TABLE IF NOT EXISTS ''' + t + '''
(`illust` INTEGER NOT NULL PRIMARY KEY);
'''
conn.execute(create_tb_cmd)
conn.commit()
logger.info("数据表创建完成")
return conn


# 关闭数据库
def dbclose(conn: sqlite3.Connection) -> None:
conn.close()
logger.info("关闭数据库成功")


# 判断表中是否存在指定illust
def dbifhave(conn: sqlite3.Connection, table: str, illust: int) -> bool:
sql = 'select `illust` from `' + table + \
'` where `illust`=' + str(illust) + ';'
logger.debug(sql)
if checkTable(table) and conn.execute(sql).fetchall():
return True
else:
return False


# 插入元素(增)
def dbinsert(conn: sqlite3.Connection, mode: str, illust: int, item: dict) -> None:
logger.info("正在写入数据库 " + str(illust))
item.pop('url') # 已下载到本地,不需要url
if not dbifhave(conn, "illustJsonMsg", illust):
tmp = time.localtime()
timeint = tmp.tm_year*10000 + tmp.tm_mon*100 + tmp.tm_mday
insert_dt_cmd = 'insert into `illustJsonMsg` values (' + str(
illust) + ',' + str(timeint) + ',\'' + json.dumps(item) + '\');'
logger.debug(insert_dt_cmd)
conn.execute(insert_dt_cmd)
conn.commit()
if not dbifhave(conn, mode, illust):
insert_dt_cmd = 'insert into `' + mode + \
'` (`illust`) values (' + str(illust) + ');'
logger.debug(insert_dt_cmd)
conn.execute(insert_dt_cmd)
conn.commit()


# 抛弃单个元素(删)
def dbdropitem(conn: sqlite3.Connection, illust: int) -> None:
logger.info("尝试删除" + str(illust))
drop_sql = 'update `illustJsonMsg` set `time`=-1 where `illust`="' + \
str(illust) + '";'
conn.execute(drop_sql)
conn.commit()
logger.info("删除" + str(illust) + "成功")


# 抛弃全部元素(删)
def dbdropall(conn: sqlite3.Connection) -> None:
logger.info("尝试删除全部")
drop_sql = 'update `illustJsonMsg` set `time`=-1 where `time`!=-1;'
conn.execute(drop_sql)
conn.commit()
logger.info("删除全部成功")


# 抛弃指定类型的全部元素(删)
def dbdropmode(conn: sqlite3.Connection, typelist: list) -> None:
logger.info("尝试删除指定类型" + str(typelist))
for mode in typelist:
if mode not in define.MODELIST:
return
drop_sql = '''
update `illustJsonMsg` set `time`=-1 where `illust` in
(select `illust` from ''' + mode + ''') and `time`!=-1;
'''
conn.execute(drop_sql)
conn.commit
logger.info("删除指定类型成功" + str(typelist))


# 时间列表(查)
def dbgettimelist(conn: sqlite3.Connection) -> list:
logger.info("查询时间列表")
get_time_list_sql = 'select distinct `time` from `illustJsonMsg` where `time`!=-1 order by `time` desc;'
temp = conn.execute(get_time_list_sql).fetchall()
timelist = [temp[i][0] for i in range(len(temp))]
logger.info(str(timelist))
return timelist


# 指定时间和类型的元素列表(查)
def dbgetitems(conn: sqlite3.Connection, typelist: list, time: int = 0) -> list:
logger.info("查询元素列表")
logger.info("时间 : " + str(time))
logger.info("类型 : " + str(typelist))
imgs_sql = 'select `time`,`json` from `illustJsonMsg` where `illust` in ('
ifUnion = False
for t in typelist:
if t not in define.MODELIST:
continue
if ifUnion:
imgs_sql += ' union '
imgs_sql += 'select `illust` from ' + t
ifUnion = True
if time == 0:
imgs_sql += ') and `time`!=-1 order by `illust` desc;'
elif time == -1:
time = dbgettimelist(conn)[0]
imgs_sql += ') and `time`=' + str(time) + ' order by `illust` desc;'
else:
imgs_sql += ') and `time`=' + str(time) + ' order by `illust` desc;'
rt = conn.execute(imgs_sql).fetchall()
rtjson = {}
for i in rt:
if i[0] not in rtjson.keys():
rtjson[i[0]] = []
rtjson[i[0]].append(json.loads(i[1]))
logger.info(str(rtjson))
return rtjson


# 查询指定元素(查)
def dbgetillust(conn: sqlite3.Connection, illust: int) -> dict:
logger.info("查询元素 " + str(illust))
get_illust_sql = 'select `time`,`json` from `illustJsonMsg` where `illust`=' + \
str(illust) + ' and `time`!=-1;'
rt = conn.execute(get_illust_sql).fetchall()
if rt:
j = json.loads(rt[0][1])
else:
raise Exception('查询结果为空。')
rt = {"time": rt[0][0], "detail": j}
logger.info(str(rt))
return rt
15 changes: 15 additions & 0 deletions src/define.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# -*- coding: utf-8 -*-
from os import path

PATHDIR = path.dirname(path.dirname(path.abspath(__file__)))
STATICPATH = path.join(PATHDIR, "static")

MODELIST = ["day", "week", "month", "day_male", "day_female",
"week_original", "week_rookie", "day_manga", "day_r18", "day_male_r18",
"day_female_r18", "week_r18", "week_r18g"]
NORMALMODE = ["day", "week", "month", "day_male", "day_female",
"week_original", "week_rookie", "day_manga"]
R18MODE = ["day_r18", "day_male_r18",
"day_female_r18", "week_r18", "week_r18g"]

PIXIVCAT = "https://pixiv.cat/"
40 changes: 40 additions & 0 deletions src/deleteUnit.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# -*- coding: utf-8 -*-
import os
from .logger import logger
from . import config
from . import define


def delete(data: dict = {}, all: bool = False):
if not data and all:
# 删除全部
logger.info("尝试删除全部")
try:
os.removedirs(os.path.join(define.STATICPATH, config.IMGPATH))
logger.info("删除全部 - 完成")
except:
logger.error("删除全部 - 失败")
elif data:
logger.info("尝试删除指定文件")
basePath = os.path.join(define.STATICPATH, config.IMGPATH)
thumPath = os.path.join(basePath, "thum")
try:
for key in data:
stime = str(key)
yea = stime[:4]
mon = stime[4:6]
day = stime[6:8]
origPath = os.path.join(basePath, "orig", yea, mon, day)
items = data[key]
for item in items:
thumname = os.path.join(thumPath, str(item['illust']) + ".png")
if os.path.exists(thumname):
os.remove(thumname)
for i in range(item['count']):
filename = os.path.join(origPath, str(
item['illust']) + "-" + str(i) + "." + item['suffix'])
if os.path.exists(filename):
os.remove(filename)
logger.info("删除成功")
except:
logger.error("删除失败 - " + str(data))
21 changes: 21 additions & 0 deletions src/logger.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# -*- coding: utf-8 -*-
import logging
import sys
from . import config

# 日志记录器
logger = logging.getLogger('PixivFlask')
logger.setLevel(config.LOGLEVEL)
while logger.hasHandlers():
for i in logger.handlers:
logger.removeHandler(i)
if config.LOGON:
formatter = logging.Formatter('[ %(levelname)s ] : %(asctime)s - %(message)s')
fh = logging.FileHandler(config.LOGFILE, encoding='utf-8')
fh.setLevel(config.LOGLEVEL)
fh.setFormatter(formatter)
logger.addHandler(fh)
formatter = logging.Formatter('%(message)s')
ch = logging.StreamHandler(sys.stdout)
ch.setLevel(config.LOGLEVEL)
logger.addHandler(ch)
Loading

0 comments on commit a7b7da3

Please sign in to comment.