Advertisement
Guest User

Untitled

a guest
Dec 25th, 2023
29
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.04 KB | None | 0 0
  1. from django.core.management.base import BaseCommand
  2. from django.db import transaction
  3. from rutube.models import Video
  4. from django.db.utils import IntegrityError
  5.  
  6. import requests
  7.  
  8. class Parser:
  9. def parse(self, list_url):
  10. playlist_id = list_url.split('playlist=')[-1]
  11. nexp_flag = True
  12. page = 1
  13. data = []
  14. while nexp_flag:
  15. url = f'https://rutube.ru/api/playlist/custom/{playlist_id}/videos/?limit=20&page={page}'
  16. headers = {'Content-Type': 'application/json; charset=utf-8'}
  17. rs = requests.get(url, headers=headers)
  18. json = rs.json()
  19.  
  20. for result in json['results']:
  21. temp = {}
  22. temp['title'] = result['title']
  23. temp['description'] = result['description']
  24. temp['category'] = result['category']['name']
  25. temp['img_link'] = result['thumbnail_url']
  26. temp['publication'] = result['publication_ts']
  27. temp['duration'] = result['duration']
  28. temp['video_url'] = result['embed_url']
  29. data.append(temp)
  30.  
  31. nexp_flag = json['has_next']
  32. page += 1
  33. return data
  34.  
  35. class Builder:
  36. def video(self, title, description, category, img_link, publication, duration, video_link, commit=False):
  37. try:
  38. video = Video(
  39. title = title,
  40. description = description,
  41. category = category,
  42. img_link = img_link,
  43. publication = publication,
  44. duration = duration,
  45. video_link = video_link
  46. )
  47. if commit:
  48. video.save()
  49. print(f'Video {title} is added.')
  50. except IntegrityError:
  51. print(f'Video is not unique.')
  52. return video
  53.  
  54. class Command(BaseCommand):
  55. help = "Parse rutube playlist to the database."
  56.  
  57. @transaction.atomic
  58. def handle(self, *args, **options):
  59. list_url = "https://rutube.ru/video/a40eadf2723be63c3a865e3790c1f52a/?playlist=329379"
  60. builder = Builder()
  61. parser = Parser()
  62. videos = parser.parse(list_url)
  63. video_objects = [builder.video(video['title'], video['description'], video['category'], video['img_link'], video['publication'], video['duration'], video['video_url']) for video in videos]
  64. Video.objects.bulk_create(video_objects)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement