Skip to content

Commit

Permalink
MOD:框架升级为fastapi
Browse files Browse the repository at this point in the history
  • Loading branch information
ShilongLee committed Jul 14, 2024
1 parent 8890388 commit 217db7f
Show file tree
Hide file tree
Showing 112 changed files with 1,400 additions and 11,871 deletions.
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ RUN apk add --update nodejs npm \

COPY . .

ENV THREADS=4
ENV FILE = config/docker-config.yaml

EXPOSE 8080

CMD gunicorn -c config/gunicorn.conf.py -w $THREADS -b :8080 main:app
CMD uvicorn --host 0.0.0.0 --port $(port) main:app
19 changes: 12 additions & 7 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
.PHONY: venv
.PHONY: venv, install, clean, help

venv:
python3 -m venv .venv

.PHONY: install
install:
. .venv/bin/activate; pip3 install -r requirements.txt

.PHONY: clean

clean:
rm -rf .venv

port ?= 8080
thread ?= 4
port ?= 10002
run: venv install
. .venv/bin/activate; .venv/bin/gunicorn -c config/gunicorn.conf.py -w $(thread) -b :$(port) main:app
. .venv/bin/activate; .venv/bin/uvicorn --host 0.0.0.0 --port $(port) main:app

help:
@echo "Available targets:"
@echo " venv Create a virtual environment"
@echo " install Install dependencies"
@echo " clean Remove the virtual environment"
@echo " run Run the application"
1 change: 1 addition & 0 deletions config/config.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
logger:
type : file
level: INFO
backupcount: 144
format: "[%(asctime)s][%(name)s][%(levelname)s]: %(message)s"
Expand Down
4 changes: 4 additions & 0 deletions config/docker-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
logger:
type : console
level: INFO
format: "[%(asctime)s][%(name)s][%(levelname)s]: %(message)s"
8 changes: 0 additions & 8 deletions config/gunicorn.conf.py

This file was deleted.

55 changes: 30 additions & 25 deletions data/driver.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
import sqlite3
from contextlib import closing
from contextlib import asynccontextmanager, closing
from lib.logger import logger
import time
import sqlite3
import aiosqlite

class SqliteStore:
def __init__(self, db_path):
self.db_path = db_path

def _get_connection(self):
@asynccontextmanager
async def _get_connection(self):
async with aiosqlite.connect(self.db_path) as conn:
conn.row_factory = aiosqlite.Row
yield conn

def _get_sync_connection(self):
conn = sqlite3.connect(self.db_path)
conn.row_factory = sqlite3.Row # 结果以字典形式返回
conn.row_factory = sqlite3.Row
return conn

class CommonAccount(SqliteStore):
Expand All @@ -20,7 +27,7 @@ def __init__(self, store_path):
self._create_table()

def _create_table(self):
with closing(self._get_connection()) as conn, closing(conn.cursor()) as cursor:
with closing(self._get_sync_connection()) as conn, closing(conn.cursor()) as cursor:
try:
sql = f'''
CREATE TABLE IF NOT EXISTS {self.table_name} (
Expand All @@ -36,49 +43,47 @@ def _create_table(self):
except Exception as e:
logger.error(f'failed to create table, error: {e}')

def save(self, id: str, cookie: str, expired: int) -> bool:
async def save(self, id: str, cookie: str, expired: int) -> bool:
ct = ut = int(time.time())
with closing(self._get_connection()) as conn, closing(conn.cursor()) as cursor:
async with self._get_connection() as conn:
try:
sql = f'UPDATE {self.table_name} SET cookie = ?, expired = ?, ut = ? WHERE id = ?'
cursor.execute(sql, (cookie, expired, ut, id))
if cursor.rowcount == 0:
await conn.execute(sql, (cookie, expired, ut, id))
if conn.total_changes == 0:
sql = f'INSERT INTO {self.table_name} (cookie, expired, ct, ut, id) VALUES (?, ?, ?, ?, ?)'
cursor.execute(sql, (cookie, expired, ct, ut, id))
conn.commit()
await conn.execute(sql, (cookie, expired, ct, ut, id))
await conn.commit()
return True
except Exception as e:
logger.error(f'failed to save cookies, error: {e}')
conn.rollback()
await conn.rollback()
return False


def load(self, offset: int = 0, limit: int = 0) -> list:
with closing(self._get_connection()) as conn, closing(conn.cursor()) as cursor:
async def load(self, offset: int = 0, limit: int = 0) -> list:
async with self._get_connection() as conn:
try:
if limit == 0:
sql = f'SELECT * FROM {self.table_name}'
cursor.execute(sql)
cursor = await conn.execute(sql)
else:
sql = f'SELECT * FROM {self.table_name} LIMIT ? OFFSET ?'
cursor.execute(sql, (limit, offset))
results = cursor.fetchall()
cursor = await conn.execute(sql, (limit, offset))
results = await cursor.fetchall()
return [dict(row) for row in results]
except Exception as e:
logger.error(f'failed to load cookies, error: {e}')
conn.rollback()
await conn.rollback()
return []

def expire(self, id: str) -> bool:
async def expire(self, id: str) -> bool:
ut = int(time.time())
with closing(self._get_connection()) as conn, closing(conn.cursor()) as cursor:
async with self._get_connection() as conn:
try:
sql = f'UPDATE {self.table_name} SET expired = ?, ut = ? WHERE id = ?'
cursor.execute(sql, (1, ut, id))
conn.commit()
await conn.execute(sql, (1, ut, id))
await conn.commit()
return True
except Exception as e:
logger.error(f'failed to save cookies, error: {e}')
conn.rollback()
await conn.rollback()
return False

4 changes: 4 additions & 0 deletions docs/api/xhs/xhs.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@

### 获取笔记详情

- **功能说明**

无水印视频,用`http://sns-video-bd.xhscdn.com/`拼接上响应中的`origin_video_key`即可。

- **URL**

`/xhs/detail`
Expand Down
Loading

0 comments on commit 217db7f

Please sign in to comment.