Exemplo n.º 1
0
class EmissionOverview(Screen, HelpableScreen):
    skin = """<screen name="EmissionOverview" title="Torrent Overview" position="75,135" size="565,330">
		<ePixmap position="0,0" size="140,40" pixmap="skin_default/buttons/red.png" transparent="1" alphatest="on" />
		<ePixmap position="140,0" size="140,40" pixmap="skin_default/buttons/green.png" transparent="1" alphatest="on" />
		<ePixmap position="280,0" size="140,40" pixmap="skin_default/buttons/yellow.png" transparent="1" alphatest="on" />
		<ePixmap position="420,0" size="140,40" pixmap="skin_default/buttons/blue.png" transparent="1" alphatest="on" />
		<widget source="key_red" render="Label" position="0,0" zPosition="1" size="140,40" valign="center" halign="center" font="Regular;21" transparent="1" foregroundColor="white" shadowColor="black" shadowOffset="-1,-1" />
		<widget source="key_green" render="Label" position="140,0" zPosition="1" size="140,40" valign="center" halign="center" font="Regular;21" transparent="1" foregroundColor="white" shadowColor="black" shadowOffset="-1,-1" />
		<widget source="key_yellow" render="Label" position="280,0" zPosition="1" size="140,40" valign="center" halign="center" font="Regular;21" transparent="1" foregroundColor="white" shadowColor="black" shadowOffset="-1,-1" />
		<widget source="key_blue" render="Label" position="420,0" zPosition="1" size="140,40" valign="center" halign="center" font="Regular;21" transparent="1" foregroundColor="white" shadowColor="black" shadowOffset="-1,-1" />
		<widget size="320,25" alphatest="on" position="5,45" zPosition="1" name="all_sel" pixmap="skin_default/epg_now.png" />
		<widget valign="center" transparent="1" size="108,22" backgroundColor="#25062748" position="5,47" zPosition="2" source="all_text" render="Label" halign="center" font="Regular;18" />
		<widget size="320,25" alphatest="on" position="5,45" zPosition="1" name="downloading_sel" pixmap="skin_default/epg_next.png" />
		<widget valign="center" transparent="1" size="108,22" backgroundColor="#25062748" position="111,47" zPosition="2" source="downloading_text" render="Label" halign="center" font="Regular;18" />
		<widget size="320,25" alphatest="on" position="5,45" zPosition="1" name="seeding_sel" pixmap="skin_default/epg_more.png" />
		<widget valign="center" transparent="1" size="108,22" backgroundColor="#25062748" position="212,47" zPosition="2" source="seeding_text" render="Label" halign="center" font="Regular;18" />
		<widget source="torrents" render="Label" size="240,22" position="320,47" halign="right" font="Regular;18" />
		<!--ePixmap size="550,230" alphatest="on" position="5,65" pixmap="skin_default/border_epg.png" /-->
		<widget source="list" render="Listbox" position="5,70" size="550,225" scrollbarMode="showAlways">
			<convert type="TemplatedMultiContent">
				{"template": [
						MultiContentEntryText(pos=(2,2), size=(555,22), text = 1, font = 0, flags = RT_HALIGN_LEFT|RT_VALIGN_CENTER),
						MultiContentEntryText(pos=(2,26), size=(555,18), text = 2, font = 1, flags = RT_HALIGN_LEFT|RT_VALIGN_CENTER),
						(eListboxPythonMultiContent.TYPE_PROGRESS, 0, 44, 537, 6, -3),
					],
				  "fonts": [gFont("Regular", 20),gFont("Regular", 16)],
				  "itemHeight": 51
				 }
			</convert>
		</widget>
		<widget source="upspeed" render="Label" size="150,20" position="5,300" halign="left" font="Regular;18" />
		<widget source="downspeed" render="Label" size="150,20" position="410,300" halign="right" font="Regular;18" />
	</screen>"""

    def __init__(self, session):
        Screen.__init__(self, session)
        HelpableScreen.__init__(self)

        try:
            self.transmission = Client(
                address=config.plugins.emission.hostname.value,
                port=config.plugins.emission.port.value,
                user=config.plugins.emission.username.value,
                password=config.plugins.emission.password.value)
        except TransmissionError as te:
            self.transmission = None

        self["SetupActions"] = HelpableActionMap(
            self, "SetupActions", {
                "ok": (self.ok, _("show details")),
                "cancel": (self.close, _("close")),
            })

        self["ColorActions"] = HelpableActionMap(
            self, "ColorActions", {
                "green": (self.bandwidth, _("open bandwidth settings")),
                "yellow": (self.prevlist, _("show previous list")),
                "blue": (self.nextlist, _("show next list")),
            })

        self["MenuActions"] = HelpableActionMap(self, "MenuActions", {
            "menu": (self.menu, _("open context menu")),
        })

        self["key_red"] = StaticText(_("Close"))
        self["key_green"] = StaticText(_("Bandwidth"))
        self["key_yellow"] = StaticText("")
        self["key_blue"] = StaticText("")

        self["all_text"] = StaticText(_("All"))
        self["downloading_text"] = StaticText(_("DL"))
        self["seeding_text"] = StaticText(_("UL"))
        self["upspeed"] = StaticText("")
        self["downspeed"] = StaticText("")
        self["torrents"] = StaticText("")

        self["all_sel"] = Pixmap()
        self["downloading_sel"] = Pixmap()
        self["seeding_sel"] = Pixmap()

        self['list'] = List([])

        self.list_type = config.plugins.emission.last_tab.value
        self.sort_type = config.plugins.emission.last_sort.value
        self.showHideSetTextMagic()

        self.timer = eTimer()
        self.timer_conn = self.timer.timeout.connect(self.updateList)
        self.timer.start(0, 1)

    def bandwidthCallback(self, ret=None):
        if self.transmission is not None and ret:
            try:
                self.transmission.set_session(**ret)
            except TransmissionError as te:
                self.session.open(
                    MessageBox,
                    _("Error communicating with transmission-daemon: %s.") %
                    (te),
                    type=MessageBox.TYPE_ERROR,
                    timeout=5)
        self.updateList()

    def menuCallback(self, ret=None):
        ret and ret[1]()

    def newDlCallback(self, ret=None):
        if self.transmission is not None and ret:
            try:
                res = self.transmission.add_url(ret)
            except TransmissionError as te:
                self.session.open(
                    MessageBox,
                    _("Error communicating with transmission-daemon: %s.") %
                    (te),
                    type=MessageBox.TYPE_ERROR,
                    timeout=5)
            else:
                if not res:
                    self.session.open(
                        MessageBox,
                        _("Torrent could not be scheduled not download!"),
                        type=MessageBox.TYPE_ERROR,
                        timeout=5)
        self.updateList()

    def newDl(self):
        self.timer.stop()
        self.session.openWithCallback(self.newDlCallback, TorrentLocationBox)

    def sortCallback(self, ret=None):
        if ret is not None:
            self.sort_type = config.plugins.emission.last_sort.value = ret[1]
            config.plugins.emission.last_sort.save()
        self.updateList()

    def sort(self):
        self.timer.stop()
        self.session.openWithCallback(self.sortCallback, ChoiceBox,
                                      _("Which sorting method do you prefer?"),
                                      [(_("by eta"), SORT_TYPE_TIME),
                                       (_("by progress"), SORT_TYPE_PROGRESS),
                                       (_("by age"), SORT_TYPE_ADDED),
                                       (_("by speed"), SORT_TYPE_SPEED)])

    def pauseShown(self):
        if self.transmission is not None:
            self.transmission.stop([x[0].id for x in self.list])

    def unpauseShown(self):
        if self.transmission is not None:
            self.transmission.start([x[0].id for x in self.list])

    def pauseAll(self):
        if self.transmission is None:
            return

        try:
            self.transmission.stop(
                [x.id for x in self.transmission.list().values()])
        except TransmissionError as te:
            self.session.open(
                MessageBox,
                _("Error communicating with transmission-daemon: %s.") % (te),
                type=MessageBox.TYPE_ERROR,
                timeout=5)

    def unpauseAll(self):
        if self.transmission is None:
            return

        try:
            self.transmission.start(
                [x.id for x in self.transmission.list().values()])
        except TransmissionError as te:
            self.session.open(
                MessageBox,
                _("Error communicating with transmission-daemon: %s.") % (te),
                type=MessageBox.TYPE_ERROR,
                timeout=5)

    def configure(self):
        #reload(EmissionSetup)
        self.timer.stop()
        self.session.openWithCallback(self.configureCallback,
                                      EmissionSetup.EmissionSetup)

    def menu(self):
        self.session.openWithCallback(
            self.menuCallback,
            ChoiceBox,
            _("What do you want to do?"),
            [(_("Configure connection"), self.configure),
             (_("Change sorting"), self.sort),
             (_("Add new download"), self.newDl),
             (_("Pause shown"), self.pauseShown),
             (_("Unpause shown"), self.unpauseShown),
             (_("Pause all"), self.pauseAll),
             (_("Unpause all"), self.unpauseAll)],
        )

    def showHideSetTextMagic(self):
        list_type = self.list_type
        if list_type == LIST_TYPE_ALL:
            self["all_sel"].show()
            self["downloading_sel"].hide()
            self["seeding_sel"].hide()
            self["key_yellow"].setText(_("Seeding"))
            self["key_blue"].setText(_("Download"))
        elif list_type == LIST_TYPE_DOWNLOADING:
            self["all_sel"].hide()
            self["downloading_sel"].show()
            self["seeding_sel"].hide()
            self["key_yellow"].setText(_("All"))
            self["key_blue"].setText(_("Seeding"))
        else:  #if list_type == LIST_TYPE_SEEDING:
            self["all_sel"].hide()
            self["downloading_sel"].hide()
            self["seeding_sel"].show()
            self["key_yellow"].setText(_("Download"))
            self["key_blue"].setText(_("All"))

    def prevlist(self):
        self.timer.stop()
        list_type = self.list_type
        if list_type == LIST_TYPE_ALL:
            self.list_type = LIST_TYPE_SEEDING
        elif list_type == LIST_TYPE_DOWNLOADING:
            self.list_type = LIST_TYPE_ALL
        else:  #if list_type == LIST_TYPE_SEEDING:
            self.list_type = LIST_TYPE_DOWNLOADING
        self.showHideSetTextMagic()
        self.updateList()

    def nextlist(self):
        self.timer.stop()
        list_type = self.list_type
        if list_type == LIST_TYPE_ALL:
            self.list_type = LIST_TYPE_DOWNLOADING
        elif list_type == LIST_TYPE_DOWNLOADING:
            self.list_type = LIST_TYPE_SEEDING
        else:  #if list_type == LIST_TYPE_SEEDING:
            self.list_type = LIST_TYPE_ALL
        self.showHideSetTextMagic()
        self.updateList()

    def prevItem(self):
        self['list'].selectPrevious()
        cur = self['list'].getCurrent()
        return cur and cur[0]

    def nextItem(self):
        self['list'].selectNext()
        cur = self['list'].getCurrent()
        return cur and cur[0]

    def bandwidth(self):
        if self.transmission is None:
            return

        #reload(EmissionBandwidth)
        self.timer.stop()
        try:
            sess = self.transmission.get_session()
            rpc_version = self.transmission.rpc_version
        except TransmissionError as te:
            self.session.open(
                MessageBox,
                _("Error communicating with transmission-daemon: %s.") % (te),
                type=MessageBox.TYPE_ERROR,
                timeout=5)
            # XXX: this seems silly but cleans the gui and restarts the timer :-)
            self.updateList()
        else:
            self.session.openWithCallback(self.bandwidthCallback,
                                          EmissionBandwidth.EmissionBandwidth,
                                          sess, False, rpc_version)

    def configureCallback(self):
        try:
            self.transmission = Client(
                address=config.plugins.emission.hostname.value,
                port=config.plugins.emission.port.value,
                user=config.plugins.emission.username.value,
                password=config.plugins.emission.password.value)
        except TransmissionError as te:
            self.transmission = None
            self.session.open(
                MessageBox,
                _("Error communicating with transmission-daemon: %s.") % (te),
                type=MessageBox.TYPE_ERROR,
                timeout=5)
        else:
            self.updateList()

    def updateList(self, *args, **kwargs):
        # XXX: if we are not connected do NOT restart timer, it's useless anyway
        if self.transmission is None:
            return

        try:
            lst = list(self.transmission.list().values())
            session = self.transmission.session_stats()
        except TransmissionError:
            # XXX: some hint in gui would be nice
            self['list'].setList([])
            self["torrents"].setText("")
            self["upspeed"].setText("")
            self["downspeed"].setText("")
        else:
            sort_type = self.sort_type
            if sort_type == SORT_TYPE_TIME:

                def cmp_func(x, y):
                    x_eta = x.fields['eta']
                    y_eta = y.fields['eta']
                    if x_eta > -1 and y_eta < 0:
                        return 1
                    if x_eta < 0 and y_eta > -1:
                        return -1
                    # note: cmp call inversed because lower eta is "better"
                    return cmp(y_eta, x_eta) or cmp(x.progress, y.progress)

                lst.sort(cmp=cmp_func, reverse=True)
            elif sort_type == SORT_TYPE_PROGRESS:
                lst.sort(key=lambda x: x.progress, reverse=True)
            elif sort_type == SORT_TYPE_SPEED:
                lst.sort(key=lambda x: (x.rateDownload, x.rateUpload),
                         reverse=True)
            # SORT_TYPE_ADDED is what we already have

            list_type = self.list_type
            if list_type == LIST_TYPE_ALL:
                lst = [(x, x.name.encode('utf-8', 'ignore'),
                        str(x.eta
                            or '?:??:??').encode('utf-8'), int(x.progress))
                       for x in lst]
            elif list_type == LIST_TYPE_DOWNLOADING:
                lst = [(x, x.name.encode('utf-8', 'ignore'),
                        str(x.eta
                            or '?:??:??').encode('utf-8'), int(x.progress))
                       for x in lst if x.status == "downloading"]
            else:  #if list_type == LIST_TYPE_SEEDING:
                lst = [(x, x.name.encode('utf-8', 'ignore'),
                        str(x.eta
                            or '?:??:??').encode('utf-8'), int(x.progress))
                       for x in lst if x.status == "seeding"]

            self["torrents"].setText(
                _("Active Torrents: %d/%d") %
                (session.activeTorrentCount, session.torrentCount))
            self["upspeed"].setText(
                _("UL: %d kb/s") % (session.uploadSpeed / 1024))
            self["downspeed"].setText(
                _("DL: %d kb/s") % (session.downloadSpeed / 1024))

            # XXX: this is a little ugly but this way we have the least
            # visible distortion :-)
            index = min(self['list'].index, len(lst) - 1)
            self['list'].setList(lst)
            self['list'].index = index

            self.list = lst
        self.timer.startLongTimer(10)

    def ok(self):
        cur = self['list'].getCurrent()
        if self.transmission is not None and cur:
            #reload(EmissionDetailview)
            self.timer.stop()
            self.session.openWithCallback(
                self.updateList,
                EmissionDetailview.EmissionDetailview,
                self.transmission,
                cur[0],
                self.prevItem,
                self.nextItem,
            )

    def close(self):
        self.timer.stop()
        config.plugins.emission.last_tab.value = self.list_type
        config.plugins.emission.last_tab.save()
        Screen.close(self)
Exemplo n.º 2
0
class TransmissionClient(client.TorrentClient):
    """ Backend implementation for transmission """

    config_key = "client_transmission"

    _torrent_list_args = None

    def __init__(self, host=None, port=None, user=None, password=None):
        super(TransmissionClient, self).__init__()
        if not host:
            host = config.get_default(self.config_key, "host", "localhost")
        self.host = host
        if not port:
            port = config.get_default(self.config_key, "port", DEFAULT_PORT,
                                      int)
        self.port = port
        if not user:
            user = config.get_default(self.config_key, "user", None)
        self.user = user
        if not password:
            password = config.get_default(self.config_key, "password", None)
        self.password = password
        self.client = None
        self.connect()

    def client_version(self):
        version = "{}.{} ({})".format(*self.client.server_version)
        return version

    def connect(self):
        try:
            self.client = Client(address=self.host,
                                 port=self.port,
                                 user=self.user,
                                 password=self.password)
        except TransmissionError as err:
            if err.original.code == 111:
                self.log.error(
                    "Failed to connect to transmission-daemon, is it running?")
            elif err.original.code == 113:
                self.log.error("No route to host")
            else:
                self.log.exception("Error connecting to transmission server")
            raise

    def add(self, data, download_dir=None):
        """ Add a torrent to the client

        :param data: Torrent data to load in
        :type data: TorrentData
        :param download_dir: Path on deluge server to store download
        :type download_dir: basestring
        :return: Status of successful load (according to deluge)
        :rtype: bool
        """
        try:
            torrent = Torrent.from_str(data.torrent_data)
            try:
                self.torrent_status(torrent.info_hash)
            except KeyError:
                pass
            else:
                self.log.warn("Tried to load duplicate info hash: {}".format(
                    torrent.info_hash))
                return True
            torrent_data = b64encode(data.torrent_data)
            res = self.client.add_torrent(torrent_data,
                                          download_dir=download_dir)
        except TransmissionError as err:
            try:
                msg = err._message
            except AttributeError:
                msg = err.message
            if "duplicate torrent" in msg:
                self.log.warning("Tried to add duplicate torrent file")
                return True
            self.log.exception(err)
            return False

        return res

    torrent_add = add

    def current_speeds(self):
        """ Fetch the speeds from the session instance
        :return: Upload, Download speeds in bytes/s
        :rtype: tuple
        """
        ses = self.client.session_stats()
        return ses.uploadSpeed, ses.downloadSpeed

    def torrent_list(self):
        """ Get a list of currently loaded torrents from the client

        :return:
        :rtype:
        """
        if not self._torrent_list_args:
            self._torrent_list_args = get_arguments('torrent-get',
                                                    self.client.rpc_version)
            self._torrent_list_args.extend([
                'seeders', 'peersKnown', 'peersGettingFromUs',
                'peersSendingToUs', 'isPrivate'
            ])
        torrents = self.client.get_torrents(arguments=self._torrent_list_args)
        torrent_data = list()
        for torrent in torrents:
            data = client.ClientTorrentData(
                info_hash=torrent.hashString,
                name=torrent.name,
                ratio=torrent.ratio,
                up_rate=torrent.rateUpload,
                dn_rate=torrent.rateDownload,
                up_total=torrent.uploadedEver,
                dn_total=torrent.downloadedEver,
                size=torrent.sizeWhenDone,
                size_completed=torrent.sizeWhenDone - torrent.leftUntilDone,
                # TODO peer values are wrong
                peers=torrent.peersGettingFromUs,
                total_peers=0,
                seeders=torrent.peersSendingToUs,
                total_seeders=0,
                priority=torrent.priority,
                private=torrent.isPrivate,
                state=torrent.status,
                progress=torrent.progress)
            torrent_data.append(data)
        return torrent_data

    def torrent_remove(self, torrent_id, remove_data=False):
        """ Remove a torrent from the backend client via its torrentID supplied by the
        torrent daemon

        :param remove_data: Remove the torrent data file as well as .torrent
        :type remove_data: bool
        :param torrent_id: TorrentID provided by transmission
        :type torrent_id: int
        """
        self.client.remove_torrent(torrent_id, delete_data=remove_data)

    def torrent_peers(self, info_hash):
        torrent = self.client.get_torrent(
            info_hash, arguments=['id', 'hashString', 'peers'])
        peers = []
        session = Session()
        # TODO country code lookup
        for peer in torrent.peers:
            peers.append({
                'client':
                peer['clientName'],
                'down_speed':
                peer['rateToClient'],
                'up_speed':
                peer['rateToPeer'],
                'progress':
                peer['progress'],
                'ip':
                peer['address'],
                'country':
                geoip.find_country_code(session, peer['address'])
            })
        return peers

    def torrent_start(self, info_hash):
        self.client.start_torrent(info_hash)
        return True

    def torrent_pause(self, info_hash):
        self.client.stop(info_hash)
        return True

    def torrent_files(self, info_hash):
        files = []
        file_set = self.client.get_files(info_hash)
        for v in list(file_set.values()):
            for file_info in [f for f in list(v.values())]:
                files.append(
                    client.ClientFileData(path=file_info['name'],
                                          progress=file_info['size'] -
                                          file_info['completed'],
                                          size=file_info['size'],
                                          priority=file_info['priority']))
                break
        return files

    def torrent_speed(self, info_hash):
        speed = self.client.get_torrent(
            info_hash,
            arguments=['id', 'hashString', 'rateDownload', 'rateUpload'])
        return speed.rateDownload, speed.rateUpload

    def disconnect(self):
        return True

    def torrent_status(self, info_hash):
        key_map = {
            'info_hash':
            'hashString',
            'up_rate':
            'rateUpload',
            'dn_rate':
            'rateDownload',
            'up_total':
            'uploadedEver',
            'dn_total':
            'downloadedEver',
            'size':
            'totalSize',
            'size_completed':
            'percentDone',  # wrong
            'seeders':
            lambda t: len(t.peers),
            'total_seeders':
            lambda t: len(t.peers),
            'peers':
            'peersConnected',
            'total_peers':
            lambda t: len(t.peers),
            'priority':
            'queue_position',
            'private':
            'isPrivate',
            'state':
            'status',
            'progress':
            '',
            'tracker_status':
            '',
            'next_announce':
            lambda t: t.trackerStats[0]['nextAnnounceTime'],
            'save_path':
            'downloadDir',
            'piece_length':
            'pieceSize',
            'num_pieces':
            'pieceCount',
            'time_added':
            'addedDate',
            'distributed_copies':
            lambda t: functools.reduce(lambda a, b: a + b,
                                       [p['progress'] for p in t.peers], 0),
            'active_time':
            '',
            #'seeding_time': 'secondsSeeding', Not found in my version? trans 2.8.4
            'num_files':
            lambda t: len(torrent.files()),
            'queue_position':
            'queue_position'
        }
        torrent = self.client.get_torrent(info_hash)
        detail = client.ClientTorrentDataDetail(info_hash=info_hash)
        for key in detail.key_list:
            val = key_map.get(key, None)
            if val:
                if callable(val):
                    detail[key] = val(torrent)
                else:
                    detail[key] = getattr(torrent, val)
        return detail

    def torrent_recheck(self, info_hash):
        self.client.verify_torrent(info_hash)
        return True

    def torrent_reannounce(self, info_hash):
        self.client.reannounce_torrent(info_hash)
        return True

    def torrent_queue_up(self, info_hash):
        self.client.queue_up(info_hash)
        return True

    def torrent_queue_down(self, info_hash):
        self.client.queue_down(info_hash)
        return True

    def torrent_queue_top(self, info_hash):
        self.client.queue_top(info_hash)
        return True

    def torrent_queue_bottom(self, info_hash):
        self.client.queue_bottom(info_hash)
        return True

    def torrent_move_data(self, info_hash, dest):
        self.client.move_torrent_data(info_hash, dest)
        return True
Exemplo n.º 3
0
class TransmissionClient(client.TorrentClient):
    """ Backend implementation for transmission """

    config_key = "client_transmission"

    _torrent_list_args = None

    def __init__(self, host=None, port=None, user=None, password=None):
        super(TransmissionClient, self).__init__()
        if not host:
            host = config.get_default(self.config_key, "host", "localhost")
        self.host = host
        if not port:
            port = config.get_default(self.config_key, "port", DEFAULT_PORT, int)
        self.port = port
        if not user:
            user = config.get_default(self.config_key, "user", None)
        self.user = user
        if not password:
            password = config.get_default(self.config_key, "password", None)
        self.password = password
        self.client = None
        self.connect()

    def client_version(self):
        version = "{}.{} ({})".format(*self.client.server_version)
        return version

    def connect(self):
        try:
            self.client = Client(address=self.host, port=self.port, user=self.user, password=self.password)
        except TransmissionError as err:
            if err.original.code == 111:
                self.log.error("Failed to connect to transmission-daemon, is it running?")
            elif err.original.code == 113:
                self.log.error("No route to host")
            else:
                self.log.exception("Error connecting to transmission server")
            raise

    def add(self, data, download_dir=None):
        """ Add a torrent to the client

        :param data: Torrent data to load in
        :type data: TorrentData
        :param download_dir: Path on deluge server to store download
        :type download_dir: basestring
        :return: Status of successful load (according to deluge)
        :rtype: bool
        """
        try:
            torrent = Torrent.from_str(data.torrent_data)
            try:
                self.torrent_status(torrent.info_hash)
            except KeyError:
                pass
            else:
                self.log.warn("Tried to load duplicate info hash: {}".format(torrent.info_hash))
                return True
            torrent_data = b64encode(data.torrent_data)
            res = self.client.add_torrent(torrent_data, download_dir=download_dir)
        except TransmissionError as err:
            try:
                msg = err._message
            except AttributeError:
                msg = err.message
            if "duplicate torrent" in msg:
                self.log.warning("Tried to add duplicate torrent file")
                return True
            self.log.exception(err)
            return False

        return res

    torrent_add = add

    def current_speeds(self):
        """ Fetch the speeds from the session instance
        :return: Upload, Download speeds in bytes/s
        :rtype: tuple
        """
        ses = self.client.session_stats()
        return ses.uploadSpeed, ses.downloadSpeed

    def torrent_list(self):
        """ Get a list of currently loaded torrents from the client

        :return:
        :rtype:
        """
        if not self._torrent_list_args:
            self._torrent_list_args = get_arguments('torrent-get', self.client.rpc_version)
            self._torrent_list_args.extend(['seeders', 'peersKnown',
                                            'peersGettingFromUs', 'peersSendingToUs',
                                            'isPrivate'])
        torrents = self.client.get_torrents(arguments=self._torrent_list_args)
        torrent_data = list()
        for torrent in torrents:
            data = client.ClientTorrentData(
                info_hash=torrent.hashString,
                name=torrent.name,
                ratio=torrent.ratio,
                up_rate=torrent.rateUpload,
                dn_rate=torrent.rateDownload,
                up_total=torrent.uploadedEver,
                dn_total=torrent.downloadedEver,
                size=torrent.sizeWhenDone,
                size_completed=torrent.sizeWhenDone-torrent.leftUntilDone,
                # TODO peer values are wrong
                peers=torrent.peersGettingFromUs,
                total_peers=0,
                seeders=torrent.peersSendingToUs,
                total_seeders=0,
                priority=torrent.priority,
                private=torrent.isPrivate,
                state=torrent.status,
                progress=torrent.progress
            )
            torrent_data.append(data)
        return torrent_data

    def torrent_remove(self, torrent_id, remove_data=False):
        """ Remove a torrent from the backend client via its torrentID supplied by the
        torrent daemon

        :param remove_data: Remove the torrent data file as well as .torrent
        :type remove_data: bool
        :param torrent_id: TorrentID provided by transmission
        :type torrent_id: int
        """
        self.client.remove_torrent(torrent_id, delete_data=remove_data)

    def torrent_peers(self, info_hash):
        torrent = self.client.get_torrent(info_hash, arguments=['id', 'hashString', 'peers'])
        peers = []
        session = Session()
        # TODO country code lookup
        for peer in torrent.peers:
            peers.append({
                'client': peer['clientName'],
                'down_speed': peer['rateToClient'],
                'up_speed': peer['rateToPeer'],
                'progress': peer['progress'],
                'ip': peer['address'],
                'country': geoip.find_country_code(session, peer['address'])
            })
        return peers

    def torrent_start(self, info_hash):
        self.client.start_torrent(info_hash)
        return True

    def torrent_pause(self, info_hash):
        self.client.stop(info_hash)
        return True

    def torrent_files(self, info_hash):
        files = []
        file_set = self.client.get_files(info_hash)
        for v in file_set.values():
            for file_info in [f for f in v.values()]:
                files.append(client.ClientFileData(
                    path=file_info['name'],
                    progress=file_info['size'] - file_info['completed'],
                    size=file_info['size'],
                    priority=file_info['priority']
                ))
                break
        return files

    def torrent_speed(self, info_hash):
        speed = self.client.get_torrent(info_hash, arguments=['id', 'hashString', 'rateDownload', 'rateUpload'])
        return speed.rateDownload, speed.rateUpload

    def disconnect(self):
        return True

    def torrent_status(self, info_hash):
        key_map = {
            'info_hash': 'hashString',
            'up_rate': 'rateUpload',
            'dn_rate': 'rateDownload',
            'up_total': 'uploadedEver',
            'dn_total': 'downloadedEver',
            'size': 'totalSize',
            'size_completed': 'percentDone',  # wrong
            'seeders': lambda t: len(t.peers),
            'total_seeders': lambda t: len(t.peers),
            'peers': 'peersConnected',
            'total_peers': lambda t: len(t.peers),
            'priority': 'queue_position',
            'private': 'isPrivate',
            'state': 'status',
            'progress': '',
            'tracker_status': '',
            'next_announce': lambda t: t.trackerStats[0]['nextAnnounceTime'],
            'save_path': 'downloadDir',
            'piece_length': 'pieceSize',
            'num_pieces': 'pieceCount',
            'time_added': 'addedDate',
            'distributed_copies': lambda t: functools.reduce(lambda a,b: a+b, [p['progress'] for p in t.peers], 0),
            'active_time': '',
            #'seeding_time': 'secondsSeeding', Not found in my version? trans 2.8.4
            'num_files': lambda t: len(torrent.files()),
            'queue_position': 'queue_position'
        }
        torrent = self.client.get_torrent(info_hash)
        detail = client.ClientTorrentDataDetail(info_hash=info_hash)
        for key in detail.key_list:
            val = key_map.get(key, None)
            if val:
                if callable(val):
                    detail[key] = val(torrent)
                else:
                    detail[key] = getattr(torrent, val)
        return detail

    def torrent_recheck(self, info_hash):
        self.client.verify_torrent(info_hash)
        return True

    def torrent_reannounce(self, info_hash):
        self.client.reannounce_torrent(info_hash)
        return True

    def torrent_queue_up(self, info_hash):
        self.client.queue_up(info_hash)
        return True

    def torrent_queue_down(self, info_hash):
        self.client.queue_down(info_hash)
        return True

    def torrent_queue_top(self, info_hash):
        self.client.queue_top(info_hash)
        return True

    def torrent_queue_bottom(self, info_hash):
        self.client.queue_bottom(info_hash)
        return True

    def torrent_move_data(self, info_hash, dest):
        self.client.move_torrent_data(info_hash, dest)
        return True
Exemplo n.º 4
0
class EmissionOverview(Screen, HelpableScreen):
    skin = """<screen name="EmissionOverview" title="Torrent Overview" position="75,135" size="565,330">
		<ePixmap position="0,0" size="140,40" pixmap="skin_default/buttons/red.png" transparent="1" alphatest="on" />
		<ePixmap position="140,0" size="140,40" pixmap="skin_default/buttons/green.png" transparent="1" alphatest="on" />
		<ePixmap position="280,0" size="140,40" pixmap="skin_default/buttons/yellow.png" transparent="1" alphatest="on" />
		<ePixmap position="420,0" size="140,40" pixmap="skin_default/buttons/blue.png" transparent="1" alphatest="on" />
		<widget source="key_red" render="Label" position="0,0" zPosition="1" size="140,40" valign="center" halign="center" font="Regular;21" transparent="1" foregroundColor="white" shadowColor="black" shadowOffset="-1,-1" />
		<widget source="key_green" render="Label" position="140,0" zPosition="1" size="140,40" valign="center" halign="center" font="Regular;21" transparent="1" foregroundColor="white" shadowColor="black" shadowOffset="-1,-1" />
		<widget source="key_yellow" render="Label" position="280,0" zPosition="1" size="140,40" valign="center" halign="center" font="Regular;21" transparent="1" foregroundColor="white" shadowColor="black" shadowOffset="-1,-1" />
		<widget source="key_blue" render="Label" position="420,0" zPosition="1" size="140,40" valign="center" halign="center" font="Regular;21" transparent="1" foregroundColor="white" shadowColor="black" shadowOffset="-1,-1" />
		<widget size="320,25" alphatest="on" position="5,45" zPosition="1" name="all_sel" pixmap="skin_default/epg_now.png" />
		<widget valign="center" transparent="1" size="108,22" backgroundColor="#25062748" position="5,47" zPosition="2" source="all_text" render="Label" halign="center" font="Regular;18" />
		<widget size="320,25" alphatest="on" position="5,45" zPosition="1" name="downloading_sel" pixmap="skin_default/epg_next.png" />
		<widget valign="center" transparent="1" size="108,22" backgroundColor="#25062748" position="111,47" zPosition="2" source="downloading_text" render="Label" halign="center" font="Regular;18" />
		<widget size="320,25" alphatest="on" position="5,45" zPosition="1" name="seeding_sel" pixmap="skin_default/epg_more.png" />
		<widget valign="center" transparent="1" size="108,22" backgroundColor="#25062748" position="212,47" zPosition="2" source="seeding_text" render="Label" halign="center" font="Regular;18" />
		<widget source="torrents" render="Label" size="240,22" position="320,47" halign="right" font="Regular;18" />
		<!--ePixmap size="550,230" alphatest="on" position="5,65" pixmap="skin_default/border_epg.png" /-->
		<widget source="list" render="Listbox" position="5,70" size="550,225" scrollbarMode="showAlways">
			<convert type="TemplatedMultiContent">
				{"template": [
						MultiContentEntryText(pos=(2,2), size=(555,22), text = 1, font = 0, flags = RT_HALIGN_LEFT|RT_VALIGN_CENTER),
						MultiContentEntryText(pos=(2,26), size=(555,18), text = 2, font = 1, flags = RT_HALIGN_LEFT|RT_VALIGN_CENTER),
						(eListboxPythonMultiContent.TYPE_PROGRESS, 0, 44, 537, 6, -3),
					],
				  "fonts": [gFont("Regular", 20),gFont("Regular", 16)],
				  "itemHeight": 51
				 }
			</convert>
		</widget>
		<widget source="upspeed" render="Label" size="150,20" position="5,300" halign="left" font="Regular;18" />
		<widget source="downspeed" render="Label" size="150,20" position="410,300" halign="right" font="Regular;18" />
	</screen>"""

    def __init__(self, session):
        Screen.__init__(self, session)
        HelpableScreen.__init__(self)

        try:
            self.transmission = Client(
                address=config.plugins.emission.hostname.value,
                port=config.plugins.emission.port.value,
                user=config.plugins.emission.username.value,
                password=config.plugins.emission.password.value,
            )
        except TransmissionError as te:
            self.transmission = None

        self["SetupActions"] = HelpableActionMap(
            self, "SetupActions", {"ok": (self.ok, _("show details")), "cancel": (self.close, _("close"))}
        )

        self["ColorActions"] = HelpableActionMap(
            self,
            "ColorActions",
            {
                "green": (self.bandwidth, _("open bandwidth settings")),
                "yellow": (self.prevlist, _("show previous list")),
                "blue": (self.nextlist, _("show next list")),
            },
        )

        self["MenuActions"] = HelpableActionMap(self, "MenuActions", {"menu": (self.menu, _("open context menu"))})

        self["key_red"] = StaticText(_("Close"))
        self["key_green"] = StaticText(_("Bandwidth"))
        self["key_yellow"] = StaticText("")
        self["key_blue"] = StaticText("")

        self["all_text"] = StaticText(_("All"))
        self["downloading_text"] = StaticText(_("DL"))
        self["seeding_text"] = StaticText(_("UL"))
        self["upspeed"] = StaticText("")
        self["downspeed"] = StaticText("")
        self["torrents"] = StaticText("")

        self["all_sel"] = Pixmap()
        self["downloading_sel"] = Pixmap()
        self["seeding_sel"] = Pixmap()

        self["list"] = List([])

        self.list_type = config.plugins.emission.last_tab.value
        self.sort_type = config.plugins.emission.last_sort.value
        self.showHideSetTextMagic()

        self.timer = eTimer()
        self.timer.callback.append(self.updateList)
        self.timer.start(0, 1)

    def bandwidthCallback(self, ret=None):
        if self.transmission is not None and ret:
            try:
                self.transmission.set_session(**ret)
            except TransmissionError as te:
                self.session.open(
                    MessageBox,
                    _("Error communicating with transmission-daemon: %s.") % (te),
                    type=MessageBox.TYPE_ERROR,
                    timeout=5,
                )
        self.updateList()

    def menuCallback(self, ret=None):
        ret and ret[1]()

    def newDlCallback(self, ret=None):
        if self.transmission is not None and ret:
            try:
                res = self.transmission.add_url(ret)
            except TransmissionError as te:
                self.session.open(
                    MessageBox,
                    _("Error communicating with transmission-daemon: %s.") % (te),
                    type=MessageBox.TYPE_ERROR,
                    timeout=5,
                )
            else:
                if not res:
                    self.session.open(
                        MessageBox,
                        _("Torrent could not be scheduled not download!"),
                        type=MessageBox.TYPE_ERROR,
                        timeout=5,
                    )
        self.updateList()

    def newDl(self):
        self.timer.stop()
        self.session.openWithCallback(self.newDlCallback, TorrentLocationBox)

    def sortCallback(self, ret=None):
        if ret is not None:
            self.sort_type = config.plugins.emission.last_sort.value = ret[1]
            config.plugins.emission.last_sort.save()
        self.updateList()

    def sort(self):
        self.timer.stop()
        self.session.openWithCallback(
            self.sortCallback,
            ChoiceBox,
            _("Which sorting method do you prefer?"),
            [
                (_("by eta"), SORT_TYPE_TIME),
                (_("by progress"), SORT_TYPE_PROGRESS),
                (_("by age"), SORT_TYPE_ADDED),
                (_("by speed"), SORT_TYPE_SPEED),
            ],
        )

    def pauseShown(self):
        if self.transmission is not None:
            self.transmission.stop([x[0].id for x in self.list])

    def unpauseShown(self):
        if self.transmission is not None:
            self.transmission.start([x[0].id for x in self.list])

    def pauseAll(self):
        if self.transmission is None:
            return

        try:
            self.transmission.stop([x.id for x in self.transmission.list().values()])
        except TransmissionError as te:
            self.session.open(
                MessageBox,
                _("Error communicating with transmission-daemon: %s.") % (te),
                type=MessageBox.TYPE_ERROR,
                timeout=5,
            )

    def unpauseAll(self):
        if self.transmission is None:
            return

        try:
            self.transmission.start([x.id for x in self.transmission.list().values()])
        except TransmissionError as te:
            self.session.open(
                MessageBox,
                _("Error communicating with transmission-daemon: %s.") % (te),
                type=MessageBox.TYPE_ERROR,
                timeout=5,
            )

    def configure(self):
        # reload(EmissionSetup)
        self.timer.stop()
        self.session.openWithCallback(self.configureCallback, EmissionSetup.EmissionSetup)

    def menu(self):
        self.session.openWithCallback(
            self.menuCallback,
            ChoiceBox,
            _("What do you want to do?"),
            [
                (_("Configure connection"), self.configure),
                (_("Change sorting"), self.sort),
                (_("Add new download"), self.newDl),
                (_("Pause shown"), self.pauseShown),
                (_("Unpause shown"), self.unpauseShown),
                (_("Pause all"), self.pauseAll),
                (_("Unpause all"), self.unpauseAll),
            ],
        )

    def showHideSetTextMagic(self):
        list_type = self.list_type
        if list_type == LIST_TYPE_ALL:
            self["all_sel"].show()
            self["downloading_sel"].hide()
            self["seeding_sel"].hide()
            self["key_yellow"].setText(_("Seeding"))
            self["key_blue"].setText(_("Download"))
        elif list_type == LIST_TYPE_DOWNLOADING:
            self["all_sel"].hide()
            self["downloading_sel"].show()
            self["seeding_sel"].hide()
            self["key_yellow"].setText(_("All"))
            self["key_blue"].setText(_("Seeding"))
        else:  # if list_type == LIST_TYPE_SEEDING:
            self["all_sel"].hide()
            self["downloading_sel"].hide()
            self["seeding_sel"].show()
            self["key_yellow"].setText(_("Download"))
            self["key_blue"].setText(_("All"))

    def prevlist(self):
        self.timer.stop()
        list_type = self.list_type
        if list_type == LIST_TYPE_ALL:
            self.list_type = LIST_TYPE_SEEDING
        elif list_type == LIST_TYPE_DOWNLOADING:
            self.list_type = LIST_TYPE_ALL
        else:  # if list_type == LIST_TYPE_SEEDING:
            self.list_type = LIST_TYPE_DOWNLOADING
        self.showHideSetTextMagic()
        self.updateList()

    def nextlist(self):
        self.timer.stop()
        list_type = self.list_type
        if list_type == LIST_TYPE_ALL:
            self.list_type = LIST_TYPE_DOWNLOADING
        elif list_type == LIST_TYPE_DOWNLOADING:
            self.list_type = LIST_TYPE_SEEDING
        else:  # if list_type == LIST_TYPE_SEEDING:
            self.list_type = LIST_TYPE_ALL
        self.showHideSetTextMagic()
        self.updateList()

    def prevItem(self):
        self["list"].selectPrevious()
        cur = self["list"].getCurrent()
        return cur and cur[0]

    def nextItem(self):
        self["list"].selectNext()
        cur = self["list"].getCurrent()
        return cur and cur[0]

    def bandwidth(self):
        if self.transmission is None:
            return

            # reload(EmissionBandwidth)
        self.timer.stop()
        try:
            sess = self.transmission.get_session()
            rpc_version = self.transmission.rpc_version
        except TransmissionError as te:
            self.session.open(
                MessageBox,
                _("Error communicating with transmission-daemon: %s.") % (te),
                type=MessageBox.TYPE_ERROR,
                timeout=5,
            )
            # XXX: this seems silly but cleans the gui and restarts the timer :-)
            self.updateList()
        else:
            self.session.openWithCallback(
                self.bandwidthCallback, EmissionBandwidth.EmissionBandwidth, sess, False, rpc_version
            )

    def configureCallback(self):
        try:
            self.transmission = Client(
                address=config.plugins.emission.hostname.value,
                port=config.plugins.emission.port.value,
                user=config.plugins.emission.username.value,
                password=config.plugins.emission.password.value,
            )
        except TransmissionError as te:
            self.transmission = None
            self.session.open(
                MessageBox,
                _("Error communicating with transmission-daemon: %s.") % (te),
                type=MessageBox.TYPE_ERROR,
                timeout=5,
            )
        else:
            self.updateList()

    def updateList(self, *args, **kwargs):
        # XXX: if we are not connected do NOT restart timer, it's useless anyway
        if self.transmission is None:
            return

        try:
            lst = list(self.transmission.list().values())
            session = self.transmission.session_stats()
        except TransmissionError:
            # XXX: some hint in gui would be nice
            self["list"].setList([])
            self["torrents"].setText("")
            self["upspeed"].setText("")
            self["downspeed"].setText("")
        else:
            sort_type = self.sort_type
            if sort_type == SORT_TYPE_TIME:

                def cmp_func(x, y):
                    x_eta = x.fields["eta"]
                    y_eta = y.fields["eta"]
                    if x_eta > -1 and y_eta < 0:
                        return 1
                    if x_eta < 0 and y_eta > -1:
                        return -1
                        # note: cmp call inversed because lower eta is "better"
                    return cmp(y_eta, x_eta) or cmp(x.progress, y.progress)

                lst.sort(cmp=cmp_func, reverse=True)
            elif sort_type == SORT_TYPE_PROGRESS:
                lst.sort(key=lambda x: x.progress, reverse=True)
            elif sort_type == SORT_TYPE_SPEED:
                lst.sort(key=lambda x: (x.rateDownload, x.rateUpload), reverse=True)
                # SORT_TYPE_ADDED is what we already have

            list_type = self.list_type
            if list_type == LIST_TYPE_ALL:
                lst = [
                    (x, x.name.encode("utf-8", "ignore"), str(x.eta or "?:??:??").encode("utf-8"), int(x.progress))
                    for x in lst
                ]
            elif list_type == LIST_TYPE_DOWNLOADING:
                lst = [
                    (x, x.name.encode("utf-8", "ignore"), str(x.eta or "?:??:??").encode("utf-8"), int(x.progress))
                    for x in lst
                    if x.status == "downloading"
                ]
            else:  # if list_type == LIST_TYPE_SEEDING:
                lst = [
                    (x, x.name.encode("utf-8", "ignore"), str(x.eta or "?:??:??").encode("utf-8"), int(x.progress))
                    for x in lst
                    if x.status == "seeding"
                ]

            self["torrents"].setText(_("Active Torrents: %d/%d") % (session.activeTorrentCount, session.torrentCount))
            self["upspeed"].setText(_("UL: %d kb/s") % (session.uploadSpeed / 1024))
            self["downspeed"].setText(_("DL: %d kb/s") % (session.downloadSpeed / 1024))

            # XXX: this is a little ugly but this way we have the least
            # visible distortion :-)
            index = min(self["list"].index, len(lst) - 1)
            self["list"].setList(lst)
            self["list"].index = index

            self.list = lst
        self.timer.startLongTimer(10)

    def ok(self):
        cur = self["list"].getCurrent()
        if self.transmission is not None and cur:
            # reload(EmissionDetailview)
            self.timer.stop()
            self.session.openWithCallback(
                self.updateList,
                EmissionDetailview.EmissionDetailview,
                self.transmission,
                cur[0],
                self.prevItem,
                self.nextItem,
            )

    def close(self):
        self.timer.stop()
        config.plugins.emission.last_tab.value = self.list_type
        config.plugins.emission.last_tab.save()
        Screen.close(self)