Skip to content

Commit

Permalink
Auto-format code [skip actions]
Browse files Browse the repository at this point in the history
  • Loading branch information
5hojib committed Dec 7, 2024
1 parent 4679d49 commit 11ace94
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 140 deletions.
7 changes: 2 additions & 5 deletions bot/helper/ext_utils/links_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,10 @@ def is_gdrive_id(id_: str):
),
)


def is_mega_link(url):
return "mega.nz" in url or "mega.co.nz" in url


def get_mega_link_type(url: str):
return (
"folder"
if "folder" in url or "/#F!" in url
else "file"
)
return "folder" if "folder" in url or "/#F!" in url else "file"
137 changes: 63 additions & 74 deletions bot/helper/listeners/mega_listener.py
Original file line number Diff line number Diff line change
@@ -1,71 +1,64 @@
from threading import Event

from mega import (
MegaApi,
MegaError,
MegaListener,
MegaRequest,
MegaTransfer
)
from mega import MegaApi, MegaError, MegaListener, MegaRequest, MegaTransfer

from bot import LOGGER
from bot.helper.ext_utils.bot_utils import (
async_to_sync,
sync_to_async
)
from bot.helper.ext_utils.bot_utils import async_to_sync, sync_to_async


class AsyncExecutor:
def __init__(self):
self.continue_event = Event()

def do(
self,
function,
args
):
self,
function,
args,
):
self.continue_event.clear()
function(*args)
self.continue_event.wait()


async def mega_login(
executor,
api,
MAIL,
PASS
):
executor,
api,
MAIL,
PASS,
):
if MAIL and PASS:
await sync_to_async(
executor.do,
api.login,
(
MAIL,
PASS
)
PASS,
),
)


async def mega_logout(
executor,
api,
folder_api=None
):
executor,
api,
folder_api=None,
):
await sync_to_async(
executor.do,
api.logout,
()
)
executor.do,
api.logout,
(),
)
if folder_api:
await sync_to_async(
executor.do,
folder_api.logout,
()
(),
)


class MegaAppListener(MegaListener):
_NO_EVENT_ON = (
MegaRequest.TYPE_LOGIN,
MegaRequest.TYPE_FETCH_NODES
MegaRequest.TYPE_FETCH_NODES,
)

def __init__(self, continue_event: Event, listener):
Expand All @@ -89,11 +82,11 @@ def downloaded_bytes(self):
return self._bytes_transferred

def onRequestFinish(
self,
api,
request,
error
):
self,
api,
request,
error,
):
if str(error).lower() != "no error":
self.error = error.copy()
if str(self.error).casefold() != "not found":
Expand All @@ -114,84 +107,80 @@ def onRequestFinish(
self._name = self.node.getName()
LOGGER.info(f"Node Name: {self.node.getName()}")

if (
request_type not in self._NO_EVENT_ON
or (
self.node
and "cloud drive" not in self._name.lower()
)
if request_type not in self._NO_EVENT_ON or (
self.node and "cloud drive" not in self._name.lower()
):
self.continue_event.set()

def onRequestTemporaryError(
self,
api,
request,
error: MegaError
):
self,
api,
request,
error: MegaError,
):
LOGGER.error(f"Mega Request error in {error}")
if not self.is_cancelled:
self.is_cancelled = True
async_to_sync(
self.listener.on_download_error,
f"RequestTempError: {error.toString()}"
f"RequestTempError: {error.toString()}",
)
self.error = error.toString()
self.continue_event.set()

def onTransferUpdate(
self,
api: MegaApi,
transfer: MegaTransfer
):
self,
api: MegaApi,
transfer: MegaTransfer,
):
if self.is_cancelled:
api.cancelTransfer(
transfer,
None
None,
)
self.continue_event.set()
return
self._speed = transfer.getSpeed()
self._bytes_transferred = transfer.getTransferredBytes()

def onTransferFinish(
self,
api: MegaApi,
transfer: MegaTransfer,
error
):
self,
api: MegaApi,
transfer: MegaTransfer,
error,
):
try:
if self.is_cancelled:
self.continue_event.set()
elif (
transfer.isFinished()
and (
transfer.isFolderTransfer() or
transfer.getFileName() == self._name
)
elif transfer.isFinished() and (
transfer.isFolderTransfer() or transfer.getFileName() == self._name
):
async_to_sync(self.listener.on_download_complete)
self.continue_event.set()
except Exception as e:
LOGGER.error(e)

def onTransferTemporaryError(
self,
api,
transfer,
error
):
LOGGER.error(f"Mega download error in file {transfer.getFileName()}: {error}")
self,
api,
transfer,
error,
):
LOGGER.error(
f"Mega download error in file {transfer.getFileName()}: {error}"
)
if transfer.getState() in [
1,
4
4,
]:
return
self.error = f"TransferTempError: {error.toString()} ({transfer.getFileName()})"
self.error = (
f"TransferTempError: {error.toString()} ({transfer.getFileName()})"
)
if not self.is_cancelled:
self.is_cancelled = True
self.continue_event.set()

async def cancel_task(self):
self.is_cancelled = True
await self.listener.on_download_error("Download Canceled by user")
await self.listener.on_download_error("Download Canceled by user")
Loading

0 comments on commit 11ace94

Please sign in to comment.