48 lines
2.3 KiB
Python

#! /usr/bin/env python3
import uuid
import odyseescraper
from django.core.management.base import BaseCommand, CommandError
from django.db import models
class Command(BaseCommand):
help = "Updates all OdyseeChannel objects, creating OdyseeRelease objects for anything new"
def handle(self, *args, **options):
self.stdout.write('Updating Channels...')
updates = 0
failures = []
for channel in odyseescraper.models.OdyseeChannel.objects.all():
print(f'{str(channel)}...')
self.stdout.write('Getting releases...')
releases = odyseescraper.odysee.odysee_get_releases(channel.handle)
for release in releases:
try:
data = releases[release]
if odyseescraper.models.OdyseeRelease.objects.filter(pk=release).exists():
self.stdout.write(f'Skipping existing release: {str(channel)} - {data['title']}')
continue
a = odyseescraper.models.OdyseeRelease.objects.get_or_create(
id = release,
channel = channel,
name = data['title'],
description = data['description'],
url = data['url'],
thumbnail = data['thumbnail'],
)
self.stdout.write(f'Imported release: {str(channel)} - {data['title']}')
updates += 1
except Exception as e:
self.stdout.write(self.style.WARNING(f'Failed to update {str(channel)}: {e}'))
failures.append({"channel": str(channel),"item":releases[release]["title"],"error":e})
if failures:
self.stdout.write(self.style.ERROR('Errors occurred while importing data:'))
for error in failures:
self.stdout.write(self.style.ERROR(f'{error["channel"]} - {error["item"]} - {error["error"]}'))
if updates > 0:
self.stdout.write(self.style.WARNING(f'Imported {updates} new releases despite this'))
else:
if updates > 0:
self.stdout.write(self.style.SUCCESS(f'Imported {updates} new releases'))
else:
self.stdout.write(self.style.SUCCESS('No new imports'))