2025-01-23 23:07:59 -06:00
|
|
|
#! /usr/bin/env python3
|
2025-01-24 03:07:01 -06:00
|
|
|
import datetime
|
2025-01-23 23:07:59 -06:00
|
|
|
import odyseescraper
|
2025-01-24 03:07:01 -06:00
|
|
|
import uuid
|
2025-01-23 23:07:59 -06:00
|
|
|
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):
|
2025-01-23 23:22:10 -06:00
|
|
|
self.stdout.write('Scraping Odysee channels for new content...')
|
2025-01-23 23:07:59 -06:00
|
|
|
updates = 0
|
|
|
|
failures = []
|
|
|
|
for channel in odyseescraper.models.OdyseeChannel.objects.all():
|
2025-01-23 23:22:10 -06:00
|
|
|
print(f'Updating channel: {str(channel)}...')
|
2025-01-23 23:07:59 -06:00
|
|
|
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,
|
2025-01-23 23:15:56 -06:00
|
|
|
channel = channel,
|
2025-01-23 23:07:59 -06:00
|
|
|
name = data['title'],
|
|
|
|
description = data['description'],
|
2025-01-24 03:07:01 -06:00
|
|
|
released = datetime.datetime.utcfromtimestamp(int(data['publishdate'])).strftime("%Y-%m-%d"),
|
2025-01-23 23:07:59 -06:00
|
|
|
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"]}'))
|
2025-01-23 23:15:56 -06:00
|
|
|
if updates > 0:
|
|
|
|
self.stdout.write(self.style.WARNING(f'Imported {updates} new releases despite this'))
|
2025-01-23 23:07:59 -06:00
|
|
|
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'))
|