#! /usr/bin/env python3 import datetime import odyseescraper import time import uuid 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('Scraping Odysee channels for new content...') updates = 0 failures = [] for channel in odyseescraper.models.OdyseeChannel.objects.all(): print(f'Updating channel: {str(channel)}...') 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(): continue a = odyseescraper.models.OdyseeRelease.objects.get_or_create( id = release, channel = channel, name = data['title'], description = data['description'], released = datetime.datetime.utcfromtimestamp(data['publishdate']).strftime("%Y-%m-%d"), url = data['url'], thumbnail = data['thumbnail'], filehash = data['filehash'] ) 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}) # We sleep for 1 second here so that we're not slamming Odysee with tons and tons of requests time.sleep(1) 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'))