Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from django.core.management.base import BaseCommand
- from django.db import transaction
- from rutube.models import Video
- from django.db.utils import IntegrityError
- import requests
- class Parser:
- def parse(self, list_url):
- playlist_id = list_url.split('playlist=')[-1]
- nexp_flag = True
- page = 1
- data = []
- while nexp_flag:
- url = f'https://rutube.ru/api/playlist/custom/{playlist_id}/videos/?limit=20&page={page}'
- headers = {'Content-Type': 'application/json; charset=utf-8'}
- rs = requests.get(url, headers=headers)
- json = rs.json()
- for result in json['results']:
- temp = {}
- temp['title'] = result['title']
- temp['description'] = result['description']
- temp['category'] = result['category']['name']
- temp['img_link'] = result['thumbnail_url']
- temp['publication'] = result['publication_ts']
- temp['duration'] = result['duration']
- temp['video_url'] = result['embed_url']
- data.append(temp)
- nexp_flag = json['has_next']
- page += 1
- return data
- class Builder:
- def video(self, title, description, category, img_link, publication, duration, video_link, commit=False):
- try:
- video = Video(
- title = title,
- description = description,
- category = category,
- img_link = img_link,
- publication = publication,
- duration = duration,
- video_link = video_link
- )
- if commit:
- video.save()
- print(f'Video {title} is added.')
- except IntegrityError:
- print(f'Video is not unique.')
- return video
- class Command(BaseCommand):
- help = "Parse rutube playlist to the database."
- @transaction.atomic
- def handle(self, *args, **options):
- list_url = "https://rutube.ru/video/a40eadf2723be63c3a865e3790c1f52a/?playlist=329379"
- builder = Builder()
- parser = Parser()
- videos = parser.parse(list_url)
- video_objects = [builder.video(video['title'], video['description'], video['category'], video['img_link'], video['publication'], video['duration'], video['video_url']) for video in videos]
- Video.objects.bulk_create(video_objects)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement