예제 #1
0
def listen_now():
    ifl = xbmcgui.ListItem(utils.translate(30045))
    ifl.setArt({'thumb': thumbs.IMG_IFL, 'poster': thumbs.IMG_IFL})

    albums = xbmcgui.ListItem(utils.translate(30023))
    albums.setArt({'thumb': thumbs.IMG_ALBUM, 'poster': thumbs.IMG_ALBUM})

    stations = xbmcgui.ListItem(utils.translate(30021))
    stations.setArt({
        'thumb': thumbs.IMG_STATION,
        'poster': thumbs.IMG_STATION
    })

    playlists = xbmcgui.ListItem(utils.translate(30020))
    playlists.setArt({
        'thumb': thumbs.IMG_PLAYLIST,
        'poster': thumbs.IMG_PLAYLIST
    })

    items = [
        (utils.build_url(url=URL,
                         paths=['play', 'station'],
                         queries={'station_id': 'IFL'},
                         r_path=True,
                         r_query=True), ifl, False),
        (utils.build_url(URL, ['albums']), albums, True),
        (utils.build_url(URL, ['stations']), stations, True),
        (utils.build_url(URL, ['playlists']), playlists, True),
    ]

    # Only fetch new information if one full hour has passed
    # to keep things speedy on slow devices
    try:
        last_check = ADDON.getSetting('listen_now_last_update')

    except:
        last_check = -1

    from_cache = True
    if last_check != time.strftime('%Y%m%d%H'):
        from_cache = False
        ADDON.setSetting('listen_now_last_update', time.strftime('%Y%m%d%H'))

    primary_header, situations = gmusic.get_listen_now_situations(from_cache)

    if primary_header and situations:
        situations = xbmcgui.ListItem(primary_header)
        situations.setArt({
            'thumb': thumbs.IMG_ALBUM,
            'poster': thumbs.IMG_ALBUM
        })

        # Add Situations after IFL
        items.insert(1,
                     (utils.build_url(URL, ['situations']), situations, True))

    listing.list_items(items)
    def login(self):
        # Set Kodis locale to super class
        locale_code = xbmc.getLanguage(xbmc.ISO_639_1)
        locale_code = locale.normalize(locale_code).split('.')[0]
        if not locale_code:
            locale_code = 'en_US'

        self.locale = locale_code

        if self._is_logged_in and not self._should_test_login():
            return True

        username = ADDON.getSetting('username')
        password = ADDON.getSetting('password')
        device_id = ADDON.getSetting('device_id')
        authtoken = ADDON.getSetting('authtoken')

        if authtoken:
            self.android_id = device_id
            self.session._authtoken = authtoken
            self.session.is_authenticated = True

            ADDON.setSetting('last_login_check', str(int(time.time())))
            try:
                # Send a test request to ensure our authtoken
                # is still valide and working
                self.get_registered_devices()
                self._is_logged_in = True
                return True

            except:
                # Faild with the test-request so we set
                # "is_authenticated=False" and go through the login-process
                # again to get a new "authtoken"
                self.session.is_authenticated = False

        if device_id:
            success = super(GMusic, self).login(username, password, device_id,
                                                self.locale)

            if success:
                ADDON.setSetting('authtoken', self.session._authtoken)
                self._is_logged_in = True
                return True

        utils.notify(utils.translate(30048), '')
        ADDON.setSetting('is_setup', 'false')

        # Prevent further addon execution in case we failed with the login-process
        raise SystemExit
    except Exception:
        library_last_updated = 0

    return library_last_updated


if __name__ == '__main__':
    monitor = xbmc.Monitor()
    while not monitor.abortRequested():
        if monitor.waitForAbort(30):
            # Abort was requested while waiting. We should exit
            break

        update_interval = _get_update_interval()
        library_last_updated = _get_library_last_updated()

        if update_interval == 0:
            continue

        if time.time() >= library_last_updated + update_interval:
            ADDON.setSetting('library_last_updated', str(int(time.time())))

            try:
                if GMUSIC.login():
                    GMUSIC.get_my_library_songs(from_cache=False)
                    GMUSIC.get_my_library_artists(from_cache=False)
                    GMUSIC.get_my_library_albums(from_cache=False)

            except Exception:
                continue
예제 #4
0
def setup(force=False):
    is_setup = True if ADDON.getSetting('is_setup') == 'true' else False

    if is_setup and not force:
        return True

    dialog = xbmcgui.Dialog()

    username = dialog.input(utils.translate(30075),
                            type=xbmcgui.INPUT_ALPHANUM)
    if not username:
        return False

    # If 2-Factor Authentication is used
    is_two_factor = dialog.yesno(utils.translate(30071),
                                 utils.translate(30072))
    if is_two_factor:
        if not dialog.ok(utils.translate(30071), utils.translate(30073),
                         utils.translate(30074)):
            return False

    password = dialog.input(utils.translate(30076),
                            type=xbmcgui.INPUT_ALPHANUM,
                            option=xbmcgui.ALPHANUM_HIDE_INPUT)
    if not password:
        return False

    device_id = None
    if is_two_factor:
        # If Android Device available
        if dialog.yesno(utils.translate(30077), utils.translate(30078)):
            if not dialog.ok(utils.translate(30079), utils.translate(30081)):
                return False

            device_id = dialog.input(utils.translate(30084),
                                     type=xbmcgui.INPUT_ALPHANUM)
            if not device_id:
                return False
        else:
            # If using MAC-Address
            if dialog.yesno(utils.translate(30082), utils.translate(30083)):
                device_id = gmusicapi.Mobileclient.FROM_MAC_ADDRESS
            else:
                return False
    else:
        web = gmusicapi.Webclient()
        if not web.login(username, password):
            # If re-run setup due to login failed
            if dialog.yesno(utils.translate(30048), utils.translate(30085)):
                return setup(force=True)
            else:
                return False

        try:
            devices = web.get_registered_devices()
            if not devices:
                raise

            dev_list = []
            for dev in devices:
                # Not an Android Device so we skip as streaming would not work
                if dev['deviceType'] != 2:
                    continue

                if 'id' in dev and dev['id']:
                    dev_list.append('%s - %s' % (
                        dev.get('carrier', '').strip(' '),
                        dev.get('model', '').strip(' '),
                    ))
                    dev_list = sorted(dev_list)

            if len(dev_list) <= 0:
                raise

            elif len(dev_list) == 1:
                device_id = devices[0]['id'].lstrip('0x')

            else:
                selection = dialog.select(utils.translate(30042), dev_list, 0)

                if selection >= 0:
                    device_id = devices[selection]['id'].lstrip('0x')

                else:
                    return False

        except Exception:
            # If use MAC-Address instead due to no devices found
            if not dialog.yesno(utils.translate(30079),
                                utils.translate(30097)):
                return False

            device_id = gmusicapi.Mobileclient.FROM_MAC_ADDRESS

    # Test login
    mobile = gmusicapi.Mobileclient()
    if mobile.login(username, password, device_id):

        # Test if this is an all-access account
        if not mobile.get_all_stations():
            dialog.ok(utils.translate(30091), utils.translate(30092))
            return False

        ADDON.setSetting('username', username)
        ADDON.setSetting('password', password)
        ADDON.setSetting('authtoken', mobile.session._authtoken)

        if device_id == gmusicapi.Mobileclient.FROM_MAC_ADDRESS:
            mac_address = ''.join(re.findall('..', '%012x' % uuid.getnode()))
            ADDON.setSetting('device_id', mac_address)
        else:
            ADDON.setSetting('device_id', device_id)

        ADDON.setSetting('is_setup', 'true')

        utils.notify(utils.translate(30086), utils.translate(30087))

        return True

    else:
        # If re-run setup
        if dialog.yesno(utils.translate(30048), utils.translate(30085)):
            return setup(force=True)

        return False