-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7ae1a25
commit 651333a
Showing
25 changed files
with
329 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Binary file modified
BIN
-15 Bytes
(97%)
Read+Django/music_app/music_controller/frontend/__pycache__/urls.cpython-311.pyc
Binary file not shown.
32 changes: 32 additions & 0 deletions
32
Read+Django/music_app/music_controller/frontend/src/components/MusicPlayer.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import React, { Component } from "react"; | ||
import {Grid, Typography, Card, Iconbutton} from "@material-ui/core" | ||
import { PlayArrwIcon, SkipNextIcon, PauseIcon} from "@material-ui/icons" | ||
|
||
export default class MusicPlayer extends Component { | ||
constructor(props) { | ||
super(props); | ||
} | ||
|
||
render() { | ||
return ( | ||
<Card> | ||
<Grid container alignItems="certer"> | ||
<Grid item align="center" xs={4}> | ||
<img src={this.props.image_url} height="100%" width="100%" /> | ||
</Grid> | ||
<Grid item align="center" xs={8}> | ||
<Typography color="textsecondary" variant="subtitle1"> | ||
{this.props.title} | ||
</Typography> | ||
<div> | ||
<Iconbutton> | ||
{this.props.is_playing ? <PauseIcon /> : <PlayArrwIcon />} | ||
</Iconbutton> | ||
</div> | ||
|
||
</Grid> | ||
</Grid> | ||
</Card> | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file modified
BIN
+8 Bytes
(100%)
Read+Django/music_app/music_controller/music_controller/__pycache__/settings.cpython-311.pyc
Binary file not shown.
Binary file modified
BIN
+91 Bytes
(110%)
Read+Django/music_app/music_controller/music_controller/__pycache__/urls.cpython-311.pyc
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from django.contrib import admin | ||
|
||
# Register your models here. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
from django.apps import AppConfig | ||
|
||
|
||
class SportifyConfig(AppConfig): | ||
default_auto_field = 'django.db.models.BigAutoField' | ||
name = 'sportify' |
3 changes: 3 additions & 0 deletions
3
Read+Django/music_app/music_controller/sportify/credentials.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
CLIENT_ID = "40ed5b29eda141e5a45038852ecf89a3" | ||
CLIENT_SECRET = "17e4ba446a444aa0aa920bfdf79caa26" | ||
REDIRECT_URI = "http://127.0.0.1:8000/sportify/redirect" |
26 changes: 26 additions & 0 deletions
26
Read+Django/music_app/music_controller/sportify/migrations/0001_initial.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# Generated by Django 4.2.1 on 2023-07-11 20:05 | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
initial = True | ||
|
||
dependencies = [ | ||
] | ||
|
||
operations = [ | ||
migrations.CreateModel( | ||
name='SportifyToken', | ||
fields=[ | ||
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), | ||
('user', models.CharField(max_length=50, unique=True)), | ||
('created_at', models.DateTimeField(auto_now_add=True)), | ||
('refresh_token', models.CharField(max_length=150)), | ||
('access_token', models.CharField(max_length=150)), | ||
('expires_in', models.DateTimeField()), | ||
('token_type', models.CharField(max_length=50)), | ||
], | ||
), | ||
] |
Empty file.
Binary file added
BIN
+1.34 KB
...o/music_app/music_controller/sportify/migrations/__pycache__/0001_initial.cpython-311.pyc
Binary file not shown.
Binary file added
BIN
+191 Bytes
...jango/music_app/music_controller/sportify/migrations/__pycache__/__init__.cpython-311.pyc
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
from django.db import models | ||
|
||
# Create your models here. | ||
class SportifyToken(models.Model): | ||
user = models.CharField(max_length=50, unique=True) | ||
created_at = models.DateTimeField(auto_now_add=True) | ||
refresh_token = models.CharField(max_length=150) | ||
access_token = models.CharField(max_length=150) | ||
expires_in = models.DateTimeField() | ||
token_type = models.CharField(max_length=50) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from django.test import TestCase | ||
|
||
# Create your tests here. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
from django.urls import path | ||
from .views import * | ||
|
||
urlpatterns = [ | ||
path('get-auth-url', AuthURL.as_view()), | ||
path('redirect', sportify_callback), | ||
path('is-authenticated', IsAuthenticated.as_view()), | ||
path('current-song', CurrentSong.as_view()) | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
from .models import SportifyToken | ||
from django.utils import timezone | ||
from datetime import timedelta | ||
from .credentials import CLIENT_ID, CLIENT_SECRET | ||
from requests import post, put, get | ||
|
||
BASE_URL = "https://api.spotify.com/v1/me/" | ||
|
||
def get_user_tokens(session_id): | ||
user_tokens = SportifyToken.objects.filter(user = session_id) | ||
if user_tokens.exists(): | ||
return user_tokens[0] | ||
else: | ||
return None | ||
|
||
def update_or_create_token(session_id, access_token, token_type, expires_in, refresh_token): | ||
tokens = get_user_tokens(session_id) | ||
expires_in = timezone.now() + timedelta(seconds=expires_in) | ||
|
||
if tokens: | ||
tokens.access_token = access_token | ||
tokens.refresh_token = refresh_token | ||
tokens.expires_in = expires_in | ||
tokens.token_type = token_type | ||
tokens.save(update_fields=['access_token', | ||
'refesh_toke', 'expires_in', 'token_type']) | ||
|
||
else: | ||
tokens = SportifyToken(user=session_id, access_token=access_token, | ||
refresh_token=refresh_token, token_type=token_type, expires_in=expires_in) | ||
tokens.save() | ||
|
||
def is_spotify_authenticated(session_id): | ||
tokens = get_user_tokens(session_id) | ||
if tokens: | ||
expiry = tokens.expires_in | ||
if expiry <= timezone.now(): | ||
refresh_spotify_token(session_id) | ||
|
||
return True | ||
|
||
return False | ||
|
||
|
||
def refresh_spotify_token(session_id): | ||
refresh_token = get_user_tokens(session_id).refresh_token | ||
|
||
response = post('https://accounts.spotify.com/api/token', data={ | ||
'grant_type': 'refresh_token', | ||
'refresh_token': refresh_token, | ||
'client_id': CLIENT_ID, | ||
'client_secret': CLIENT_SECRET | ||
}).json() | ||
|
||
access_token = response.get('access_token') | ||
token_type = response.get('token_type') | ||
expires_in = response.get('expires_in') | ||
refresh_token = response.get('refresh_token') | ||
|
||
update_or_create_token(session_id, access_token, token_type, expires_in, refresh_token) | ||
|
||
def execute_spotify_api_request(session_id, endpoint, post_=False, put_=False): | ||
tokens = get_user_tokens(session_id) | ||
headers = {'Content-Type': 'application/json', | ||
'Authorization': "Bearer " + tokens.access_token} | ||
|
||
if post_: | ||
post(BASE_URL + endpoint, headers=headers) | ||
if put_: | ||
put(BASE_URL + endpoint, headers=headers) | ||
|
||
response = get(BASE_URL + endpoint, {}, headers=headers) | ||
try: | ||
return response.json() | ||
except: | ||
return {'Error': 'Issue with request'} |
Oops, something went wrong.