Nomineren even opgeleukt

En spotify.link shortlinks worden geaccepteerd. Mogelijk.
This commit is contained in:
2024-02-25 00:20:44 +01:00
parent b9e0177c98
commit 7f199b6886
6 changed files with 61 additions and 11 deletions
+8
View File
@@ -1,5 +1,6 @@
import requests
import os
import re
from datetime import datetime, timedelta
from dotenv import load_dotenv
@@ -34,6 +35,12 @@ class Spotify():
self.valid_until = datetime.now() + timedelta(seconds=data["expires_in"]-300)
return self.token
def unshort(self, url):
"""Converts a spotify.link url into a normal spotify ID"""
r = requests.get(url, allow_redirects=False)
r.raise_for_status()
return r.headers["Location"]
def get_song_info(self, spotify_id):
token = self.get_token()
url = 'https://api.spotify.com/v1/tracks/' + spotify_id
@@ -42,4 +49,5 @@ class Spotify():
'Authorization': 'Bearer ' + token
}
r = requests.get(url, headers=headers)
r.raise_for_status()
return r.json()
+2 -1
View File
@@ -19,7 +19,8 @@ body {
#bsod {
display: block;
width: 70vw;
width: max(70vw, 400px);
max-width: 800px;
flex-shrink: 1;
gap: 1rem;
display: flex;
Binary file not shown.

After

Width:  |  Height:  |  Size: 394 B

+16
View File
@@ -30,6 +30,22 @@ input[type=submit] {
filter: contrast(1);
}
input#spotify-input {
width: 100%;
margin-bottom: 10px;
}
input#spotify-input + input[type=submit]{
background-image: url(img/sound.png);
background-repeat:no-repeat;
padding: 4px 8px 4px 24px;
height: 16px;
min-width: 0;
background-position: 4px 50%;
font-weight: bold;
}
#bsod, .hide {
display: none;
}
+15 -6
View File
@@ -7,19 +7,28 @@ Nomineren
{% block main-content %}
<form method="post">
{% csrf_token %}
<input type="text" name="spotify_link" placeholder="Spotify-link">
<input type="submit" value="OK">
<p>Vul een spotify link in om een nummer te nomineren.</p>
<p>Deze link mag zowel naar open.spotify.com gaan als naar spotify.link</p>
<input type="text" id="spotify-input" name="spotify_link" placeholder="Spotify-link">
<input type="submit" value="Insturen">
</form>
{% if nominated %}
{% if nominated or error or warning %}
<script>
function hidePopup() {
console.log("je moeder")
document.getElementById("info-window").classList.add("hide");
}
</script>
<div class="window popup" id="info-window">
<div class="title-bar">
<div class="title-bar-text">{{nominated}}</div>
<div class="title-bar-text">
{% if nominated %}
{{nominated}}
{% elif error %}
Error
{% else %}
Let op!
{% endif %}
</div>
<div class="title-bar-controls">
<button aria-label="Close" onclick="hidePopup()"></button>
</div>
@@ -30,7 +39,7 @@ Nomineren
<div>
{{warning}}
{{error}}
{% if not warning and not error %}Genomineerd!{% endif %}
{% if not error %}Genomineerd!{% endif %}
</div>
<section class="field-row" style="justify-content: flex-end">
<button onclick="hidePopup()">Ok</button>
+17 -1
View File
@@ -45,13 +45,23 @@ class NominateView(LoginRequiredMixin, View):
def get(self, request):
return TemplateResponse(request, "Nominate.html", {})
def post(self, request):
try:
spotify_link = self.request.POST['spotify_link']
m = re.match(r"^https:\/\/open.spotify.com\/track\/(\w+)\??", spotify_link)
# Short url?
if re.match(r"^https:\/\/spotify.link.*", spotify_link):
spotify_link = spt.unshort(spotify_link)
m = re.match(r"^https:\/\/open.spotify.com\/track\/([a-zA-Z0-9]+)\??", spotify_link)
spotify_id = m.group(1)
except:
return TemplateResponse(request, "Nominate.html", {"error": "Geef een geldige spotify link op!"})
try:
track = Track.all_tracks.get(pk=spotify_id)
if track.old:
track.old = False
track.save()
return TemplateResponse(request, "Nominate.html", {
"warning": "Hey een klassieker.",
"nominated": track
@@ -61,7 +71,13 @@ class NominateView(LoginRequiredMixin, View):
"nominated": track
})
except Track.DoesNotExist:
try:
json = spt.get_song_info(spotify_id)
except:
return TemplateResponse(request, "Nominate.html", {"error": "Fout bij het ophalen van het liedje."})
print(json)
print("hey")
# Album artists
album_artists = []