def getEventDesc(ref, idev):
    ref = unquote(ref)
    epgcache = eEPGCache.getInstance()
    event = epgcache.lookupEvent(['ESX', (ref, 2, int(idev))])
    if len(event[0][0]) > 1:
        description = mangle_epg_text(event[0][0])
    elif len(event[0][1]) > 1:
        description = mangle_epg_text(event[0][1])
    else:
        description = "No description available"

    return {"description": description}
    def get_servicename(self, servicereference, encoding=None):
        if encoding is None:
            encoding = "utf-8"

        if isinstance(servicereference, basestring):
            servicereference = eServiceReference(servicereference)

        actual_servicereference = self.service_center_instance.info(
            servicereference).getInfoString(
            servicereference, iServiceInformation.sServiceref)
        es = eServiceReference(actual_servicereference)
        value = self.service_center_instance.info(es).getName(es).decode(
            encoding)
        value = mangle_epg_text(value)
        # self.log.info("{!r} -> {!r}".format(actual_servicereference, value))
        self.service_lookup[actual_servicereference] = value
        return value
def get_servicereference_name(some_ref):
    """
    Try to dertermine service's name for service reference *some_ref*

    Args:
        some_ref: eServiceReference instance or basestring
    Returns:
        service name or service reference string
    """
    ech = enigma.eServiceCenter.getInstance()

    try:
        if isinstance(some_ref, basestring):
            some_ref = enigma.eServiceReference(some_ref.encode("ascii"))
        sinfo = ech.info(some_ref)
        return mangle_epg_text(sinfo.getName(some_ref))
    except Exception:
        pass

    return str(some_ref)
def getMultiEpg(self, ref, begintime=-1, endtime=None, Mode=1):
    # Check if an event has an associated timer. Unfortunately
    # we cannot simply check against timer.eit, because a timer
    # does not necessarily have one belonging to an epg event id.
    def getTimerEventStatus(event):
        startTime = event[1]
        endTime = event[1] + event[6] - 120
        serviceref = event[4]
        if serviceref not in timerlist:
            return ''
        for timer in timerlist[serviceref]:
            if timer.begin <= startTime and timer.end >= endTime:
                if timer.disabled:
                    return 'timer disabled'
                else:
                    return 'timer'
        return ''

    ret = OrderedDict()
    services = eServiceCenter.getInstance().list(eServiceReference(ref))
    if not services:
        return {"events": ret, "result": False, "slot": None}

    search = ['IBTSRND']
    for service in services.getContent('S'):
        if endtime:
            search.append((service, 0, begintime, endtime))
        else:
            search.append((service, 0, begintime))

    epgcache = eEPGCache.getInstance()
    events = epgcache.lookupEvent(search)
    offset = None
    picons = {}

    if events is not None:
        # We want to display if an event is covered by a timer.
        # To keep the costs low for a nested loop against the timer list, we
        # partition the timers by service reference. For an event we then only
        # have to check the part of the timers that belong to that specific
        # service reference. Partition is generated here.
        timerlist = {}
        for timer in self.session.nav.RecordTimer.timer_list + \
                self.session.nav.RecordTimer.processed_timers:
            if str(timer.service_ref) not in timerlist:
                timerlist[str(timer.service_ref)] = []
            timerlist[str(timer.service_ref)].append(timer)

        if begintime == -1:
            # If no start time is requested, use current time as start time
            # and extend show all events until 6:00 next day
            bt = localtime()
            offset = mktime(
                (bt.tm_year, bt.tm_mon, bt.tm_mday, bt.tm_hour - bt.tm_hour %
                 2, 0, 0, -1, -1, -1))
            lastevent = mktime(
                (bt.tm_year, bt.tm_mon, bt.tm_mday, 23, 59, 0, -1, -1,
                 -1)) + 6 * 3600
        else:
            # If a start time is requested, show all events in a 24 hour frame
            bt = localtime(begintime)
            offset = mktime(
                (bt.tm_year, bt.tm_mon, bt.tm_mday, bt.tm_hour - bt.tm_hour %
                 2, 0, 0, -1, -1, -1))
            lastevent = offset + 86399

        for event in events:
            ev = {}
            ev['id'] = event[0]
            ev['begin_timestamp'] = event[1]
            ev['title'] = event[2]
            ev['shortdesc'] = event[3]
            ev['ref'] = event[4]
            ev['timerStatus'] = getTimerEventStatus(event)
            if Mode == 2:
                ev['duration'] = event[6]

            channel = mangle_epg_text(event[5])
            if channel not in ret:
                if Mode == 1:
                    ret[channel] = [[], [], [], [],
                                    [], [], [], [], [], [], [], []]
                else:
                    ret[channel] = [[]]
                picons[channel] = getPicon(event[4])

            if Mode == 1:
                slot = int((event[1] - offset) / 7200)
                if slot < 0:
                    slot = 0
                if slot < 12 and event[1] < lastevent:
                    ret[channel][slot].append(ev)
            else:
                ret[channel][0].append(ev)
    return {"events": ret, "result": True, "picons": picons}
def getChannels(idbouquet, stype):
    ret = []
    idp = 0
    s_type = service_types_tv
    if stype == "radio":
        s_type = service_types_radio
    if idbouquet == "ALL":
        idbouquet = '%s ORDER BY name' % (s_type)

    epgcache = eEPGCache.getInstance()
    serviceHandler = eServiceCenter.getInstance()
    services = serviceHandler.list(eServiceReference(idbouquet))
    channels = services and services.getContent("SN", True)
    for channel in channels:
        chan = {}
        chan['ref'] = quote(channel[0], safe=' ~@%#$&()*!+=:;,.?/\'')
        if chan['ref'].split(":")[1] == '320':  # Hide hidden number markers
            continue
        chan['name'] = mangle_epg_text(channel[1])
        if not int(channel[0].split(":")[1]) & 64:
            psref = parse_servicereference(channel[0])
            chan['service_type'] = SERVICE_TYPE_LOOKUP.get(
                psref.get('service_type'), "UNKNOWN")
            chan['ns'] = NS_LOOKUP.get(psref.get('ns'), "DVB-S")
            chan['picon'] = getPicon(chan['ref'])
            chan['protection'] = "0"
            nowevent = epgcache.lookupEvent(['TBDCIX', (channel[0], 0, -1)])
            if len(nowevent) > 0 and nowevent[0][0] is not None:
                chan['now_title'] = mangle_epg_text(nowevent[0][0])
                chan['now_begin'] = strftime(
                    "%H:%M", (localtime(nowevent[0][1])))
                chan['now_end'] = strftime(
                    "%H:%M", (localtime(nowevent[0][1] + nowevent[0][2])))
                chan['now_left'] = int(
                    ((nowevent[0][1] + nowevent[0][2]) - nowevent[0][3]) / 60)
                chan['progress'] = int(
                    ((nowevent[0][3] - nowevent[0][1]) * 100 / nowevent[0][2]))
                chan['now_ev_id'] = nowevent[0][4]
                chan['now_idp'] = "nowd" + str(idp)
                nextevent = epgcache.lookupEvent(
                    ['TBDIX', (channel[0], +1, -1)])
                if len(nextevent) > 0 and nextevent[0][0] is not None:
                    # Some fields have been seen to be missing from the next
                    # event...
                    if nextevent[0][1] is None:
                        nextevent[0][1] == time.time()
                    if nextevent[0][2] is None:
                        nextevent[0][2] == 0
                    chan['next_title'] = mangle_epg_text(nextevent[0][0])
                    chan['next_begin'] = strftime(
                        "%H:%M", (localtime(nextevent[0][1])))
                    chan['next_end'] = strftime(
                        "%H:%M", (
                            localtime(nextevent[0][1] + nextevent[0][2])))
                    chan['next_duration'] = int(nextevent[0][2] / 60)
                    chan['next_ev_id'] = nextevent[0][3]
                    chan['next_idp'] = "nextd" + str(idp)
                else:
                    # Have to fudge one in, as rest of OWI code expects it...
                    chan['next_title'] = mangle_epg_text("<<absent>>")
                    chan['next_begin'] = chan['now_end']
                    chan['next_end'] = chan['now_end']
                    chan['next_duration'] = 0
                    chan['next_ev_id'] = chan['now_ev_id']
                    chan['next_idp'] = chan['now_idp']
                idp += 1
        if int(channel[0].split(":")[1]) != 832:
            ret.append(chan)
    return {"channels": ret}