コード例 #1
0
ファイル: Player.py プロジェクト: jositux/guicavane
    def get_hosts(self):
        """ Returns a list with the avaliable downloaders for the file. """

        result = []
        avaliable_downloaders = Downloaders.get_avaliable()

        hosts = self.file_object.file_hosts
        hosts["dummy"] = ""

        for host in hosts:
            if host in avaliable_downloaders:
                result.append(Downloaders.get(host, self.gui_manager, hosts[host]))

        return result
コード例 #2
0
class Player(object):
    """
    Class that takes care of the downloading and correctly
    playing the file.
    """
    def __init__(self,
                 gui_manager,
                 file_object,
                 file_path=None,
                 download_only=False,
                 choose_host=False):
        self.gui_manager = gui_manager
        self.config = Config.get()

        self.file_object = file_object
        self.download_only = download_only
        self.choose_host = choose_host

        self.selected_quality = None

        self._speed_list = []
        self._last_downloaded_size = 0
        self.speed = 0

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

        self.file_path = os.path.join(self.file_path, self.get_filename())

        # Builder for the hosts selection
        self.hosts_builder = gtk.Builder()
        self.hosts_builder.add_from_file(HOSTS_GUI_FILE)
        self.hosts_builder.connect_signals(self)

        glade_objects = [
            "hosts_window", "hosts_icon_view", "hosts_icon_view_model"
        ]

        for glade_object in glade_objects:
            setattr(self, glade_object,
                    self.hosts_builder.get_object(glade_object))

        self.gui_manager.background_task(
            self.get_hosts,
            self.display_hosts,
            status_message=gettext("Fetching hosts..."),
            unfreeze=False)

    def get_hosts(self):
        """ Returns a dict with the avaliable downloaders for the file. """

        result = {}
        avaliable_downloaders = Downloaders.get_avaliable()

        hosts = self.file_object.file_hosts

        hosts["dummy"] = ""

        for host in hosts:
            host = host.lower()
            if host in avaliable_downloaders:
                result[host] = hosts[host]

        return result

    def display_hosts(self, (is_error, result)):
        """ Shows up the hosts selecting window. """

        gobject.idle_add(self.gui_manager.set_status_message, "")

        if is_error:
            self.gui_manager.report_error(gettext("Error fetching hosts: %s") % \
                                          result)
            gobject.idle_add(self.gui_manager.progress_box.hide)
            return

        if not result:
            self.gui_manager.report_error(gettext("No host found"))
            self.gui_manager.unfreeze()
            return

        automatic_start = self.config.get_key("automatic_start")

        if automatic_start and len(result) == 1 and not self.choose_host:
            host, qualities = result.items()[0]

            if len(qualities) == 1:
                gobject.idle_add(
                    self.gui_manager.set_status_message,
                    gettext("Only one host found, starting download..."))

                url = qualities.items()[0][1]
                self.downloader = Downloaders.get(host, self.gui_manager, url)
                self.downloader.process_url(self.play, self.file_path)
                return

        for host in result:
            for quality in result[host]:
                downloader = Downloaders.get(host, self.gui_manager,
                                             result[host][quality])
                icon = downloader.icon
                name = "%s (%s)" % (downloader.name, quality)

                self.hosts_icon_view_model.append([icon, name, downloader])

        self.hosts_window.show_all()