live_share_youtube/player/migrations/0005_title.py

39 lines
1.1 KiB
Python
Raw Normal View History

2018-03-28 21:01:47 +00:00
from urllib.parse import parse_qs
import requests
from django.db import models, migrations, transaction
YOUTUBE_INFO_URL = 'http://youtube.com/get_video_info?video_id={}'
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))
q = parse_qs(response.content.decode('utf-8'))
try:
o.title = q['title'][0]
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"),
),
]