Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tiyx #5

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 3 additions & 10 deletions bot/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,7 @@ def mktable():
parent_id = getConfig('GDRIVE_FOLDER_ID')
telegraph_token = getConfig('TELEGRAPH_TOKEN')
DOWNLOAD_DIR = getConfig('DOWNLOAD_DIR')
if DOWNLOAD_DIR[-1] != '/' or DOWNLOAD_DIR[-1] != '\\':
DOWNLOAD_DIR = DOWNLOAD_DIR + '/'
DOWNLOAD_DIR = DOWNLOAD_DIR + '/'
DOWNLOAD_STATUS_UPDATE_INTERVAL = int(getConfig('DOWNLOAD_STATUS_UPDATE_INTERVAL'))
OWNER_ID = int(getConfig('OWNER_ID'))
AUTO_DELETE_MESSAGE_DURATION = int(getConfig('AUTO_DELETE_MESSAGE_DURATION'))
Expand Down Expand Up @@ -139,19 +138,13 @@ def mktable():
INDEX_URL = None
try:
IS_TEAM_DRIVE = getConfig('IS_TEAM_DRIVE')
if IS_TEAM_DRIVE.lower() == 'true':
IS_TEAM_DRIVE = True
else:
IS_TEAM_DRIVE = False
IS_TEAM_DRIVE = IS_TEAM_DRIVE.lower() == 'true'
except KeyError:
IS_TEAM_DRIVE = False

try:
USE_SERVICE_ACCOUNTS = getConfig('USE_SERVICE_ACCOUNTS')
if USE_SERVICE_ACCOUNTS.lower() == 'true':
USE_SERVICE_ACCOUNTS = True
else:
USE_SERVICE_ACCOUNTS = False
USE_SERVICE_ACCOUNTS = USE_SERVICE_ACCOUNTS.lower() == 'true'
except KeyError:
USE_SERVICE_ACCOUNTS = False

Expand Down
32 changes: 17 additions & 15 deletions bot/helper/ext_utils/bot_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,20 +63,23 @@ def getDownloadByGid(gid):
with download_dict_lock:
for dl in download_dict.values():
status = dl.status()
if status != MirrorStatus.STATUS_UPLOADING and status != MirrorStatus.STATUS_ARCHIVING\
and status != MirrorStatus.STATUS_EXTRACTING:
if dl.gid() == gid:
return dl
if (
status
not in [
MirrorStatus.STATUS_UPLOADING,
MirrorStatus.STATUS_ARCHIVING,
MirrorStatus.STATUS_EXTRACTING,
]
and dl.gid() == gid
):
return dl
return None


def get_progress_bar_string(status):
completed = status.processed_bytes() / 8
total = status.size_raw() / 8
if total == 0:
p = 0
else:
p = round(completed * 100 / total)
p = 0 if total == 0 else round(completed * 100 / total)
p = min(max(p, 0), 100)
cFull = p // 8
cPart = p % 8 - 1
Expand All @@ -94,7 +97,10 @@ def get_readable_message():
for download in list(download_dict.values()):
msg += f"<b>FileName :</b> <i>{download.name()}</i> \n\n<b>Status : </b> "
msg += download.status()
if download.status() != MirrorStatus.STATUS_ARCHIVING and download.status() != MirrorStatus.STATUS_EXTRACTING:
if download.status() not in [
MirrorStatus.STATUS_ARCHIVING,
MirrorStatus.STATUS_EXTRACTING,
]:
msg += f"\n\n<code>{get_progress_bar_string(download)} {download.progress()}</code>" \
f"\n\n<b>Progress :</b> {get_readable_file_size(download.processed_bytes())}" \
f"\n\n<b>Size :</b> {download.size()}" \
Expand Down Expand Up @@ -129,16 +135,12 @@ def get_readable_time(seconds: int) -> str:

def is_url(url: str):
url = re.findall(URL_REGEX, url)
if url:
return True
return False
return bool(url)


def is_magnet(url: str):
magnet = re.findall(MAGNET_REGEX, url)
if magnet:
return True
return False
return bool(magnet)


def is_mega_link(url: str):
Expand Down
80 changes: 38 additions & 42 deletions bot/helper/ext_utils/db_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,59 +20,55 @@ def disconnect(self):

def db_auth(self,chat_id: int):
self.connect()
if self.err :
if self.err:
return "There's some error check log for details"
else:
sql = 'INSERT INTO users VALUES ({});'.format(chat_id)
self.cur.execute(sql)
self.conn.commit()
self.disconnect()
AUTHORIZED_CHATS.add(chat_id)
return 'Authorized successfully'
sql = 'INSERT INTO users VALUES ({});'.format(chat_id)
self.cur.execute(sql)
self.conn.commit()
self.disconnect()
AUTHORIZED_CHATS.add(chat_id)
return 'Authorized successfully'

def db_unauth(self,chat_id: int):
self.connect()
if self.err :
if self.err:
return "There's some error check log for details"
else:
sql = 'DELETE from users where uid = {};'.format(chat_id)
self.cur.execute(sql)
self.conn.commit()
self.disconnect()
AUTHORIZED_CHATS.remove(chat_id)
if chat_id in SUDO_USERS:
SUDO_USERS.remove(chat_id)
return 'Unauthorized successfully'
sql = 'DELETE from users where uid = {};'.format(chat_id)
self.cur.execute(sql)
self.conn.commit()
self.disconnect()
AUTHORIZED_CHATS.remove(chat_id)
if chat_id in SUDO_USERS:
SUDO_USERS.remove(chat_id)
return 'Unauthorized successfully'

def db_addsudo(self,chat_id: int):
self.connect()
if self.err :
if self.err:
return "There's some error check log for details"
if chat_id in AUTHORIZED_CHATS:
sql = 'UPDATE users SET sudo = TRUE where uid = {};'.format(chat_id)
self.cur.execute(sql)
self.conn.commit()
self.disconnect()
SUDO_USERS.add(chat_id)
return 'Successfully promoted as sudo'
else:
if chat_id in AUTHORIZED_CHATS:
sql = 'UPDATE users SET sudo = TRUE where uid = {};'.format(chat_id)
self.cur.execute(sql)
self.conn.commit()
self.disconnect()
SUDO_USERS.add(chat_id)
return 'Successfully promoted as sudo'
else:
sql = 'INSERT INTO users VALUES ({},TRUE);'.format(chat_id)
self.cur.execute(sql)
self.conn.commit()
self.disconnect()
AUTHORIZED_CHATS.add(chat_id)
SUDO_USERS.add(chat_id)
return 'Successfully Authorized and promoted as sudo'
sql = 'INSERT INTO users VALUES ({},TRUE);'.format(chat_id)
self.cur.execute(sql)
self.conn.commit()
self.disconnect()
AUTHORIZED_CHATS.add(chat_id)
SUDO_USERS.add(chat_id)
return 'Successfully Authorized and promoted as sudo'

def db_rmsudo(self,chat_id: int):
self.connect()
if self.err :
if self.err:
return "There's some error check log for details"
else:
sql = 'UPDATE users SET sudo = FALSE where uid = {};'.format(chat_id)
self.cur.execute(sql)
self.conn.commit()
self.disconnect()
SUDO_USERS.remove(chat_id)
return 'Successfully removed from Sudo'
sql = 'UPDATE users SET sudo = FALSE where uid = {};'.format(chat_id)
self.cur.execute(sql)
self.conn.commit()
self.disconnect()
SUDO_USERS.remove(chat_id)
return 'Successfully removed from Sudo'
2 changes: 1 addition & 1 deletion bot/helper/ext_utils/fs_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,5 +137,5 @@ def get_base_name(orig_path: str):
def get_mime_type(file_path):
mime = magic.Magic(mime=True)
mime_type = mime.from_file(file_path)
mime_type = mime_type if mime_type else "text/plain"
mime_type = mime_type or "text/plain"
return mime_type
3 changes: 1 addition & 2 deletions bot/helper/mirror_utils/download_utils/aria2_download.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ def __onDownloadComplete(self, api: API, gid):
download_dict[dl.uid()].is_torrent = True
update_all_messages()
LOGGER.info(f'Changed gid from {gid} to {new_gid}')
else:
if dl: threading.Thread(target=dl.getListener().onDownloadComplete).start()
elif dl: threading.Thread(target=dl.getListener().onDownloadComplete).start()

@new_thread
def __onDownloadPause(self, api, gid):
Expand Down
15 changes: 5 additions & 10 deletions bot/helper/mirror_utils/download_utils/direct_link_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,10 @@ def yandex_disk(url: str) -> str:
try:
link = re.findall(r'\bhttps?://.*yadi\.sk\S+', url)[0]
except IndexError:
reply = "`No Yandex.Disk links found`\n"
return reply
return "`No Yandex.Disk links found`\n"
api = 'https://cloud-api.yandex.net/v1/disk/public/resources/download?public_key={}'
try:
dl_url = requests.get(api.format(link)).json()['href']
return dl_url
return requests.get(api.format(link)).json()['href']
except KeyError:
raise DirectDownloadLinkException("`Error: File not found / Download limit reached`\n")

Expand All @@ -97,8 +95,7 @@ def cm_ru(url: str) -> str:
data = json.loads(result)
except json.decoder.JSONDecodeError:
raise DirectDownloadLinkException("`Error: Can't extract the link`\n")
dl_url = data['download']
return dl_url
return data['download']


def mediafire(url: str) -> str:
Expand All @@ -109,8 +106,7 @@ def mediafire(url: str) -> str:
raise DirectDownloadLinkException("`No MediaFire links found`\n")
page = BeautifulSoup(requests.get(link).content, 'lxml')
info = page.find('a', {'aria-label': 'Download file'})
dl_url = info.get('href')
return dl_url
return info.get('href')


def osdn(url: str) -> str:
Expand Down Expand Up @@ -140,8 +136,7 @@ def github(url: str) -> str:
raise DirectDownloadLinkException("`No GitHub Releases links found`\n")
download = requests.get(url, stream=True, allow_redirects=False)
try:
dl_url = download.headers["location"]
return dl_url
return download.headers["location"]
except KeyError:
raise DirectDownloadLinkException("`Error: Can't extract the link`\n")

Expand Down
5 changes: 2 additions & 3 deletions bot/helper/mirror_utils/download_utils/telegram_downloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,8 @@ def __download(self, message, path):
progress=self.__onDownloadProgress, file_name=path)
if download is not None:
self.__onDownloadComplete()
else:
if not self.__is_cancelled:
self.__onDownloadError('Internal error occurred')
elif not self.__is_cancelled:
self.__onDownloadError('Internal error occurred')

def add_download(self, message, path):
_message = self.__user_bot.get_messages(message.chat.id, message.message_id)
Expand Down
9 changes: 4 additions & 5 deletions bot/helper/mirror_utils/status_utils/aria_download_status.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,13 @@ def eta(self):
def status(self):
download = self.aria_download()
if download.is_waiting:
status = MirrorStatus.STATUS_WAITING
return MirrorStatus.STATUS_WAITING
elif download.is_paused:
status = MirrorStatus.STATUS_CANCELLED
return MirrorStatus.STATUS_CANCELLED
elif download.has_failed:
status = MirrorStatus.STATUS_FAILED
return MirrorStatus.STATUS_FAILED
else:
status = MirrorStatus.STATUS_DOWNLOADING
return status
return MirrorStatus.STATUS_DOWNLOADING

def aria_download(self):
self.__update()
Expand Down
Loading