示例#1
0
class Channels:
    """ Menu code related to channels """

    def __init__(self):
        """ Initialise object """
        self._auth = VtmGoAuth(kodiutils.get_setting('username'),
                               kodiutils.get_setting('password'),
                               'VTM',
                               kodiutils.get_setting('profile'),
                               kodiutils.get_tokens_path())
        self._vtm_go = VtmGo(self._auth)

    def show_channels(self):
        """ Shows TV channels """
        # Fetch EPG from API
        channels = self._vtm_go.get_live_channels()

        listing = []
        for channel in channels:
            channel_data = CHANNELS.get(channel.key)

            icon = channel.logo
            fanart = channel.background
            title = channel.name
            if channel_data:
                icon = '{path}/resources/logos/{logo}-white.png'.format(path=kodiutils.addon_path(), logo=channel.key)
                title = channel_data.get('label')

            context_menu = [(
                kodiutils.localize(30052, channel=title),  # Watch live {channel}
                'PlayMedia(%s)' %
                kodiutils.url_for('play', category='channels', item=channel.channel_id),
            )]

            if channel_data and channel_data.get('epg'):
                context_menu.append((
                    kodiutils.localize(30053, channel=title),  # TV Guide for {channel}
                    'Container.Update(%s)' %
                    kodiutils.url_for('show_tvguide_channel', channel=channel_data.get('epg'))
                ))

            context_menu.append((
                kodiutils.localize(30055, channel=title),  # Catalog for {channel}
                'Container.Update(%s)' %
                kodiutils.url_for('show_catalog_channel', channel=channel.key)
            ))

            if channel.epg:
                label = title + '[COLOR gray] | {title} ({start} - {end})[/COLOR]'.format(
                    title=channel.epg[0].title,
                    start=channel.epg[0].start.strftime('%H:%M'),
                    end=channel.epg[0].end.strftime('%H:%M'))
            else:
                label = title

            listing.append(kodiutils.TitleItem(
                title=label,
                path=kodiutils.url_for('show_channel_menu', channel=channel.key),
                art_dict=dict(
                    icon=icon,
                    thumb=icon,
                    fanart=fanart,
                ),
                info_dict=dict(
                    plot=Menu.format_plot(channel),
                    playcount=0,
                    mediatype='video',
                    studio=channel_data.get('studio_icon') if channel_data else None,
                ),
                stream_dict=dict(
                    codec='h264',
                    height=1080,
                    width=1920,
                ),
                context_menu=context_menu,
            ))

        kodiutils.show_listing(listing, 30007)

    def show_channel_menu(self, key):
        """ Shows a TV channel
        :type key: str
        """
        # Fetch EPG from API
        channel = self._vtm_go.get_live_channel(key)
        channel_data = CHANNELS.get(channel.key)

        icon = channel.logo
        fanart = channel.background
        title = channel.name
        if channel_data:
            icon = '{path}/resources/logos/{logo}-white.png'.format(path=kodiutils.addon_path(), logo=channel.key)
            title = channel_data.get('label')

        title = kodiutils.localize(30052, channel=title)  # Watch live {channel}
        if channel.epg:
            label = title + '[COLOR gray] | {title} ({start} - {end})[/COLOR]'.format(
                title=channel.epg[0].title,
                start=channel.epg[0].start.strftime('%H:%M'),
                end=channel.epg[0].end.strftime('%H:%M'))
        else:
            label = title

        # The .pvr suffix triggers some code paths in Kodi to mark this as a live channel
        listing = [kodiutils.TitleItem(
            title=label,
            path=kodiutils.url_for('play', category='channels', item=channel.channel_id) + '?.pvr',
            art_dict=dict(
                icon=icon,
                thumb=icon,
                fanart=fanart,
            ),
            info_dict=dict(
                plot=Menu.format_plot(channel),
                playcount=0,
                mediatype='video',
            ),
            stream_dict=dict(
                codec='h264',
                height=1080,
                width=1920,
            ),
            is_playable=True,
        )]

        if channel_data and channel_data.get('epg'):
            listing.append(
                kodiutils.TitleItem(
                    title=kodiutils.localize(30053, channel=channel_data.get('label')),  # TV Guide for {channel}
                    path=kodiutils.url_for('show_tvguide_channel', channel=channel_data.get('epg')),
                    art_dict=dict(
                        icon='DefaultAddonTvInfo.png',
                    ),
                    info_dict=dict(
                        plot=kodiutils.localize(30054, channel=channel_data.get('label')),  # Browse the TV Guide for {channel}
                    ),
                )
            )

        listing.append(kodiutils.TitleItem(
            title=kodiutils.localize(30055, channel=channel_data.get('label')),  # Catalog for {channel}
            path=kodiutils.url_for('show_catalog_channel', channel=key),
            art_dict=dict(
                icon='DefaultMovieTitle.png'
            ),
            info_dict=dict(
                plot=kodiutils.localize(30056, channel=channel_data.get('label')),  # Browse the Catalog for {channel}
            ),
        ))

        # Add YouTube channels
        if channel_data and kodiutils.get_cond_visibility('System.HasAddon(plugin.video.youtube)') != 0:
            for youtube in channel_data.get('youtube', []):
                listing.append(kodiutils.TitleItem(
                    title=kodiutils.localize(30206, label=youtube.get('label')),  # Watch {label} on YouTube
                    path=youtube.get('path'),
                    info_dict=dict(
                        plot=kodiutils.localize(30206, label=youtube.get('label')),  # Watch {label} on YouTube
                    )
                ))

        kodiutils.show_listing(listing, 30007, sort=['unsorted'])
示例#2
0
class Channels:
    """ Menu code related to channels """
    def __init__(self, kodi):
        """ Initialise object
        :type kodi: resources.lib.kodiwrapper.KodiWrapper
        """
        self._kodi = kodi
        self._vtm_go = VtmGo(self._kodi)
        self._menu = Menu(self._kodi)

    def show_channels(self):
        """ Shows TV channels """
        product = self._vtm_go.get_product()
        kids = (product == 'VTM_GO_KIDS')

        # Fetch EPG from API
        channel_infos = self._vtm_go.get_live_channels()

        listing = []
        for i, key in enumerate(CHANNELS):  # pylint: disable=unused-variable
            channel = CHANNELS[key]

            if kids and channel.get('kids') is False:
                continue

            # Find this channel in the list
            channel_info = next(c for c in channel_infos if c.key == key)

            # Lookup the high resolution logo based on the channel name
            icon = '{path}/resources/logos/{logo}-white.png'.format(
                path=self._kodi.get_addon_path(), logo=channel.get('logo'))
            fanart = '{path}/resources/logos/{logo}.png'.format(
                path=self._kodi.get_addon_path(), logo=channel.get('logo'))

            context_menu = [
                (
                    self._kodi.localize(
                        30052,
                        channel=channel.get('label')),  # Watch live {channel}
                    'XBMC.PlayMedia(%s)' %
                    self._kodi.url_for('play',
                                       category='channels',
                                       item=channel_info.channel_id)),
                (
                    self._kodi.localize(30053, channel=channel.get(
                        'label')),  # TV Guide for {channel}
                    'XBMC.Container.Update(%s)' % self._kodi.url_for(
                        'show_tvguide_channel', channel=channel.get('epg')))
            ]
            if self._kodi.get_setting_as_bool('metadata_update'):
                context_menu.append((
                    self._kodi.localize(
                        30055,
                        channel=channel.get('label')),  # Catalog for {channel}
                    'XBMC.Container.Update(%s)' %
                    self._kodi.url_for('show_catalog_channel', channel=key)))

            title = channel.get('label')
            if channel_info and channel_info.epg:
                title += '[COLOR gray] | {title} ({start} - {end})[/COLOR]'.format(
                    title=channel_info.epg[0].title,
                    start=channel_info.epg[0].start.strftime('%H:%M'),
                    end=channel_info.epg[0].end.strftime('%H:%M'))

            listing.append(
                TitleItem(title=title,
                          path=self._kodi.url_for('show_channel_menu',
                                                  channel=key),
                          art_dict={
                              'icon': icon,
                              'thumb': icon,
                              'fanart': fanart,
                          },
                          info_dict={
                              'plot': self._menu.format_plot(channel),
                              'playcount': 0,
                              'mediatype': 'video',
                              'studio': channel.get('studio_icon'),
                          },
                          stream_dict={
                              'codec': 'h264',
                              'height': 1080,
                              'width': 1920,
                          },
                          context_menu=context_menu), )

        self._kodi.show_listing(listing, 30007)

    def show_channel_menu(self, key):
        """ Shows a TV channel
        :type key: str
        """
        channel = CHANNELS[key]

        # Fetch EPG from API
        channel_info = self._vtm_go.get_live_channel(key)

        title = self._kodi.localize(
            30052, channel=channel.get('label'))  # Watch live {channel}
        if channel_info.epg:
            title += '[COLOR gray] | {title} ({start} - {end})[/COLOR]'.format(
                title=channel_info.epg[0].title,
                start=channel_info.epg[0].start.strftime('%H:%M'),
                end=channel_info.epg[0].end.strftime('%H:%M'))

        # Lookup the high resolution logo based on the channel name
        icon = '{path}/resources/logos/{logo}-white.png'.format(
            path=self._kodi.get_addon_path(), logo=channel.get('logo'))
        fanart = '{path}/resources/logos/{logo}.png'.format(
            path=self._kodi.get_addon_path(), logo=channel.get('logo'))

        listing = [
            TitleItem(title=title,
                      path=self._kodi.url_for('play',
                                              category='channels',
                                              item=channel_info.channel_id) +
                      '?.pvr',
                      art_dict={
                          'icon': icon,
                          'thumb': icon,
                          'fanart': fanart,
                      },
                      info_dict={
                          'plot': self._menu.format_plot(channel_info),
                          'playcount': 0,
                          'mediatype': 'video',
                      },
                      stream_dict={
                          'codec': 'h264',
                          'height': 1080,
                          'width': 1920,
                      },
                      is_playable=True),
            TitleItem(
                title=self._kodi.localize(
                    30053,
                    channel=channel.get('label')),  # TV Guide for {channel}
                path=self._kodi.url_for('show_tvguide_channel',
                                        channel=channel.get('epg')),
                art_dict={'icon': 'DefaultAddonTvInfo.png'},
                info_dict={
                    'plot':
                    self._kodi.localize(30054, channel=channel.get(
                        'label')),  # Browse the TV Guide for {channel}
                }),
        ]

        if self._kodi.get_setting_as_bool('metadata_update'):
            listing.append(
                TitleItem(
                    title=self._kodi.localize(
                        30055,
                        channel=channel.get('label')),  # Catalog for {channel}
                    path=self._kodi.url_for('show_catalog_channel',
                                            channel=key),
                    art_dict={'icon': 'DefaultMovieTitle.png'},
                    info_dict={
                        'plot':
                        self._kodi.localize(30056,
                                            channel=channel.get('label')),
                    }))

            # Add YouTube channels
        if self._kodi.get_cond_visibility(
                'System.HasAddon(plugin.video.youtube)') != 0:
            for youtube in channel.get('youtube', []):
                listing.append(
                    TitleItem(
                        title=self._kodi.localize(
                            30206, label=youtube.get(
                                'label')),  # Watch {label} on YouTube
                        path=youtube.get('path'),
                        info_dict={
                            'plot':
                            self._kodi.localize(
                                30206, label=youtube.get(
                                    'label')),  # Watch {label} on YouTube
                        }))

        self._kodi.show_listing(listing, 30007, sort=['unsorted'])
示例#3
0
class IPTVManager:
    """ Code related to the Kodi PVR integration """
    def __init__(self, port):
        """ Initialise object
        :type port: int
        """
        self._vtm_go = VtmGo()
        self._vtm_go_epg = VtmGoEpg()
        self.port = port

    def via_socket(func):  # pylint: disable=no-self-argument
        """Send the output of the wrapped function to socket"""
        def send(self):
            """Decorator to send over a socket"""
            sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            sock.connect(('127.0.0.1', self.port))
            try:
                sock.sendall(json.dumps(func(self)).encode())  # pylint: disable=not-callable
            finally:
                sock.close()

        return send

    @via_socket
    def send_channels(self):
        """ Report channel data """
        # Fetch EPG from API
        channels = self._vtm_go.get_live_channels()

        results = []
        for channel in channels:
            channel_data = CHANNELS.get(channel.key)

            if not channel_data:
                _LOGGER.warning(
                    'Skipping %s since we don\'t know this channel',
                    channel.key)
                continue

            results.append(
                dict(
                    name=channel_data.get('label')
                    if channel_data else channel.name,
                    id=channel_data.get('iptv_id'),
                    preset=channel_data.get('iptv_preset'),
                    logo=
                    'special://home/addons/{addon}/resources/logos/{logo}.png'.
                    format(addon=kodiutils.addon_id(), logo=channel.key)
                    if channel_data else channel.logo,
                    stream=kodiutils.url_for('play',
                                             category='channels',
                                             item=channel.channel_id),
                    vod=kodiutils.url_for('play_epg_datetime',
                                          channel=channel.key,
                                          timestamp='{date}'),
                ))

        return dict(version=1, streams=results)

    @via_socket
    def send_epg(self):
        """ Report EPG data """
        results = dict()

        # Fetch EPG data
        for date in ['yesterday', 'today', 'tomorrow']:

            channels = self._vtm_go_epg.get_epgs(date)
            for channel in channels:
                # Lookup channel data in our own CHANNELS dict
                channel_data = next((c for c in CHANNELS.values()
                                     if c.get('epg') == channel.key), None)
                if not channel_data:
                    _LOGGER.warning(
                        'Skipping EPG for %s since we don\'t know this channel',
                        channel.key)
                    continue

                key = channel_data.get('iptv_id')

                # Create channel in dict if it doesn't exists
                if key not in results.keys():
                    results[key] = []

                results[key].extend([
                    dict(
                        start=broadcast.time.isoformat(),
                        stop=(
                            broadcast.time +
                            timedelta(seconds=broadcast.duration)).isoformat(),
                        title=broadcast.title,
                        description=broadcast.description,
                        # subtitle=None,  # Not available in the API
                        # season=None,  # Not available in the API
                        # epsiode=None,  # Not available in the API
                        genre=broadcast.genre,
                        image=broadcast.thumb,
                        stream=kodiutils.url_for(
                            'play',
                            category=broadcast.playable_type,
                            item=broadcast.playable_uuid)
                        if broadcast.playable_uuid else None)
                    for broadcast in channel.broadcasts
                ])

        return dict(version=1, epg=results)