Skip to content

Commit

Permalink
Update tools.py
Browse files Browse the repository at this point in the history
  • Loading branch information
ufoptg authored Apr 7, 2024
1 parent c0ddedb commit a10e6a5
Showing 1 changed file with 59 additions and 21 deletions.
80 changes: 59 additions & 21 deletions pyUltroid/fns/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,34 +233,72 @@ async def saavn_search(query: str):


async def webuploader(chat_id: int, msg_id: int, uploader: str):
file = _webupload_cache[int(chat_id)][int(msg_id)]
LOGS.info("webuploader function called with uploader: %s", uploader)
if chat_id in _webupload_cache and msg_id in _webupload_cache[chat_id]:
file = _webupload_cache[chat_id][msg_id]
else:
return "File not found in cache."

sites = {
"anonfiles": {"url": "https://api.anonfiles.com/upload", "json": True},
"siasky": {"url": "https://siasky.net/skynet/skyfile", "json": True},
"file.io": {"url": "https://file.io", "json": True},
"bayfiles": {"url": "https://api.bayfiles.com/upload", "json": True},
"x0.at": {"url": "https://x0.at/", "json": False},
"file.io": {"url": "https://file.io/", "json": True},
"0x0.st": {"url": "https://0x0.st", "json": False},
"transfer": {"url": "https://transfer.sh", "json": False},
"catbox": {"url": "https://catbox.moe/user/api.php", "json": False},
"filebin": {"url": "https://filebin.net", "json": False},
}

if uploader and uploader in sites:
url = sites[uploader]["url"]
json = sites[uploader]["json"]
with open(file, "rb") as data:
# todo: add progress bar
status = await async_searcher(
url, data={"file": data.read()}, post=True, re_json=json
)
if isinstance(status, dict):
if "skylink" in status:
return f"https://siasky.net/{status['skylink']}"
if status["status"] == 200 or status["status"] is True:
json_format = sites[uploader]["json"]
else:
return "Uploader not supported or invalid."

files = {"file": open(file, "rb")} # Adjusted for both formats

try:
if uploader == "filebin":
cmd = f"curl -X POST --data-binary '@{file}' -H 'filename: \"{file}\"' \"{url}\""
response = subprocess.run(cmd, shell=True, capture_output=True, text=True)
if response.returncode == 0:
response_json = json.loads(response.stdout)
bin_id = response_json.get("bin", {}).get("id")
if bin_id:
filebin_url = f"https://filebin.net/{bin_id}"
return filebin_url
else:
return "Failed to extract bin ID from Filebin response"
else:
return f"Failed to upload file to Filebin: {response.stderr.strip()}"
elif uploader == "catbox":
cmd = f"curl -F reqtype=fileupload -F time=24h -F 'fileToUpload=@{file}' {url}"
elif uploader == "0x0.st":
cmd = f"curl -F 'file=@{file}' {url}"
elif uploader == "file.io" or uploader == "siasky":
try:
link = status["link"]
except KeyError:
link = status["data"]["file"]["url"]["short"]
return link
del _webupload_cache[int(chat_id)][int(msg_id)]
return status
status = await async_searcher(
url, data=files, post=True, re_json=json_format
)
except Exception as e:
return f"Failed to upload file: {e}"
if isinstance(status, dict):
if "skylink" in status:
return f"https://siasky.net/{status['skylink']}"
if status.get("status") == 200:
return status.get("link")
else:
raise ValueError("Uploader not supported")

response = subprocess.run(cmd, shell=True, capture_output=True, text=True)
if response.returncode == 0:
return response.stdout.strip()
else:
return f"Failed to upload file: {response.stderr.strip()}"
except Exception as e:
return f"Failed to upload file: {e}"

del _webupload_cache.get(chat_id, {})[msg_id]
return "Failed to get valid URL for the uploaded file."


def get_all_files(path, extension=None):
Expand Down

0 comments on commit a10e6a5

Please sign in to comment.