Add command for scraping local files
This commit is contained in:
13
.vscode/launch.json
vendored
13
.vscode/launch.json
vendored
@ -14,7 +14,7 @@
|
|||||||
"justMyCode": true
|
"justMyCode": true
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"name": "python manage.py scrape_twitch",
|
"name": "scrape_twitch",
|
||||||
"type": "debugpy",
|
"type": "debugpy",
|
||||||
"request": "launch",
|
"request": "launch",
|
||||||
"program": "${workspaceFolder}/manage.py",
|
"program": "${workspaceFolder}/manage.py",
|
||||||
@ -23,6 +23,17 @@
|
|||||||
],
|
],
|
||||||
"django": true,
|
"django": true,
|
||||||
"justMyCode": true
|
"justMyCode": true
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "scrape_local",
|
||||||
|
"type": "debugpy",
|
||||||
|
"request": "launch",
|
||||||
|
"program": "${workspaceFolder}/manage.py",
|
||||||
|
"args": [
|
||||||
|
"scrape_local"
|
||||||
|
],
|
||||||
|
"django": true,
|
||||||
|
"justMyCode": true
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
39
core/management/commands/scrape_local.py
Normal file
39
core/management/commands/scrape_local.py
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
import asyncio
|
||||||
|
import json
|
||||||
|
import logging
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
from django.core.management.base import BaseCommand
|
||||||
|
|
||||||
|
from core.management.commands.scrape_twitch import process_json_data
|
||||||
|
|
||||||
|
logger: logging.Logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
class Command(BaseCommand):
|
||||||
|
help = "Scrape local files."
|
||||||
|
|
||||||
|
def handle(self, *args, **kwargs) -> None: # noqa: ANN002, ANN003, ARG002, PLR6301
|
||||||
|
"""Scrape local files.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
*args: Variable length argument list.
|
||||||
|
**kwargs: Arbitrary keyword arguments.
|
||||||
|
"""
|
||||||
|
dirs: list[str] = ["drop_campaigns", "reward_campaigns", "drop_campaigns"]
|
||||||
|
for dir_name in dirs:
|
||||||
|
logger.info("Scraping %s", dir_name)
|
||||||
|
for num, file in enumerate(Path(dir_name).rglob("*.json")):
|
||||||
|
logger.info("Processing %s", file)
|
||||||
|
|
||||||
|
with file.open(encoding="utf-8") as f:
|
||||||
|
try:
|
||||||
|
load_json = json.load(f)
|
||||||
|
except json.JSONDecodeError:
|
||||||
|
logger.exception("Failed to load JSON from %s", file)
|
||||||
|
continue
|
||||||
|
asyncio.run(main=process_json_data(num=num, campaign=load_json, local=True))
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
Command().handle()
|
Reference in New Issue
Block a user