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
|
|
|
|
|
|
|
from django.db import models, migrations, transaction
|
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-28 21:01:47 +00:00
|
|
|
|
|
|
|
def gen_title(apps, schema_editor):
|
|
|
|
Link = apps.get_model('player', 'Link')
|
|
|
|
for o in Link.objects.all():
|
|
|
|
response = requests.get(YOUTUBE_INFO_URL.format(o.token))
|
2018-03-28 22:52:50 +00:00
|
|
|
q = json.loads(response.content.decode('utf-8'))
|
2018-03-28 21:01:47 +00:00
|
|
|
try:
|
2018-03-28 22:52:50 +00:00
|
|
|
o.title = q['title']
|
2018-03-28 21:01:47 +00:00
|
|
|
except KeyError:
|
|
|
|
o.delete()
|
|
|
|
else:
|
|
|
|
o.save()
|
|
|
|
|
|
|
|
|
|
|
|
class Migration(migrations.Migration):
|
|
|
|
dependencies = [
|
|
|
|
('player', '0004_playlist_public'),
|
|
|
|
]
|
|
|
|
atomic = False
|
|
|
|
|
|
|
|
operations = [
|
|
|
|
migrations.AddField(
|
|
|
|
model_name='link',
|
|
|
|
name='title',
|
|
|
|
field= models.CharField(null=True, max_length=200, verbose_name="Titre"),
|
|
|
|
),
|
|
|
|
migrations.RunPython(gen_title),
|
|
|
|
migrations.AlterField(
|
|
|
|
model_name='link',
|
|
|
|
name='title',
|
|
|
|
field= models.CharField(max_length=255, verbose_name="Titre"),
|
|
|
|
),
|
|
|
|
]
|