2018-03-28 21:01:47 +00:00
|
|
|
import requests
|
2018-03-28 22:52:50 +00:00
|
|
|
import json
|
2018-03-28 21:01:47 +00:00
|
|
|
|
2018-03-24 18:34:01 +00:00
|
|
|
from django.db import models
|
2018-03-25 15:52:52 +00:00
|
|
|
from django.shortcuts import reverse
|
2018-03-24 18:34:01 +00:00
|
|
|
|
2018-03-24 19:07:17 +00:00
|
|
|
PK_LENGTH = 23
|
2018-03-28 22:52:50 +00:00
|
|
|
YOUTUBE_INFO_URL = 'http://www.youtube.com/oembed?url=http://www.youtube.com/watch?v={}&format=json'
|
2018-03-24 19:07:17 +00:00
|
|
|
|
|
|
|
class Playlist(models.Model):
|
|
|
|
date = models.DateTimeField(verbose_name="date")
|
2018-03-25 15:52:52 +00:00
|
|
|
name = models.CharField(
|
|
|
|
max_length=255,
|
|
|
|
verbose_name="Nom de la playlist"
|
|
|
|
)
|
2018-03-28 15:05:18 +00:00
|
|
|
last_update = models.DateTimeField(
|
|
|
|
verbose_name="Dernière mise à jour"
|
|
|
|
)
|
|
|
|
last_get = models.DateTimeField(
|
|
|
|
verbose_name="Dernière écoute"
|
|
|
|
)
|
2018-03-28 18:30:37 +00:00
|
|
|
public = models.BooleanField(
|
|
|
|
verbose_name="Visible sur la page d'accueil",
|
|
|
|
default=True,
|
|
|
|
)
|
2018-03-24 19:07:17 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def reverse_token(token):
|
|
|
|
return token & ((1 << PK_LENGTH) - 1)
|
|
|
|
|
|
|
|
def get_token(self):
|
|
|
|
return int(self.date.timestamp()) << PK_LENGTH | self.pk
|
|
|
|
|
|
|
|
def get_absolute_url(self):
|
2018-03-25 15:52:52 +00:00
|
|
|
return reverse('player:playlist', kwargs={'token':self.get_token()})
|
2018-03-24 19:07:17 +00:00
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return "Playlist " + str(self.get_token())
|
|
|
|
|
|
|
|
|
|
|
|
class Link(models.Model):
|
2018-03-25 15:52:52 +00:00
|
|
|
token = models.CharField(
|
|
|
|
max_length=200,
|
|
|
|
verbose_name="Token",
|
|
|
|
)
|
2018-03-24 19:07:17 +00:00
|
|
|
playlist = models.ForeignKey(
|
|
|
|
Playlist,
|
|
|
|
on_delete=models.CASCADE,
|
|
|
|
verbose_name="Playlist",
|
|
|
|
)
|
2018-03-28 21:01:47 +00:00
|
|
|
title = models.CharField(
|
|
|
|
max_length=255,
|
|
|
|
verbose_name="Titre"
|
|
|
|
)
|
|
|
|
|
2018-03-24 19:07:17 +00:00
|
|
|
|
|
|
|
def __str__(self):
|
2018-03-28 15:05:18 +00:00
|
|
|
return "Link : " + self.token + " of " + str(self.playlist)
|
2018-03-28 21:01:47 +00:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def update_titles(cls):
|
|
|
|
for o in cls.objects.all():
|
|
|
|
response = requests.get(YOUTUBE_INFO_URL.format(o.token))
|
2018-03-28 22:52:50 +00:00
|
|
|
o.title = json.loads(response.content.decode('utf-8'))['title']
|
2018-03-28 21:01:47 +00:00
|
|
|
o.save()
|