Archived
init: upload
This commit is contained in:
16 files changed
+613
No files matched your search
@@ -0,0 +1,22 @@
|
||||
#!/usr/bin/env python3
|
||||
# -- coding:utf-8 --
|
||||
# @Author: markushammered@gmail.com
|
||||
# @Development Tool: PyCharm
|
||||
# @Create Time: 2022/8/30
|
||||
# @File Name: __init__.py.py
|
||||
|
||||
|
||||
from src.main import main
|
||||
from src.api import api
|
||||
from fastapi import FastAPI
|
||||
|
||||
|
||||
def create_app():
|
||||
"""
|
||||
Factory function
|
||||
:return:
|
||||
"""
|
||||
app = FastAPI()
|
||||
app.include_router(main)
|
||||
app.include_router(api, prefix='/api')
|
||||
return app
|
||||
@@ -0,0 +1,12 @@
|
||||
#!/usr/bin/env python3
|
||||
# -- coding:utf-8 --
|
||||
# @Author: markushammered@gmail.com
|
||||
# @Development Tool: PyCharm
|
||||
# @Create Time: 2022/9/2
|
||||
# @File Name: __init__.py.py
|
||||
|
||||
from fastapi import APIRouter
|
||||
|
||||
api = APIRouter()
|
||||
|
||||
from src.api import view
|
||||
@@ -0,0 +1,40 @@
|
||||
#!/usr/bin/env python3
|
||||
# -- coding:utf-8 --
|
||||
# @Author: markushammered@gmail.com
|
||||
# @Development Tool: PyCharm
|
||||
# @Create Time: 2022/9/2
|
||||
# @File Name: view.py
|
||||
|
||||
|
||||
from src.api import api
|
||||
from src.db import SQLite
|
||||
from fastapi.responses import FileResponse
|
||||
|
||||
|
||||
@api.get('/query/{name}')
|
||||
async def query(name: str):
|
||||
times = SQLite().query(name)[1]
|
||||
return {name: times}
|
||||
|
||||
|
||||
@api.get('/query-all/')
|
||||
async def query_all(limit: int = 30):
|
||||
data = SQLite().query_all()[:limit]
|
||||
result = {}
|
||||
for i in data:
|
||||
result[i[0]] = i[1]
|
||||
return result
|
||||
|
||||
|
||||
@api.get('/export/')
|
||||
async def export():
|
||||
return FileResponse('./src/db/data.sqlite')
|
||||
|
||||
|
||||
@api.get('/query-theme/{name}')
|
||||
async def query_theme(name: str):
|
||||
themes = []
|
||||
for i in range(10):
|
||||
data = SQLite().query_image(name + '/' + str(i))[0]
|
||||
themes.append(data)
|
||||
return themes
|
||||
@@ -0,0 +1,60 @@
|
||||
#!/usr/bin/env python3
|
||||
# -- coding:utf-8 --
|
||||
# @Author: markushammered@gmail.com
|
||||
# @Development Tool: PyCharm
|
||||
# @Create Time: 2022/8/30
|
||||
# @File Name: __init__.py.py
|
||||
|
||||
|
||||
import os
|
||||
import sqlite3
|
||||
from urllib.request import urlopen
|
||||
|
||||
|
||||
class SQLite:
|
||||
def __init__(self):
|
||||
self.conn = sqlite3.connect('./src/db/data.sqlite')
|
||||
# self.conn = sqlite3.connect('./data.sqlite')
|
||||
self.cursor = self.conn.cursor()
|
||||
|
||||
def __del__(self):
|
||||
self.conn.commit()
|
||||
self.cursor.close()
|
||||
self.conn.close()
|
||||
|
||||
def query(self, _id: str) -> tuple:
|
||||
self.cursor.execute('select * from data where id="%s"' % _id)
|
||||
result = self.cursor.fetchone()
|
||||
if result is None:
|
||||
self.insert(_id)
|
||||
return tuple([_id, 0])
|
||||
self.update(_id, result[1])
|
||||
return result
|
||||
|
||||
def insert(self, _id: str) -> bool:
|
||||
self.cursor.execute('insert into data (id, times) values ("%s", 0)' % _id)
|
||||
return True
|
||||
|
||||
def update(self, _id: str, times: int) -> bool:
|
||||
times += 1
|
||||
self.cursor.execute('update data set times=%s where id="%s"' % (times, _id))
|
||||
return True
|
||||
|
||||
def query_all(self) -> list:
|
||||
self.cursor.execute('select * from data')
|
||||
result = self.cursor.fetchall()
|
||||
return result
|
||||
|
||||
def query_image(self, _id: str) -> list:
|
||||
self.cursor.execute('select * from image where id="%s"' % _id)
|
||||
result = self.cursor.fetchall()
|
||||
return result
|
||||
|
||||
|
||||
if __name__ != '__main__':
|
||||
if not os.path.exists('./src/db/data.sqlite'):
|
||||
print('database file not exists, start downloading...')
|
||||
resp = urlopen('https://pac.rtst.tech/static_file_hosting/static/counter/data.sqlite').read()
|
||||
with open('./src/db/data.sqlite', 'wb') as fp:
|
||||
fp.write(resp)
|
||||
print('Done.')
|
||||
@@ -0,0 +1,13 @@
|
||||
#!/usr/bin/env python3
|
||||
# -- coding:utf-8 --
|
||||
# @Author: markushammered@gmail.com
|
||||
# @Development Tool: PyCharm
|
||||
# @Create Time: 2022/8/30
|
||||
# @File Name: __init__.py.py
|
||||
|
||||
|
||||
from fastapi import APIRouter
|
||||
|
||||
main = APIRouter()
|
||||
|
||||
from src.main import view
|
||||
@@ -0,0 +1,25 @@
|
||||
#!/usr/bin/env python3
|
||||
# -- coding:utf-8 --
|
||||
# @Author: markushammered@gmail.com
|
||||
# @Development Tool: PyCharm
|
||||
# @Create Time: 2022/8/30
|
||||
# @File Name: view.py
|
||||
|
||||
|
||||
from src.main import main
|
||||
from src.utils import resp
|
||||
from fastapi import Request
|
||||
from fastapi.templating import Jinja2Templates
|
||||
|
||||
template = Jinja2Templates('./src/templates')
|
||||
|
||||
|
||||
@main.get('/{name}')
|
||||
async def index(req: Request, name: str, length: int = 7, theme: str = 'lewd'):
|
||||
response = await resp(name, length, theme)
|
||||
|
||||
return template.TemplateResponse('index.html', context={'request': req,
|
||||
'context': response['context'],
|
||||
'g_width': response['g_width'],
|
||||
'g_height': response['g_height']},
|
||||
headers=response['headers'])
|
||||
@@ -0,0 +1,10 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<svg width="{{ g_width }}" height="{{ g_height }}" version="1.1" xmlns="http://www.w3.org/2000/svg"
|
||||
xmlns:xlink="http://www.w3.org/1999/xlink">
|
||||
<g>
|
||||
{% for value in context %}
|
||||
<image x="{{ value.position }}" y="0" width="{{ value.width }}" height="{{ value.height }}"
|
||||
xlink:href="data:image/gif;base64,{{ value.base64 }}"></image>
|
||||
{% endfor %}
|
||||
</g>
|
||||
</svg>
|
||||
|
After Width: | Height: | Size: 445 B |
@@ -0,0 +1,6 @@
|
||||
#!/usr/bin/env python3
|
||||
# -- coding:utf-8 --
|
||||
# @Author: markushammered@gmail.com
|
||||
# @Development Tool: PyCharm
|
||||
# @Create Time: 2022/8/30
|
||||
# @File Name: __init__.py.py
|
||||
@@ -0,0 +1,9 @@
|
||||
#!/usr/bin/env python3
|
||||
# -- coding:utf-8 --
|
||||
# @Author: markushammered@gmail.com
|
||||
# @Development Tool: PyCharm
|
||||
# @Create Time: 2022/8/30
|
||||
# @File Name: __init__.py.py
|
||||
|
||||
|
||||
from src.utils.response import resp
|
||||
@@ -0,0 +1,51 @@
|
||||
#!/usr/bin/env python3
|
||||
# -- coding:utf-8 --
|
||||
# @Author: markushammered@gmail.com
|
||||
# @Development Tool: PyCharm
|
||||
# @Create Time: 2022/8/30
|
||||
# @File Name: convert.py
|
||||
|
||||
|
||||
import os
|
||||
import base64
|
||||
import sqlite3
|
||||
import asyncio
|
||||
|
||||
|
||||
class SQLite:
|
||||
def __init__(self):
|
||||
self.conn = sqlite3.connect('../db/data.sqlite')
|
||||
self.cursor = self.conn.cursor()
|
||||
|
||||
def __del__(self):
|
||||
self.conn.commit()
|
||||
self.cursor.close()
|
||||
self.conn.close()
|
||||
|
||||
async def insert(self, _id: str, b64: str, width: int, height: int) -> bool:
|
||||
self.cursor.execute('insert into image values ("%s", "%s", %s, %s)' % (_id, b64, width, height))
|
||||
return True
|
||||
|
||||
|
||||
async def convert():
|
||||
dirs = os.listdir('../static')
|
||||
for i in dirs:
|
||||
files = os.listdir(f'../static/{i}')
|
||||
for b in files:
|
||||
with open(f'../static/{i}/{b}', 'rb') as fp:
|
||||
if i == 'moebooru':
|
||||
width = 45
|
||||
height = 100
|
||||
elif i == 'lewd':
|
||||
width = 45
|
||||
height = 100
|
||||
elif i == 'lisu':
|
||||
width = 66
|
||||
height = 152
|
||||
else:
|
||||
width = 68
|
||||
height = 150
|
||||
await SQLite().insert(f'{i}/{b}', base64.b64encode(fp.read()).decode('utf-8'), width, height)
|
||||
|
||||
|
||||
asyncio.run(convert())
|
||||
@@ -0,0 +1,40 @@
|
||||
#!/usr/bin/env python3
|
||||
# -- coding:utf-8 --
|
||||
# @Author: markushammered@gmail.com
|
||||
# @Development Tool: PyCharm
|
||||
# @Create Time: 2022/8/30
|
||||
# @File Name: response.py
|
||||
|
||||
|
||||
from src.db import SQLite
|
||||
|
||||
|
||||
async def resp(_id: str, length: int = 7, theme: str = 'lewd') -> dict:
|
||||
"""
|
||||
return a response
|
||||
:param _id:
|
||||
:param length:
|
||||
:param theme:
|
||||
:return:
|
||||
"""
|
||||
times = SQLite().query(_id)[1]
|
||||
str_number = str(times) # 将整形转换为字符串
|
||||
len_number = len(str_number) # 再获取字符串长度
|
||||
g_length = length * '0' # 根据输入的位数来自动生成0的数量
|
||||
show_number = str(g_length[:-len_number] + str_number)
|
||||
context = []
|
||||
w_h = SQLite().query_image(theme + '/0')[0]
|
||||
count = 0 # 计数器, 每次遍历一次则加一, 让图片x轴相乘
|
||||
for i in show_number:
|
||||
data = SQLite().query_image(theme + '/' + i)[0]
|
||||
context.append({'position': data[-2] * count,
|
||||
'width': data[-2],
|
||||
'height': data[-1],
|
||||
'base64': data[1]})
|
||||
count += 1
|
||||
headers = {'cache-control': 'max-age=0, no-cache, no-store, must-revalidate'}
|
||||
|
||||
return {'context': context,
|
||||
'g_width': length * w_h[-2],
|
||||
'g_height': w_h[-1],
|
||||
'headers': headers}
|
||||
Reference in New Issue
Block a user