Skip to content

Commit

Permalink
fix help and add direct link and telegram media support to gdrive
Browse files Browse the repository at this point in the history
  • Loading branch information
rking32 committed Apr 24, 2020
1 parent 1b5507a commit aa20485
Show file tree
Hide file tree
Showing 13 changed files with 135 additions and 46 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ async def testing(message: Message):
### Video Tutorial 🎥

[![Tutorial](resources/tutorial.jpg)](https://youtu.be/-XJj686zeiY)
[![Tutorial](resources/tutorial.jpg)](https://youtu.be/-XJj686zeiY "Tutorial")

### Support & Discussions 👥

Expand Down
2 changes: 1 addition & 1 deletion app.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"value": "True"
},
"DOWN_PATH": {
"description": "Set your working directory (name or path)",
"description": "Set name to your working directory",
"required": false
},
"PREFERRED_LANGUAGE": {
Expand Down
2 changes: 1 addition & 1 deletion config.env.sample
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ G_DRIVE_IS_TD = False
# ----------- OPTIONAL ----------- #


# Set your working directory (name or path)
# Set name to your working directory
DOWN_PATH = "downloads/"


Expand Down
4 changes: 2 additions & 2 deletions userge/core/_userge/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ async def send_message(self,
chat_id: Union[int, str],
text: str,
del_in: int = -1,
log: bool = False,
log: Union[bool, str] = False,
parse_mode: Union[str, object] = object,
disable_web_page_preview: Optional[bool] = None,
disable_notification: Optional[bool] = None,
Expand Down Expand Up @@ -320,7 +320,7 @@ def get_help(self,
return sorted(list(self.__help_dict)), True # names of all modules

if not key.startswith('.') and key in self.__help_dict and \
(len(self.__help_dict[key]) > 1 or list(self.__help_dict[key])[0] != key):
(len(self.__help_dict[key]) > 1 or list(self.__help_dict[key])[0].lstrip('.') != key):
return sorted(list(self.__help_dict[key])), False # all commands for that module

dict_ = {x: y for _, i in self.__help_dict.items() for x, y in i.items()}
Expand Down
26 changes: 13 additions & 13 deletions userge/core/_userge/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,12 +143,12 @@ def __filter(self) -> None:
input_str = self.input_str

for i in input_str.strip().split():
match = re.match(f"({prefix}[a-z]+)($|[0-9]+)?$", i)
match = re.match(f"({prefix}[a-zA-Z]+)($|[0-9]+)?$", i)

if match:
items: Sequence[str] = match.groups()
self.__flags[items[0].lstrip(prefix) if del_pre \
else items[0]] = items[1] or ''
self.__flags[items[0].lstrip(prefix).lower() if del_pre \
else items[0].lower()] = items[1] or ''

else:
self.__filtered_input_str += ' ' + i
Expand All @@ -165,7 +165,7 @@ async def send_as_file(self,
text: str,
filename: str = "output.txt",
caption: str = '',
log: bool = False,
log: Union[bool, str] = False,
delete_message: bool = True) -> 'Message':
"""
You can send large outputs as file
Expand Down Expand Up @@ -219,7 +219,7 @@ async def send_as_file(self,
async def reply(self,
text: str,
del_in: int = -1,
log: bool = False,
log: Union[bool, str] = False,
quote: Optional[bool] = None,
parse_mode: Union[str, object] = object,
disable_web_page_preview: Optional[bool] = None,
Expand Down Expand Up @@ -297,7 +297,7 @@ async def reply(self,
async def edit(self,
text: str,
del_in: int = -1,
log: bool = False,
log: Union[bool, str] = False,
sudo: bool = True,
parse_mode: Union[str, object] = object,
disable_web_page_preview: Optional[bool] = None,
Expand Down Expand Up @@ -376,7 +376,7 @@ async def edit(self,
async def force_edit(self,
text: str,
del_in: int = -1,
log: bool = False,
log: Union[bool, str] = False,
parse_mode: Union[str, object] = object,
disable_web_page_preview: Optional[bool] = None,
reply_markup: InlineKeyboardMarkup = None,
Expand Down Expand Up @@ -432,7 +432,7 @@ async def force_edit(self,
async def err(self,
text: str,
del_in: int = -1,
log: bool = False,
log: Union[bool, str] = False,
sudo: bool = True,
parse_mode: Union[str, object] = object,
disable_web_page_preview: Optional[bool] = None,
Expand Down Expand Up @@ -481,7 +481,7 @@ async def err(self,
async def force_err(self,
text: str,
del_in: int = -1,
log: bool = False,
log: Union[bool, str] = False,
parse_mode: Union[str, object] = object,
disable_web_page_preview: Optional[bool] = None,
reply_markup: InlineKeyboardMarkup = None,
Expand Down Expand Up @@ -530,7 +530,7 @@ async def force_err(self,
async def try_to_edit(self,
text: str,
del_in: int = -1,
log: bool = False,
log: Union[bool, str] = False,
sudo: bool = True,
parse_mode: Union[str, object] = object,
disable_web_page_preview: Optional[bool] = None,
Expand Down Expand Up @@ -584,7 +584,7 @@ async def try_to_edit(self,
async def edit_or_send_as_file(self,
text: str,
del_in: int = -1,
log: bool = False,
log: Union[bool, str] = False,
sudo: bool = True,
parse_mode: Union[str, object] = object,
disable_web_page_preview: Optional[bool] = None,
Expand Down Expand Up @@ -640,7 +640,7 @@ async def edit_or_send_as_file(self,
async def reply_or_send_as_file(self,
text: str,
del_in: int = -1,
log: bool = False,
log: Union[bool, str] = False,
quote: Optional[bool] = None,
parse_mode: Union[str, object] = object,
disable_web_page_preview: Optional[bool] = None,
Expand Down Expand Up @@ -708,7 +708,7 @@ async def reply_or_send_as_file(self,
async def force_edit_or_send_as_file(self,
text: str,
del_in: int = -1,
log: bool = False,
log: Union[bool, str] = False,
parse_mode: Union[str, object] = object,
disable_web_page_preview: Optional[bool] = None,
reply_markup: InlineKeyboardMarkup = None,
Expand Down
3 changes: 0 additions & 3 deletions userge/plugins/misc/download.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,12 @@ async def down_load_media(message: Message):
try:
downloader = SmartDL(url, download_file_path, progress_bar=False)
downloader.start(blocking=False)
c_time = time.time()

while not downloader.isFinished():
if message.process_is_canceled:
downloader.stop()
raise Exception('Process Canceled!')

diff = time.time() - c_time

total_length = downloader.filesize if downloader.filesize else 0
downloaded = downloader.get_dl_size()
percentage = downloader.get_progress() * 100
Expand Down
102 changes: 93 additions & 9 deletions userge/plugins/misc/gdrive.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import os
import io
import re
import time
import math
import pickle
Expand All @@ -20,14 +21,16 @@
from functools import wraps
from httplib2 import Http

from pySmartDL import SmartDL
from googleapiclient.discovery import build
from googleapiclient.errors import HttpError
from googleapiclient.http import MediaFileUpload, MediaIoBaseDownload
from oauth2client.client import OAuth2WebServerFlow
from oauth2client.client import HttpAccessTokenRefreshError, FlowExchangeError

from userge import userge, Message, Config, get_collection
from userge.utils import humanbytes, time_formatter
from userge.utils import progress, humanbytes, time_formatter
from userge.utils.exceptions import ProcessCanceled

CREDS: object = None
AUTH_FLOW: object = None
Expand All @@ -45,12 +48,6 @@
GDRIVE_COLLECTION = get_collection("gdrive")


class ProcessCanceled(Exception):
"""
Custom Exception to terminate uploading / downloading or copying thread.
"""


class DBase:
"""
Database Class for GDrive.
Expand Down Expand Up @@ -709,6 +706,7 @@ def _del_perms(self, file_id: str) -> str:


def creds_dec(func):
"""decorator for check CREDS"""

@wraps(func)
async def wrapper(self):
Expand Down Expand Up @@ -900,7 +898,93 @@ async def upload(self) -> None:
Upload file/folder to GDrive.
"""

upload_file_name = self.__message.input_str
if not os.path.isdir(Config.DOWN_PATH):
os.mkdir(Config.DOWN_PATH)

replied = self.__message.reply_to_message
is_url = re.search(
r"(?:(?:https?|ftp):\/\/)?[\w/\-?=%.]+\.[\w/\-?=%.]+", self.__message.input_str)
dl_loc = None

if replied and replied.media:
await self.__message.edit("`Downloading From TG...`")
c_time = time.time()

dl_loc = await userge.download_media(
message=replied,
file_name=Config.DOWN_PATH,
progress=progress,
progress_args=(
"trying to download", userge, self.__message, c_time
)
)

if self.__message.process_is_canceled:
await self.__message.edit("`Process Canceled!`", del_in=5)
return

else:
dl_loc = os.path.join(Config.DOWN_PATH, os.path.basename(dl_loc))

elif is_url:
await self.__message.edit("`Downloading From URL...`")

is_url = is_url[0]
file_name = os.path.basename(is_url)
dl_loc = os.path.join(Config.DOWN_PATH, file_name)

try:
downloader = SmartDL(is_url, dl_loc, progress_bar=False)
downloader.start(blocking=False)

while not downloader.isFinished():
if self.__message.process_is_canceled:
downloader.stop()
await self.__message.edit("`Process Canceled!`", del_in=5)
return

total_length = downloader.filesize if downloader.filesize else 0
downloaded = downloader.get_dl_size()
percentage = downloader.get_progress() * 100
speed = downloader.get_speed(human=True)
estimated_total_time = downloader.get_eta(human=True)

progress_str = \
"__{}__\n" + \
"```[{}{}]```\n" + \
"**Progress** : `{}%`\n" + \
"**URL** : `{}`\n" + \
"**FILENAME** : `{}`\n" + \
"**Completed** : `{}`\n" + \
"**Total** : `{}`\n" + \
"**Speed** : `{}`\n" + \
"**ETA** : `{}`"

progress_str = progress_str.format(
"trying to download",
''.join(["█" for i in range(math.floor(percentage / 5))]),
''.join(["░" for i in range(20 - math.floor(percentage / 5))]),
round(percentage, 2),
is_url,
file_name,
humanbytes(downloaded),
humanbytes(total_length),
speed,
estimated_total_time)

await self.__message.try_to_edit(
text=progress_str, disable_web_page_preview=True)

await asyncio.sleep(3)

except Exception as e:
if os.path.exists(dl_loc):
os.remove(dl_loc)

await self.__message.err(e)
return

upload_file_name = dl_loc if dl_loc else self.__message.input_str

if not os.path.exists(upload_file_name):
await self.__message.err("invalid file path provided?")
Expand Down Expand Up @@ -1248,7 +1332,7 @@ async def gls_(message: Message):
'header': "Upload files to GDrive",
'description': "set destination by setting parent_id, "
"use `.gset` to set parent_id (root path).",
'usage': ".gup [file | folder path]"})
'usage': ".gup [file / folder path | direct link | reply to telegram file]"})
async def gup_(message: Message):
"""gup"""
await Worker(message).upload()
Expand Down
7 changes: 1 addition & 6 deletions userge/plugins/misc/zip.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,12 @@

from userge import userge, Message, Config
from userge.utils import humanbytes
from userge.utils.exceptions import ProcessCanceled

LOGGER = userge.getLogger(__name__)
COUNTER_LOCK = Lock()


class ProcessCanceled(Exception):
"""
Custom Exception to terminate zipping / unzipping thread.
"""


class Zip:
"""
Class for ZIP / UNZIP (files / folders).
Expand Down
15 changes: 12 additions & 3 deletions userge/plugins/tools/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
from userge.utils import get_import_path
from userge.plugins import ROOT

TMP_PATH = "userge/plugins/temp/"


@userge.on_cmd('load', about={
'header': "Load Userge plugin",
Expand All @@ -24,14 +26,21 @@ async def load_cmd_handler(message: Message):
file_ = replied.document

if file_.file_name.endswith('.py') and file_.file_size < 2 ** 20:
path = await replied.download(file_name="userge/plugins/temp/")
plugin = get_import_path(ROOT, path)
if not os.path.isdir(TMP_PATH):
os.makedirs(TMP_PATH)

t_path = os.path.join(TMP_PATH, file_.file_name)
if os.path.isfile(t_path):
os.remove(t_path)

await replied.download(file_name=t_path)
plugin = get_import_path(ROOT, t_path)

try:
userge.load_plugin(plugin)

except (ImportError, SyntaxError) as i_e:
os.remove(path)
os.remove(t_path)
await message.err(i_e)

else:
Expand Down
4 changes: 2 additions & 2 deletions userge/plugins/utils/direct_links.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,10 +158,10 @@ def zippy_share(url: str) -> str:
script.text).group('url')
math = re.search(r'= (?P<url>\".+\" \+ (?P<math>\(.+\)) .+);',
script.text).group('math')
dl_url = url_raw.replace(math, f"\"{eval(math)}\"")
dl_url = url_raw.replace(math, '"' + str(eval(math)) + '"')
break

dl_url = f"{base_url}{eval(dl_url)}"
dl_url = base_url + eval(dl_url)
name = urllib.parse.unquote(dl_url.split('/')[-1])
reply += f'[{name}]({dl_url})\n'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,7 @@
# All rights reserved.


# tmp path to save loaded plugins
class ProcessCanceled(Exception):
"""
Custom Exception to terminate threads.
"""
Loading

0 comments on commit aa20485

Please sign in to comment.