Nomineren even opgeleukt
En spotify.link shortlinks worden geaccepteerd. Mogelijk.
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
import requests
|
import requests
|
||||||
import os
|
import os
|
||||||
|
import re
|
||||||
from datetime import datetime, timedelta
|
from datetime import datetime, timedelta
|
||||||
from dotenv import load_dotenv
|
from dotenv import load_dotenv
|
||||||
|
|
||||||
@@ -34,6 +35,12 @@ class Spotify():
|
|||||||
self.valid_until = datetime.now() + timedelta(seconds=data["expires_in"]-300)
|
self.valid_until = datetime.now() + timedelta(seconds=data["expires_in"]-300)
|
||||||
return self.token
|
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):
|
def get_song_info(self, spotify_id):
|
||||||
token = self.get_token()
|
token = self.get_token()
|
||||||
url = 'https://api.spotify.com/v1/tracks/' + spotify_id
|
url = 'https://api.spotify.com/v1/tracks/' + spotify_id
|
||||||
@@ -42,4 +49,5 @@ class Spotify():
|
|||||||
'Authorization': 'Bearer ' + token
|
'Authorization': 'Bearer ' + token
|
||||||
}
|
}
|
||||||
r = requests.get(url, headers=headers)
|
r = requests.get(url, headers=headers)
|
||||||
|
r.raise_for_status()
|
||||||
return r.json()
|
return r.json()
|
||||||
|
|||||||
@@ -19,7 +19,8 @@ body {
|
|||||||
|
|
||||||
#bsod {
|
#bsod {
|
||||||
display: block;
|
display: block;
|
||||||
width: 70vw;
|
width: max(70vw, 400px);
|
||||||
|
max-width: 800px;
|
||||||
flex-shrink: 1;
|
flex-shrink: 1;
|
||||||
gap: 1rem;
|
gap: 1rem;
|
||||||
display: flex;
|
display: flex;
|
||||||
|
|||||||
Binary file not shown.
|
After Width: | Height: | Size: 394 B |
@@ -30,6 +30,22 @@ input[type=submit] {
|
|||||||
filter: contrast(1);
|
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 {
|
#bsod, .hide {
|
||||||
display: none;
|
display: none;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,19 +7,28 @@ Nomineren
|
|||||||
{% block main-content %}
|
{% block main-content %}
|
||||||
<form method="post">
|
<form method="post">
|
||||||
{% csrf_token %}
|
{% csrf_token %}
|
||||||
<input type="text" name="spotify_link" placeholder="Spotify-link">
|
<p>Vul een spotify link in om een nummer te nomineren.</p>
|
||||||
<input type="submit" value="OK">
|
<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>
|
</form>
|
||||||
{% if nominated %}
|
{% if nominated or error or warning %}
|
||||||
<script>
|
<script>
|
||||||
function hidePopup() {
|
function hidePopup() {
|
||||||
console.log("je moeder")
|
|
||||||
document.getElementById("info-window").classList.add("hide");
|
document.getElementById("info-window").classList.add("hide");
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
<div class="window popup" id="info-window">
|
<div class="window popup" id="info-window">
|
||||||
<div class="title-bar">
|
<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">
|
<div class="title-bar-controls">
|
||||||
<button aria-label="Close" onclick="hidePopup()"></button>
|
<button aria-label="Close" onclick="hidePopup()"></button>
|
||||||
</div>
|
</div>
|
||||||
@@ -30,7 +39,7 @@ Nomineren
|
|||||||
<div>
|
<div>
|
||||||
{{warning}}
|
{{warning}}
|
||||||
{{error}}
|
{{error}}
|
||||||
{% if not warning and not error %}Genomineerd!{% endif %}
|
{% if not error %}Genomineerd!{% endif %}
|
||||||
</div>
|
</div>
|
||||||
<section class="field-row" style="justify-content: flex-end">
|
<section class="field-row" style="justify-content: flex-end">
|
||||||
<button onclick="hidePopup()">Ok</button>
|
<button onclick="hidePopup()">Ok</button>
|
||||||
|
|||||||
+20
-4
@@ -45,13 +45,23 @@ class NominateView(LoginRequiredMixin, View):
|
|||||||
def get(self, request):
|
def get(self, request):
|
||||||
return TemplateResponse(request, "Nominate.html", {})
|
return TemplateResponse(request, "Nominate.html", {})
|
||||||
def post(self, request):
|
def post(self, request):
|
||||||
spotify_link = self.request.POST['spotify_link']
|
try:
|
||||||
m = re.match(r"^https:\/\/open.spotify.com\/track\/(\w+)\??", spotify_link)
|
spotify_link = self.request.POST['spotify_link']
|
||||||
spotify_id = m.group(1)
|
|
||||||
|
# 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:
|
try:
|
||||||
track = Track.all_tracks.get(pk=spotify_id)
|
track = Track.all_tracks.get(pk=spotify_id)
|
||||||
|
|
||||||
if track.old:
|
if track.old:
|
||||||
|
track.old = False
|
||||||
|
track.save()
|
||||||
return TemplateResponse(request, "Nominate.html", {
|
return TemplateResponse(request, "Nominate.html", {
|
||||||
"warning": "Hey een klassieker.",
|
"warning": "Hey een klassieker.",
|
||||||
"nominated": track
|
"nominated": track
|
||||||
@@ -61,7 +71,13 @@ class NominateView(LoginRequiredMixin, View):
|
|||||||
"nominated": track
|
"nominated": track
|
||||||
})
|
})
|
||||||
except Track.DoesNotExist:
|
except Track.DoesNotExist:
|
||||||
json = spt.get_song_info(spotify_id)
|
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
|
||||||
album_artists = []
|
album_artists = []
|
||||||
|
|||||||
Reference in New Issue
Block a user