def get_tokens(self, use_refresh=False): """Makes an api request to get an oauth & refresh token.""" get_params = { "client_id": TWITCH_CLIENT_ID, "client_secret": TWITCH_CLIENT_SECRET, "grant_type": "refresh_token" if use_refresh else "authorization_code" } if use_refresh: get_params["refresh_token"] = get_value(TWITCH_REFRESH_TOKEN) else: get_params["code"] = get_value(TWITCH_AUTHORIZATION_CODE) get_params["redirect_uri"] = REDIRECT_URL r = requests.post(TOKEN_URL, params=get_params) if r.status_code != requests.codes.ok: return self.is_authorized = True data = r.json() set_value(TWITCH_ACCESS_TOKEN, data["access_token"]) self.access_token = data["access_token"] if "refresh_token" in data: set_value(TWITCH_REFRESH_TOKEN, data["refresh_token"]) return
def get_tokens(self, use_refresh=False): """Makes an api request to get an oauth & refresh token.""" post_data = { "grant_type": "refresh_token" if use_refresh else "authorization_code", "code": get_value(SPOTIFY_AUTHORIZATION_CODE), "redirect_uri": REDIRECT_URL } if use_refresh: post_data["refresh_token"] = get_value(SPOTIFY_REFRESH_TOKEN) auth_key = base64.urlsafe_b64encode(f"{SPOTIFY_CLIENT_ID}:{SPOTIFY_CLIENT_SECRET}".encode()).decode() r = requests.post( TOKEN_URL, headers={ "Accept": "application/json", "Content-Type": "application/x-www-form-urlencoded", "Authorization": f"Basic {auth_key}" }, data="&".join([f"{quote(key)}={quote(value)}" for key, value in post_data.items()]) ) if r.status_code != requests.codes.ok: return self.is_authorized = True data = r.json() set_value(SPOTIFY_ACCESS_TOKEN, data["access_token"]) self.access_token = data["access_token"] if "refresh_token" in data: set_value(SPOTIFY_REFRESH_TOKEN, data["refresh_token"]) return
def toggle_spotify_polling(request): """Updates the polling value in both the service & key value store.""" app_config = apps.get_app_config("api") spotify_service = app_config.spotify_service set_value(SPOTIFY_SHOULD_POLL, not spotify_service.should_poll) spotify_service.should_poll = not spotify_service.should_poll return JsonResponse({"status": "cool"})
def increment_page(self, positive=True): """Increments the current page value. We need to make sure to reset to 0 when we have no valid keybindings anymore. I.e. We have 37 keybindings and the page is set to 4. A page set to 4 implies a +40 to the index value aka we would never hit a valid keybinding. """ max_page = (Sound.objects.count() - 1) // 10 page = get_value(KEYBINDING_PAGE) + (1 if positive else -1) if page < 0: page = max_page elif page > max_page: page = 0 set_value(KEYBINDING_PAGE, page) self.notify_socket(page)
def twitch_authorization(request): """Gets twitch authorization code and puts it in the db.""" logger.info(request.GET.get("code")) set_value(TWITCH_AUTHORIZATION_CODE, request.GET.get("code")) return HttpResponse("Got it boss")
def spotify_authorization(request): """Get spotify authorization code and put it in the db.""" set_value(SPOTIFY_AUTHORIZATION_CODE, request.GET.get("code")) return HttpResponse("Got it boss")