fetch steam collection from event path
This commit is contained in:
@@ -4,6 +4,7 @@ Django management command to fetch Steam Workshop collections
|
|||||||
|
|
||||||
from django.core.management.base import BaseCommand, CommandError
|
from django.core.management.base import BaseCommand, CommandError
|
||||||
from opus_magnum.utils import create_or_update_collection
|
from opus_magnum.utils import create_or_update_collection
|
||||||
|
from events.models import Event
|
||||||
from opus_magnum.models import SteamAPIKey, SteamCollection
|
from opus_magnum.models import SteamAPIKey, SteamCollection
|
||||||
|
|
||||||
|
|
||||||
@@ -12,6 +13,12 @@ class Command(BaseCommand):
|
|||||||
|
|
||||||
def add_arguments(self, parser):
|
def add_arguments(self, parser):
|
||||||
parser.add_argument("url", type=str, help="Steam Workshop collection URL")
|
parser.add_argument("url", type=str, help="Steam Workshop collection URL")
|
||||||
|
parser.add_argument(
|
||||||
|
"--event",
|
||||||
|
type=str,
|
||||||
|
required=True,
|
||||||
|
help="Path of the event the collection belongs to",
|
||||||
|
)
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
"--force",
|
"--force",
|
||||||
action="store_true",
|
action="store_true",
|
||||||
@@ -22,6 +29,11 @@ class Command(BaseCommand):
|
|||||||
url = options["url"]
|
url = options["url"]
|
||||||
force = options["force"]
|
force = options["force"]
|
||||||
|
|
||||||
|
try:
|
||||||
|
event = Event.objects.get(path=options["event"])
|
||||||
|
except Event.DoesNotExist:
|
||||||
|
raise CommandError(f"No event with path '{options['event']}'")
|
||||||
|
|
||||||
self.stdout.write(f"Fetching Steam collection from: {url}")
|
self.stdout.write(f"Fetching Steam collection from: {url}")
|
||||||
|
|
||||||
api_key = SteamAPIKey.objects.filter(is_active=True).first()
|
api_key = SteamAPIKey.objects.filter(is_active=True).first()
|
||||||
@@ -53,7 +65,7 @@ class Command(BaseCommand):
|
|||||||
return
|
return
|
||||||
|
|
||||||
# Fetch and create/update collection
|
# Fetch and create/update collection
|
||||||
collection, created = create_or_update_collection(url)
|
collection, created = create_or_update_collection(url, event)
|
||||||
|
|
||||||
if created:
|
if created:
|
||||||
self.stdout.write(
|
self.stdout.write(
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ Utilities for fetching Steam Workshop collection data using Steam Web API
|
|||||||
|
|
||||||
import re
|
import re
|
||||||
import requests
|
import requests
|
||||||
|
from events.models import Event
|
||||||
from opus_magnum.models import SteamCollection, SteamCollectionItem, SubmissionFile
|
from opus_magnum.models import SteamCollection, SteamCollectionItem, SubmissionFile
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from django.utils import timezone
|
from django.utils import timezone
|
||||||
@@ -373,12 +374,15 @@ def fetch_steam_collection(url: str) -> Dict:
|
|||||||
return fetcher.fetch_collection_data(url)
|
return fetcher.fetch_collection_data(url)
|
||||||
|
|
||||||
|
|
||||||
def create_or_update_collection(url: str) -> Tuple[SteamCollection, bool]:
|
def create_or_update_collection(
|
||||||
|
url: str, event: Event
|
||||||
|
) -> Tuple[SteamCollection, bool]:
|
||||||
"""
|
"""
|
||||||
Create or update a Steam collection in the database
|
Create or update a Steam collection in the database
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
url: Steam Workshop collection URL
|
url: Steam Workshop collection URL
|
||||||
|
event: Event the collection belongs to
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
Tuple of (SteamCollection instance, created_flag)
|
Tuple of (SteamCollection instance, created_flag)
|
||||||
@@ -395,6 +399,7 @@ def create_or_update_collection(url: str) -> Tuple[SteamCollection, bool]:
|
|||||||
collection, created = SteamCollection.objects.update_or_create(
|
collection, created = SteamCollection.objects.update_or_create(
|
||||||
steam_id=data["steam_id"],
|
steam_id=data["steam_id"],
|
||||||
defaults={
|
defaults={
|
||||||
|
"event": event,
|
||||||
"url": data["url"],
|
"url": data["url"],
|
||||||
"title": data["title"],
|
"title": data["title"],
|
||||||
"description": data["description"],
|
"description": data["description"],
|
||||||
|
|||||||
Reference in New Issue
Block a user