forked from friendly-telegram/modules-repo
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtransfersh.py
67 lines (55 loc) · 2.46 KB
/
transfersh.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# Friendly Telegram (telegram userbot)
# Copyright (C) 2018-2019 The Authors
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
import logging
import requests
import asyncio
from .. import loader, utils
logger = logging.getLogger(__name__)
def sgen(agen, loop):
while True:
try:
yield utils.run_async(loop, agen.__anext__())
except StopAsyncIteration:
return
@loader.tds
class TransferShMod(loader.Module):
"""Upload to and from transfer.sh"""
strings = {"name": "transfer.sh support",
"up_cfg_doc": "URL to upload the file to.",
"no_file": "<code>Provide a file to upload</code>",
"uploading": "<code>Uploading...</code>",
"uploaded": "<a href={}>Uploaded!</a>"}
def __init__(self):
self.config = loader.ModuleConfig("UPLOAD_URL", "https://transfer.sh/{}",
lambda m: self.strings("up_cfg_doc", m))
@loader.unrestricted
@loader.ratelimit
async def uploadshcmd(self, message):
"""Uploads to transfer.sh"""
if message.file:
msg = message
else:
msg = (await message.get_reply_message())
doc = getattr(msg, "media", None)
if doc is None:
await utils.answer(message, self.strings("no_file", message))
return
doc = message.client.iter_download(doc)
logger.debug("begin transfer")
await utils.answer(message, self.strings("uploading", message))
r = await utils.run_sync(requests.put, self.config["UPLOAD_URL"].format(msg.file.name),
data=sgen(doc, asyncio.get_event_loop()))
logger.debug(r)
r.raise_for_status()
logger.debug(r.headers)
await utils.answer(message, self.strings("uploaded", message).format(r.text))