示例#1
0
    def download_file(self, episode):
        """
        Given an episode, downloads the subtitles then starts downloading
        the file and starts the player.
        """

        self.file_viewer.set_sensitive(False)

        link = self.pycavane.get_direct_links(episode, host="megaupload")
        link = link[1]

        filename = self.cachedir + os.sep + link.rsplit('/', 1)[1]
        subtitle = self.pycavane.get_subtitle(episode, filename=filename)

        megafile = MegaFile(link, self.cachedir)
        megafile.start()
        print "Waiting 45"
        time.sleep(45 + 5)  # Megaupload's 45 plus some extra space
        print "Done waiting"
        filename = megafile.cache_file
        os.system("vlc %s" % filename)
        megafile.released = True
示例#2
0
    def play(self, to_download, is_movie=False,
             file_path=None, download_only=False):
        """
        Starts the playing of `to_download`.
        """

        link = self.pycavane.get_direct_links(to_download, host="megaupload",
                                              movie=is_movie)

        if link:
            link = link[1]
        else:
            raise Exception("Not download source found")

        if file_path:
            cache_dir = file_path
        else:
            cache_dir = self.config.get_key("cache_dir")

        if is_movie:
            title = to_download[1]
        else:
            title = to_download[2]

        # Check login status
        if not self.account.verified:
            username = self.config.get_key("mega_user")
            password = self.config.get_key("mega_pass")

            if password:
                self.account.login(username, password)

        # Create the megaupload instance
        filename = self.get_filename(to_download, is_movie)
        megafile = MegaFile(link, cache_dir, self.account,
                            self.on_megaupload_error, filename)
        filepath = megafile.cache_file

        # Download the subtitles
        self.download_subtitles(to_download, filepath, is_movie)

        # Start the file download
        megafile.start()

        # Waiting megaupload link
        for i in xrange(self.account.wait, 1, -1):
            loading_dots = "." * (3 - i % 4)
            self.set_status_message("Please wait %d seconds%s" % \
                                (i, loading_dots))
            time.sleep(1)

        # Wait until the file exists
        file_exists = False
        while not file_exists:
            if self.megaupload_error:
                raise Exception("Download error: time exceeded")
            else:
                self.set_status_message("A few seconds left...")
                file_exists = os.path.exists(filepath)
                time.sleep(1)

        if download_only:
            self.set_status_message("Downloading: %s" % title)
        else:
            self.set_status_message("Now playing: %s" % title)

        # Show the progress bar
        gobject.idle_add(self.show_progress)

        cache_on_movies = self.config.get_key("cached_percentage_on_movies")
        cached_percentage = self.config.get_key("cached_percentage")
        cached_percentage = cached_percentage / 100.0

        if is_movie and not cache_on_movies:
            cached_percentage = 0.008

        player_location = self.config.get_key("player_location")
        player_args = self.config.get_key("player_arguments").split()

        size = megafile.size * 1024.0  # In KB
        stop = False
        running = False
        speed_list = []
        last_downloaded = 0
        while not stop:
            time.sleep(1)

            downloaded = megafile.downloaded_size * 1024.0  # In KB
            speed_list.append(downloaded - last_downloaded)
            offset = len(speed_list) - 30 if len(speed_list) > 30 else 0
            speed_list = speed_list[offset:]

            speed_avarage = sum(speed_list) / len(speed_list)
            last_downloaded = downloaded

            fraction = downloaded / size

            if download_only:
                if round(fraction, 2) >= 1:
                    stop = True
            else:
                if not running and fraction > cached_percentage:
                    player_cmd = [player_location] + player_args + [filepath]
                    process = subprocess.Popen(player_cmd)
                    running = True

                if running and process.poll() != None:
                    stop = True

            if speed_avarage <= 0:
                remaining_time = 0
            else:
                remaining_time = ((size - downloaded) / speed_avarage) / 60

            if remaining_time >= 1:  # if it's more than a minute
                if remaining_time == 1:
                    remaining_message = "%d minute left" % remaining_time
                else:
                    remaining_message = "%d minutes left" % remaining_time
            else:
                if (remaining_time * 60) > 10:
                    remaining_message = "%d seconds left" % (remaining_time * 60)
                elif remaining_time != 0:
                    remaining_message = "a few seconds left"
                else:
                    remaining_message = ""

            if fraction < 1:
                gobject.idle_add(self.gui.progress_label.set_text,
                    "%.2fKB/s - %s" % (speed_avarage, remaining_message))
            else:
                gobject.idle_add(self.gui.progress_label.set_text, "")

            gobject.idle_add(self.gui.progress.set_fraction, fraction)
            gobject.idle_add(self.gui.progress.set_text,
                             "%.2f%%" % (fraction * 100))

        gobject.idle_add(self.gui.progress_box.hide)

        # Automatic mark
        if self.config.get_key("automatic_marks"):
            self.gui.mark_selected()

        megafile.released = True