randydev commited on
Commit
3a640bd
·
verified ·
1 Parent(s): 3abaf09

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +26 -3
main.py CHANGED
@@ -2,6 +2,8 @@ import logging
2
  import base64
3
  import requests
4
  import uvloop
 
 
5
  from bs4 import BeautifulSoup
6
  from contextlib import asynccontextmanager
7
  from akenoai import AkenoXToJs as js
@@ -51,6 +53,26 @@ def get_random_from_channel(link):
51
  )
52
  return username, random_id
53
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
54
  @fast_app.on_event("startup")
55
  async def startup_event():
56
  user = await client.start()
@@ -100,10 +122,11 @@ async def get_user_story(link: str = None):
100
  if file_id:
101
  image_path = await user_client.download_media(file_id)
102
 
103
- with open(image_path, "rb") as f:
104
- base64_image = base64.b64encode(f.read()).decode("utf-8")
 
105
  return {
106
- "download": base64_image,
107
  "random_file_id": file_id,
108
  "caption": caption
109
  }
 
2
  import base64
3
  import requests
4
  import uvloop
5
+ import aiofiles
6
+ import aiohttp
7
  from bs4 import BeautifulSoup
8
  from contextlib import asynccontextmanager
9
  from akenoai import AkenoXToJs as js
 
53
  )
54
  return username, random_id
55
 
56
+ async def upload_to_catbox(dl_path: str) -> str:
57
+ base_url = "https://catbox.moe/user/api.php"
58
+
59
+ async with aiohttp.ClientSession() as session:
60
+ form_data = aiohttp.FormData()
61
+ form_data.add_field("reqtype", "fileupload")
62
+
63
+ async with aiofiles.open(dl_path, mode="rb") as file:
64
+ file_data = await file.read()
65
+ form_data.add_field(
66
+ "fileToUpload",
67
+ file_data,
68
+ filename=dl_path.split("/")[-1],
69
+ content_type="application/octet-stream"
70
+ )
71
+
72
+ async with session.post(base_url, data=form_data) as response:
73
+ response.raise_for_status()
74
+ return (await response.text()).strip()
75
+
76
  @fast_app.on_event("startup")
77
  async def startup_event():
78
  user = await client.start()
 
122
  if file_id:
123
  image_path = await user_client.download_media(file_id)
124
 
125
+ # with open(image_path, "rb") as f:
126
+ # base64_image = base64.b64encode(f.read()).decode("utf-8")
127
+ url_dl = await upload_to_catbox(image_path)
128
  return {
129
+ "download": url_dl,
130
  "random_file_id": file_id,
131
  "caption": caption
132
  }