Skip to content

Commit

Permalink
合并一個更新 (#3)
Browse files Browse the repository at this point in the history
* 完整实现功能

* 完整实现功能

* 完整实现功能

* 完整实现功能

* 完整实现功能

* 完整实现功能
  • Loading branch information
northgreen authored Dec 5, 2023
1 parent a57dfbe commit a2525b7
Showing 1 changed file with 26 additions and 4 deletions.
30 changes: 26 additions & 4 deletions bilibili_dm_plugin/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@

import os
from urllib.parse import urlparse
import urllib.request
from . import blivedm
from depends import plugin_main, msgs, connects
import shutil


import aiohttp
from aiohttp import web
Expand All @@ -36,14 +37,27 @@
local_path = __path__[0]


def return_for_face(path: str):
async def download_file(url, file_name):
async with aiohttp.ClientSession() as session:
async with session.get(url) as response:
with open(file_name, 'wb') as file:
while True:
chunk = await response.content.read(1024)
if not chunk:
break
file.write(chunk)


async def return_for_face(path: str):

if path:
files = os.listdir(os.path.join(local_path, "tmp"))
file = os.path.basename(urlparse(path).path)
if file in files:
return web.FileResponse(os.path.join(local_path, "tmp", file))
else:
urllib.request.urlretrieve(path, os.path.join(local_path, "tmp", file))
await download_file(path, os.path.join(local_path, "tmp", file))

return web.FileResponse(os.path.join(local_path, "tmp", file))


Expand Down Expand Up @@ -94,20 +108,28 @@ def _on_gift(self, client: ws_base.WebSocketClientBase, message: web_models.Gift
class Plugin_Main(plugin_main.Plugin_Main):

def plugin_init(self):
if os.path.exists(os.path.join(local_path, "tmp")):
shutil.rmtree(os.path.join(local_path, "tmp"))
os.mkdir(os.path.join(local_path, "tmp"))
else:
os.mkdir(os.path.join(local_path, "tmp"))
self.runners = []
self.plugin_name = "b_dm_plugin"

self.sprit_cgi_support = True
self.sprit_cgi_lists["face"] = self.sprit_cgi
self.read_config()

return "message"

async def sprit_cgi(self, request: web.Request):
ret = web.Response(status=404, text="no such file")
ret = return_for_face(request.rel_url.query.get("url"))
ret = await return_for_face(request.rel_url.query.get("url"))
return ret

async def plugin_main(self):
self.config = {"test": "test"}
self.update_config()
while True:
await asyncio.sleep(1)

Expand Down

0 comments on commit a2525b7

Please sign in to comment.