-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_external_link.py
159 lines (146 loc) · 5.76 KB
/
get_external_link.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# (c) Shrimadhav U K
# the logging things
import logging
logging.basicConfig(level=logging.DEBUG,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)
from datetime import datetime
import os
import requests
import subprocess
import time
import json
import re
# the secret configuration specific things
if bool(os.environ.get("WEBHOOK", False)):
from sample_config import Config
else:
from config import Config
# the Strings used for this "thing"
from translation import Translation
import pyrogram
logging.getLogger("pyrogram").setLevel(logging.WARNING)
from helper_funcs.chat_base import TRChatBase
from helper_funcs.display_progress import progress_for_pyrogram
from pydrive.drive import GoogleDrive
@pyrogram.Client.on_message(pyrogram.Filters.command(["getlink"]))
def get_link(bot, update):
TRChatBase(update.from_user.id, update.text, "getlink")
if str(update.from_user.id) in Config.BANNED_USERS:
bot.send_message(
chat_id=update.chat.id,
text=Translation.ABUSIVE_USERS,
reply_to_message_id=update.message_id,
disable_web_page_preview=True,
parse_mode=pyrogram.ParseMode.HTML
)
return
logger.info(update.from_user)
if update.reply_to_message is not None:
reply_message = update.reply_to_message
download_location = Config.DOWNLOAD_LOCATION + "/"
start = datetime.now()
a = bot.send_message(
chat_id=update.chat.id,
text=Translation.DOWNLOAD_START,
reply_to_message_id=update.message_id
)
c_time = time.time()
after_download_file_name = bot.download_media(
message=reply_message,
file_name=download_location,
progress=progress_for_pyrogram,
progress_args=(Translation.DOWNLOAD_START, a.message_id, update.chat.id, c_time)
)
download_extension = after_download_file_name.rsplit(".", 1)[-1]
bot.edit_message_text(
text=Translation.SAVED_RECVD_DOC_FILE,
chat_id=update.chat.id,
message_id=a.message_id
)
end_one = datetime.now()
if str(update.from_user.id) in Config.G_DRIVE_AUTH_DRQ:
gauth = Config.G_DRIVE_AUTH_DRQ[str(update.from_user.id)]
# Create GoogleDrive instance with authenticated GoogleAuth instance.
drive = GoogleDrive(gauth)
file_inance = drive.CreateFile()
# Read file and set it as a content of this instance.
file_inance.SetContentFile(after_download_file_name)
file_inance.Upload() # Upload the file.
end_two = datetime.now()
time_taken_for_upload = (end_two - end_one).seconds
logger.info(file_inance)
adfulurl = file_inance.webContentLink
max_days = 0
else:
url = "https://filebin.net"
max_days = 5
command_to_exec = [
"curl",
"-X", "POST",
"-d", "@"+after_download_file_name,
"-H", "Filename: "+after_download_file_name,
"-H", "Transfer-Encoding: chunked",
url
]
# Example of t_response
# {
# "filename": "Hell.srt",
# "bin": "s64jjtgagpgevz8h",
# "bytes": 96237,
# "mime": "text/plain; charset=utf-8",
# "created": "2019-07-18T10:13:20.677271056Z",
# "links": [
# {
# "rel": "file",
# "href": "https://filebin.net/s64jjtghgpgevz8h/Hellboy.srt"
# },
# {
# "rel": "bin",
# "href": "https://filebin.net/s64jjtghgpgevz8h"
# }
# ],
# "datetime": "0001-01-01T00:00:00Z"
# }
bot.edit_message_text(
text=Translation.UPLOAD_START,
chat_id=update.chat.id,
message_id=a.message_id
)
try:
logger.info(command_to_exec)
t_response = subprocess.check_output(command_to_exec, stderr=subprocess.STDOUT)
except subprocess.CalledProcessError as exc:
logger.info("Status : FAIL", exc.returncode, exc.output)
bot.edit_message_text(
chat_id=update.chat.id,
text=exc.output.decode("UTF-8"),
message_id=a.message_id
)
return False
else:
logger.info(t_response)
print ( t_response )
# t_response_arry = json.loads(t_response.decode("UTF-8").split("\n")[-1].strip())['url']
scan = re.search(r'.*\"file\"\,[\s\n]*"href":\s*\"(.+)\"', t_response.decode("UTF-8"))
#shorten_api_url = "http://ouo.io/api/{}?s={}".format(Config.OUO_IO_API_KEY, t_response_arry)
#adfulurl = requests.get(shorten_api_url).text
bot.edit_message_text(
chat_id=update.chat.id,
text=Translation.AFTER_GET_DL_LINK.format(scan.group(1), max_days),
parse_mode=pyrogram.ParseMode.HTML,
message_id=a.message_id,
disable_web_page_preview=True
)
try:
os.remove(after_download_file_name)
except:
pass
else:
bot.send_message(
chat_id=update.chat.id,
text=Translation.REPLY_TO_DOC_GET_LINK,
reply_to_message_id=update.message_id
)