Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New player #16

Open
wants to merge 19 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion JukeBox/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@

)

SITE_ID = 1
SITE_ID = 2

MIDDLEWARE_CLASSES = (
'django.contrib.sessions.middleware.SessionMiddleware',
Expand Down
3 changes: 2 additions & 1 deletion manager/admin.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
from django.contrib import admin
from manager.models import Playlist, Track, Artist, PlaylistEntry
from manager.models import Playlist, Track, Artist, PlaylistEntry, PlaylistSession

admin.site.register(Playlist)
admin.site.register(Track)
admin.site.register(Artist)
admin.site.register(PlaylistEntry)
admin.site.register(PlaylistSession)

# Register your models here.
65 changes: 48 additions & 17 deletions manager/models.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
from django.db import models
import pprint
import sys
import os
import subprocess

import unicodedata
import requests
import json

def import_playlists(userid = 674215921):
user_playlists=json.loads(requests.get("https://api.deezer.com/user/" + str(userid) + "/playlists").text)['data']
# from fields import JSONField
from json_field import JSONField
import json


def import_playlists(userid=674215921):
user_playlists = json.loads(requests.get(
"https://api.deezer.com/user/" + str(userid) + "/playlists").text)['data']
for playlist in user_playlists:
json_result=requests.get("https://api.deezer.com/playlist/" + str(playlist['id']))
playlist_tracks=json.loads(json_result.text)['tracks']['data']
if not Playlist.objects.filter(DeezerId= int(playlist['id'])).exists():
json_result = requests.get(
"https://api.deezer.com/playlist/" + str(playlist['id']))
playlist_tracks = json.loads(json_result.text)['tracks']['data']
if not Playlist.objects.filter(DeezerId=int(playlist['id'])).exists():
pl = Playlist()
pl.title = playlist['title']
pl.DeezerId = playlist['id']
Expand All @@ -23,25 +27,27 @@ def import_playlists(userid = 674215921):
pl.save()

for track in playlist_tracks:
if not Artist.objects.filter(DeezerId= int(track['artist']['id'])).exists():
if not Artist.objects.filter(DeezerId=int(track['artist']['id'])).exists():
art = Artist()
art.name = track['artist']['name']
art.DeezerId = track['artist']['id']
art.link = track['artist']['link']
# art.picture = track['artist']['picture']
art.save()

if not Track.objects.filter(DeezerId= int(track['id'])).exists():
if not Track.objects.filter(DeezerId=int(track['id'])).exists():
tr = Track()
tr.title = track['title']
tr.DeezerId = track['id']
tr.link = track['link']
tr.duration = track['duration']
tr.ArtistId = Artist.objects.filter(DeezerId=int(track['artist']['id'])).first()
tr.ArtistId = Artist.objects.filter(
DeezerId=int(track['artist']['id'])).first()
tr.save()

tr_id = Track.objects.filter(DeezerId=int(track['id'])).first()
pl_id = Playlist.objects.filter(DeezerId=int(playlist['id'])).first()
pl_id = Playlist.objects.filter(
DeezerId=int(playlist['id'])).first()

if not PlaylistEntry.objects.filter(TrackId=tr_id, PlaylistId=pl_id).exists():
pl_ent = PlaylistEntry()
Expand All @@ -51,6 +57,8 @@ def import_playlists(userid = 674215921):
pl_ent.save()

# Create your models here.


class Playlist(models.Model):
title = models.CharField(max_length=100)
DeezerId = models.IntegerField(unique=True)
Expand All @@ -73,7 +81,7 @@ def to_dict(self):
"link": self.link,
"picture": self.picture,
"duration": self.duration,
"Tracks": [ a.TrackId.to_dict() for a in self.playlistentry_set.all() ]
# "Tracks": [a.TrackId.to_dict() for a in self.playlistentry_set.all()]
}
return dico

Expand All @@ -93,7 +101,8 @@ class Artist(models.Model):

def to_dict(self):
dico = {
"name": self.name,
"name": remove_accents(self.name[:16]),
# "name": self.name,
"DeezerId": self.DeezerId,
"link": self.link,
"picture": self.picture,
Expand All @@ -103,6 +112,7 @@ def to_dict(self):
def __str__(self): # __unicode__ on Python 2
return self.name


class Track(models.Model):
title = models.CharField(max_length=100)
DeezerId = models.IntegerField(unique=True)
Expand All @@ -112,13 +122,15 @@ class Track(models.Model):

def to_dict(self):
dico = {
"title": self.title,
"title": remove_accents(self.title[:25]),
# "title": self.title,
"DeezerId": self.DeezerId,
"link": self.link,
"duration": self.duration,
"minutes": self.getMinutes(),
"msec": self.getmSec(),
"Artist": None,
"vote": 0
}

art = Artist.objects.get(id=self.ArtistId.id)
Expand All @@ -144,6 +156,25 @@ class PlaylistEntry(models.Model):
TrackId = models.ForeignKey(Track)

def __str__(self): # __unicode__ on Python 2
string = self.PlaylistId.title + ' - ' + str(elf.TrackId.title)
string = self.PlaylistId.title + ' - ' + str(self.TrackId.title)
return string

def to_dict(self):
dico = {}
return dico

class PlaylistSession(models.Model):
Session = JSONField(blank=True, null=True)
# Session = JSONField(blank=True, null=True)

def __str__(self): # __unicode__ on Python 2
# string = self.PlaylistId.title + ' - ' + str(elf.TrackId.title)
return str(self.id)


def remove_accents(input_str):
nkfd_form = unicodedata.normalize('NFKD', input_str)
only_ascii = nkfd_form.encode('ASCII', 'ignore')
return only_ascii


2 changes: 1 addition & 1 deletion manager/templates/manager/nodejs_manager.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
});

function playOn(){
socket.emit('playlistOn', "{{ playlist.to_json }}".replace(/"/g,"\"") );
socket.emit('playlistOn', "{{ context }}".replace(/"/g,"\"") );
}
function playOff(){
socket.emit('playlistOff', "Aucune playlist en ligne" );
Expand Down
10 changes: 5 additions & 5 deletions manager/templates/manager/track_list.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

(L'id de la playlist est : {{ playlist.DeezerId }})
({{ playlist.getMinutes }})
({{ playlist.duration }})
<form>

Liste des morceaux :
Expand All @@ -15,11 +15,11 @@
</tr>
</thead>
<tbody>
{% for tr in track_list %}
{% for tr in tracks %}
<tr>
<td> {{tr.TrackId.title}} </td>
<td> {{tr.TrackId.ArtistId.name}} </td>
<td> {{tr.TrackId.getMinutes}} </td>
<td> {{tr.title}} </td>
<td> {{tr.Artist.name}} </td>
<td> {{tr.duration}} </td>
</tr>
{% endfor %}
</tbody>
Expand Down
4 changes: 4 additions & 0 deletions manager/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,8 @@
url(r'^selection/', views.select, name='select'),
url(r'^update/', views.update, name='update'),
url(r'^playing/', views.playing, name='play'),
url(r'^createSession/', views.createSession, name='newSession'),
url(r'^getSession/', views.getSession, name='getSession'),
url(r'^next/', views.nextTrack, name='next'),
url(r'^vote/', views.newVote, name='vote'),
)
Loading