use data.json

This commit is contained in:
xtaodada 2022-08-02 23:24:13 +08:00
parent dad4f45f46
commit 1e3c984751
Signed by: xtaodada
GPG Key ID: 4CBB3F4FA8C85659
4 changed files with 21 additions and 9 deletions

5
ci.py
View File

@ -1,16 +1,13 @@
import json
from configparser import RawConfigParser from configparser import RawConfigParser
from os import sep, mkdir from os import mkdir
from os.path import exists from os.path import exists
from pyrogram import Client from pyrogram import Client
from apscheduler.schedulers.asyncio import AsyncIOScheduler from apscheduler.schedulers.asyncio import AsyncIOScheduler
from httpx import AsyncClient from httpx import AsyncClient
from sqlitedict import SqliteDict
if not exists("data"): if not exists("data"):
mkdir("data") mkdir("data")
sqlite = SqliteDict(f"data{sep}data.sqlite", encode=json.dumps, decode=json.loads, autocommit=True)
# data.sqlite 结构如下: # data.sqlite 结构如下:
# { # {
# "update_time": int, # "update_time": int,

16
defs/dump_data.py Normal file
View File

@ -0,0 +1,16 @@
import json
import time as time_
from os.path import exists
def get_time() -> int:
if exists("data.json"):
with open("data.json", "r") as f:
data = json.load(f)
return data.get("update_time", int(time_.time()) - 600)
return int(time_.time()) - 600
def set_time(time: int) -> None:
with open("data.json", "w") as f:
json.dump({"update_time": time}, f)

View File

@ -1,13 +1,13 @@
import time as time_
from typing import List from typing import List
from ci import client, sqlite from ci import client
from defs.model import New from defs.model import New
from defs.dump_data import get_time, set_time
async def get_news(time: int=None) -> List[New]: async def get_news(time: int=None) -> List[New]:
if not time: if not time:
time = sqlite.get("update_time", int(time_.time()) - 600) time = get_time()
url = f"https://papi.jiemian.com/page/api/kuaixun/getLastest?cid=1325kb&end_time={time}&tagid=1325" url = f"https://papi.jiemian.com/page/api/kuaixun/getLastest?cid=1325kb&end_time={time}&tagid=1325"
data = await client.get(url) data = await client.get(url)
if data.status_code != 200: if data.status_code != 200:
@ -18,5 +18,5 @@ async def get_news(time: int=None) -> List[New]:
for new in data: for new in data:
if new.publish_time > update_time: if new.publish_time > update_time:
update_time = new.publish_time update_time = new.publish_time
sqlite["update_time"] = update_time set_time(update_time)
return data return data

View File

@ -2,5 +2,4 @@ Pyrogram==2.0.35
Tgcrypto>=1.2.3 Tgcrypto>=1.2.3
httpx>=0.22.0 httpx>=0.22.0
apscheduler>=3.8.1 apscheduler>=3.8.1
sqlitedict>=2.0.0
pytz pytz