diff options
| author | Fuwn <[email protected]> | 2023-01-02 21:25:40 -0800 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2023-01-02 21:25:40 -0800 |
| commit | 440d3c29ddb0e9f7ba20f603834aae9af1a5a4d6 (patch) | |
| tree | 30a939cb3eb6488abb1e5107c69e34d377e4cb9f | |
| parent | fix(spotify.py): headers (diff) | |
| download | rubys_song_skipper-440d3c29ddb0e9f7ba20f603834aae9af1a5a4d6.tar.xz rubys_song_skipper-440d3c29ddb0e9f7ba20f603834aae9af1a5a4d6.zip | |
feat: auto refresh
| -rw-r--r-- | rubys_song_skipper/api.py | 13 | ||||
| -rw-r--r-- | rubys_song_skipper/bot.py | 30 | ||||
| -rw-r--r-- | rubys_song_skipper/spotify.py | 24 |
3 files changed, 55 insertions, 12 deletions
diff --git a/rubys_song_skipper/api.py b/rubys_song_skipper/api.py index 63f8e79..4b6d3a8 100644 --- a/rubys_song_skipper/api.py +++ b/rubys_song_skipper/api.py @@ -22,11 +22,20 @@ def login(): @app.route("/callback") def callback(): with open("code.txt", "w") as file: - file.write(spotify.bearer(request.args.get("code"))[0]) + api = spotify.bearer(request.args.get("code")) + + if api == None: + return ( + "an error has occurred. <a href='/login'>click here to try again</a>." + + "<br />" + + "<img src='https://c.tenor.com/fUgB2dIp1BEAAAAC/tenor.gif" + ) + + file.write(f"{api[0]}\n{api[1]}\n{api[2]}") file.close() return ( - "thanks" + "authorization has been completed." + "<br />" + "<img src='https://gifimage.net/wp-content/uploads/2017/09/anime-waving-gif-4.gif'>" ) diff --git a/rubys_song_skipper/bot.py b/rubys_song_skipper/bot.py index 06590e9..a40f459 100644 --- a/rubys_song_skipper/bot.py +++ b/rubys_song_skipper/bot.py @@ -1,7 +1,8 @@ from twitchio.ext import commands import requests import twitch -import api +import time +import spotify class Bot(commands.Bot): @@ -22,19 +23,32 @@ class Bot(commands.Bot): @commands.command(name="skip") async def skip(self, ctx): with open("code.txt", "r") as file: - code = file.read() + empty = file.read() + data = file.readlines() - if code == "": - await ctx.send("no authorization found, please sign in ~uwu~") + access = data[0].strip() + refresh = data[1].strip() + expire = data[2].strip() + + if time.time() > expire: + file.close() + + refreshed = spotify.refresh_bearer(refresh) + access = refreshed[0] + new_expire = refreshed[1] + + with open("code.txt", "w") as file: + file.write(f"{access}\n{refresh}\n{new_expire}") + + if empty == "": + await ctx.send("i'm not authorized to do that. sign in.") else: requests.post( "https://api.spotify.com/v1/me/player/next", headers={ - "Authorization": "Bearer " + code, + "Authorization": "Bearer " + access, "Content-Type": "application/json", }, ) - await ctx.send("i've skipped that song ~uwu~") - - file.close() + await ctx.send("i've skipped that song.") diff --git a/rubys_song_skipper/spotify.py b/rubys_song_skipper/spotify.py index 19f8d6b..ac3455a 100644 --- a/rubys_song_skipper/spotify.py +++ b/rubys_song_skipper/spotify.py @@ -5,14 +5,34 @@ import base64 CLIENT_ID = "3b278200b722431488a0e70111700211" CLIENT_SECRET = "0689cf819adb442a9abddce154f58cad" -# AUTHORIZATION = "M2IyNzgyMDBiNzIyNDMxNDg4YTBlNzAxMTE3MDAyMTE6MDY4OWNmODE5YWRiNDQyYTlhYmRkY2UxNTRmNThjYWQ=" + + +def refresh_bearer(refresh_token): + authorization = requests.post( + "https://accounts.spotify.com/api/token", + headers={ + "Authorization": f"Basic {base64.urlsafe_b64encode(f'{CLIENT_ID}:{CLIENT_SECRET}'.encode()).decode()}", + "Accept": "application/json", + "Content-Type": "application/x-www-form-urlencoded", + }, + data={ + "grant_type": "refresh_token", + "refresh_token": refresh_token, + }, + ) + + if authorization.status_code == 200: + json = authorization.json() + + return (json["access_token"], json["expires_in"]) + else: + return None def bearer(code): authorization = requests.post( "https://accounts.spotify.com/api/token", headers={ - # "Authorization": f"Basic {AUTHORIZATION}", "Authorization": f"Basic {base64.urlsafe_b64encode(f'{CLIENT_ID}:{CLIENT_SECRET}'.encode()).decode()}", "Accept": "application/json", "Content-Type": "application/x-www-form-urlencoded", |