initial commit
No real output to terminal yet, but spotify api works. mostly.
This commit is contained in:
@@ -0,0 +1,13 @@
|
||||
from django.contrib import admin
|
||||
from .models import Track, Artist, Album
|
||||
|
||||
class TrackAdmin(admin.ModelAdmin):
|
||||
pass
|
||||
class ArtistAdmin(admin.ModelAdmin):
|
||||
pass
|
||||
class AlbumAdmin(admin.ModelAdmin):
|
||||
pass
|
||||
|
||||
admin.site.register(Track, TrackAdmin)
|
||||
admin.site.register(Artist, ArtistAdmin)
|
||||
admin.site.register(Album, AlbumAdmin)
|
||||
@@ -0,0 +1,6 @@
|
||||
from django.apps import AppConfig
|
||||
|
||||
|
||||
class PlaylistConfig(AppConfig):
|
||||
default_auto_field = 'django.db.models.BigAutoField'
|
||||
name = 'playlist'
|
||||
@@ -0,0 +1,30 @@
|
||||
from django.db import models
|
||||
|
||||
class Artist(models.Model):
|
||||
id = models.CharField(primary_key=True, max_length=128)
|
||||
name = models.CharField(max_length=255)
|
||||
popularity = models.SmallIntegerField(null=True, blank=True)
|
||||
genres = models.JSONField()
|
||||
|
||||
class Album(models.Model):
|
||||
id = models.CharField(primary_key=True, max_length=128)
|
||||
name = models.CharField(max_length=255)
|
||||
artists = models.ManyToManyField(Artist)
|
||||
album_type = models.CharField(max_length=255)
|
||||
total_tracks = models.PositiveSmallIntegerField(null=True)
|
||||
image = models.ImageField(upload_to="albums/")
|
||||
|
||||
|
||||
class Track(models.Model):
|
||||
id = models.CharField(primary_key=True, max_length=128)
|
||||
title = models.CharField(max_length=255)
|
||||
artists = models.ManyToManyField(Artist)
|
||||
album = models.ForeignKey(Album, null=True, on_delete=models.SET_NULL)
|
||||
|
||||
explicit = models.BooleanField(default=False)
|
||||
duration_ms = models.PositiveIntegerField(null=True)
|
||||
popularity = models.PositiveIntegerField(null=True)
|
||||
|
||||
banter = models.TextField()
|
||||
|
||||
#class TrackFeatures(models.Model):
|
||||
@@ -0,0 +1,45 @@
|
||||
import requests
|
||||
import os
|
||||
from datetime import datetime, timedelta
|
||||
from dotenv import load_dotenv
|
||||
|
||||
load_dotenv()
|
||||
|
||||
class SpotifyFactory():
|
||||
def __init__(self):
|
||||
self.token = None
|
||||
self.valid_until = None
|
||||
|
||||
def token_valid(self):
|
||||
if self.token and self.valid_until and (datetime.now() < self.valid_until):
|
||||
return True
|
||||
return False
|
||||
|
||||
def get_token(self):
|
||||
if self.token_valid():
|
||||
print("Token still valid")
|
||||
return self.token
|
||||
print("New token")
|
||||
url = 'https://accounts.spotify.com/api/token'
|
||||
headers = {
|
||||
'Content-Type': 'application/x-www-form-urlencoded',
|
||||
'Authorization': 'Basic ' + os.getenv('SPOTIFY_ENCODED_ID')
|
||||
}
|
||||
data = {
|
||||
'grant_type': 'client_credentials'
|
||||
}
|
||||
r = requests.post(url, headers=headers, data=data)
|
||||
data = r.json()
|
||||
self.token = data["access_token"]
|
||||
self.valid_until = datetime.now() + timedelta(seconds=data["expires_in"]-300)
|
||||
return self.token
|
||||
|
||||
def get_song_info(self, spotify_id):
|
||||
token = self.get_token()
|
||||
url = 'https://api.spotify.com/v1/tracks/' + spotify_id
|
||||
headers = {
|
||||
'Content-Type': 'application/json',
|
||||
'Authorization': 'Bearer ' + token
|
||||
}
|
||||
r = requests.get(url, headers=headers)
|
||||
return r.json()
|
||||
@@ -0,0 +1,6 @@
|
||||
from django.urls import path
|
||||
from . import views
|
||||
|
||||
urlpatterns = [
|
||||
path('tracks/', views.TrackView.as_view()),
|
||||
]
|
||||
@@ -0,0 +1,11 @@
|
||||
from django.shortcuts import render
|
||||
from django.views import View
|
||||
from django.http import HttpResponse
|
||||
from . import models
|
||||
from . import spotify
|
||||
|
||||
class TrackView(View):
|
||||
def get(self, request):
|
||||
return HttpResponse("Hello world!")
|
||||
def post(self, request):
|
||||
return HttpResponse("Bye world")
|
||||
Reference in New Issue
Block a user