Ejemplo n.º 1
0
 def __init__(self, listener):
     super().__init__()
     self.__listener = listener
     self.__name = ""
     self.__gid = ""
     self.__resource_lock = threading.Lock()
     self.__mega_client = MegaSdkRestClient("http://localhost:6090")
     self.__periodic = None
     self.__downloaded_bytes = 0
     self.__progress = 0
     self.__size = 0
Ejemplo n.º 2
0
async def init_mega_client(holder=[], return_pr=False):
    if len(holder) == 0:
        MEGA_API = get_val("MEGA_API")
        MEGA_UNAME = get_val("MEGA_UNAME")
        MEGA_PASS = get_val("MEGA_PASS")

        if MEGA_API is None:
            return None

        pr = Popen(["megasdkrest", "--apikey", MEGA_API, "--port", "8200"])
        await asyncio.sleep(5)
        mega_client = MegaSdkRestClient("http://localhost:8200")

        anon = False
        if MEGA_UNAME is None:
            anon = True
            torlog.warn("Mega Username not specified")

        if MEGA_PASS is None:
            anon = True
            torlog.warn("Mega Password not specified")

        if anon:
            torlog.info("Mega running in Anon mode.")

        else:
            torlog.info("Mega running in Logged in mode.")

            try:
                mega_client.login(MEGA_UNAME, MEGA_PASS)
            except:
                torlog.error("Mega login failed.")
                torlog.info("Started in anon mode.")
        holder.append(mega_client)
        holder.append(pr)

    if return_pr:
        return holder[1]
    else:
        return holder[0]
Ejemplo n.º 3
0
    UPTOBOX_TOKEN = getConfig('UPTOBOX_TOKEN')
except KeyError:
    logging.info('UPTOBOX_TOKEN not provided!')
    UPTOBOX_TOKEN = None

try:
    MEGA_KEY = getConfig('MEGA_KEY')

except KeyError:
    MEGA_KEY = None
    LOGGER.info('MEGA API KEY NOT AVAILABLE')
if MEGA_KEY is not None:
    # Start megasdkrest binary
    subprocess.Popen(["megasdkrest", "--apikey", MEGA_KEY])
    time.sleep(3)  # Wait for the mega server to start listening
    mega_client = MegaSdkRestClient('http://localhost:6090')
    try:
        MEGA_USERNAME = getConfig('MEGA_USERNAME')
        MEGA_PASSWORD = getConfig('MEGA_PASSWORD')
        if len(MEGA_USERNAME) > 0 and len(MEGA_PASSWORD) > 0:
            try:
                mega_client.login(MEGA_USERNAME, MEGA_PASSWORD)
            except mega_err.MegaSdkRestClientException as e:
                logging.error(e.message['message'])
                exit(0)
        else:
            LOGGER.info(
                "Mega API KEY provided but credentials not provided. Starting mega in anonymous mode!"
            )
            MEGA_USERNAME = None
            MEGA_PASSWORD = None
Ejemplo n.º 4
0
class MegaDownloader:
    POLLING_INTERVAL = 2

    def __init__(self, listener):
        super().__init__()
        self.__listener = listener
        self.__name = ""
        self.__gid = ""
        self.__resource_lock = threading.Lock()
        self.__mega_client = MegaSdkRestClient("http://localhost:6090")
        self.__periodic = None
        self.__downloaded_bytes = 0
        self.__progress = 0
        self.__size = 0

    @property
    def progress(self):
        with self.__resource_lock:
            return self.__progress

    @property
    def downloaded_bytes(self):
        with self.__resource_lock:
            return self.__downloaded_bytes

    @property
    def size(self):
        with self.__resource_lock:
            return self.__size

    @property
    def gid(self):
        with self.__resource_lock:
            return self.__gid

    @property
    def name(self):
        with self.__resource_lock:
            return self.__name

    @property
    def download_speed(self):
        if self.gid is not None:
            return self.__mega_client.getDownloadInfo(self.gid)["speed"]

    def __onDownloadStart(self, name, size, gid):
        self.__periodic = setInterval(self.POLLING_INTERVAL, self.__onInterval)
        with download_dict_lock:
            download_dict[self.__listener.uid] = MegaDownloadStatus(
                self, self.__listener)
        with self.__resource_lock:
            self.__name = name
            self.__size = size
            self.__gid = gid
        self.__listener.onDownloadStarted()

    def __onInterval(self):
        dlInfo = self.__mega_client.getDownloadInfo(self.gid)
        if (dlInfo["state"] in [
                constants.State.TYPE_STATE_COMPLETED,
                constants.State.TYPE_STATE_CANCELED,
                constants.State.TYPE_STATE_FAILED,
        ] and self.__periodic is not None):
            self.__periodic.cancel()
        if dlInfo["state"] == constants.State.TYPE_STATE_COMPLETED:
            self.__onDownloadComplete()
            return
        if dlInfo["state"] == constants.State.TYPE_STATE_CANCELED:
            self.__onDownloadError("Cancelled by user")
            return
        if dlInfo["state"] == constants.State.TYPE_STATE_FAILED:
            self.__onDownloadError(dlInfo["error_string"])
            return
        self.__onDownloadProgress(dlInfo["completed_length"],
                                  dlInfo["total_length"])

    def __onDownloadProgress(self, current, total):
        with self.__resource_lock:
            self.__downloaded_bytes = current
            try:
                self.__progress = current / total * 100
            except ZeroDivisionError:
                self.__progress = 0

    def __onDownloadError(self, error):
        self.__listener.onDownloadError(error)

    def __onDownloadComplete(self):
        self.__listener.onDownloadComplete()

    def add_download(self, link, path):
        Path(path).mkdir(parents=True, exist_ok=True)
        dl = self.__mega_client.addDl(link, path)
        gid = dl["gid"]
        info = self.__mega_client.getDownloadInfo(gid)
        file_name = info["name"]
        file_size = info["total_length"]
        self.__onDownloadStart(file_name, file_size, gid)
        LOGGER.info(f"Started mega download with gid: {gid}")

    def cancel_download(self):
        LOGGER.info(f"Cancelling download on user request: {self.gid}")
        self.__mega_client.cancelDl(self.gid)