[scrobbles] Add management command for imap
This commit is contained in:
@ -2,7 +2,6 @@ import json
|
|||||||
import imaplib
|
import imaplib
|
||||||
import email
|
import email
|
||||||
from email.header import decode_header
|
from email.header import decode_header
|
||||||
from django.core.files.base import ContentFile
|
|
||||||
from profiles.models import UserProfile
|
from profiles.models import UserProfile
|
||||||
from scrobbles.models import Scrobble
|
from scrobbles.models import Scrobble
|
||||||
from scrobbles.scrobblers import email_scrobble_board_game
|
from scrobbles.scrobblers import email_scrobble_board_game
|
||||||
@ -11,11 +10,12 @@ import logging
|
|||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
def process_scrobbles_from_imap() -> list[Scrobble] | None:
|
def process_scrobbles_from_imap() -> list[Scrobble]:
|
||||||
"""For all user profiles with IMAP creds, check inbox for scrobbleable email attachments."""
|
"""For all user profiles with IMAP creds, check inbox for scrobbleable email attachments."""
|
||||||
scrobbles_created: list[Scrobble] = []
|
scrobbles_created: list[Scrobble] = []
|
||||||
|
|
||||||
active_profiles = UserProfile.objects.filter(imap_auto_import=True)
|
active_profiles = UserProfile.objects.filter(imap_auto_import=True)
|
||||||
|
logger.info("Starting import of scrobbles from IMAP", extra={"active_profiles": active_profiles})
|
||||||
for profile in active_profiles:
|
for profile in active_profiles:
|
||||||
logger.info(
|
logger.info(
|
||||||
"Importing scrobbles from IMAP for user",
|
"Importing scrobbles from IMAP for user",
|
||||||
@ -62,18 +62,16 @@ def process_scrobbles_from_imap() -> list[Scrobble] | None:
|
|||||||
# Try parsing JSON if applicable
|
# Try parsing JSON if applicable
|
||||||
parsed_json = {}
|
parsed_json = {}
|
||||||
if filename.lower().endswith(".bgsplay"):
|
if filename.lower().endswith(".bgsplay"):
|
||||||
|
# TODO Pull this out into a parse_pgsplay function
|
||||||
try:
|
try:
|
||||||
parsed_json = json.loads(
|
parsed_json = json.loads(
|
||||||
file_data.decode("utf-8")
|
file_data.decode("utf-8")
|
||||||
)
|
)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
# You might want to log this
|
logger.error("Failed to parse JSON file", extra={"filename": filename, "error": e})
|
||||||
print(
|
|
||||||
f"Failed to parse JSON from {filename}: {e}"
|
|
||||||
)
|
|
||||||
|
|
||||||
if not parsed_json:
|
if not parsed_json:
|
||||||
logger.info("No JSON found in BG Stats file")
|
logger.info("No JSON found in BG Stats file", extra={"filename": filename})
|
||||||
continue
|
continue
|
||||||
|
|
||||||
scrobble = email_scrobble_board_game(
|
scrobble = email_scrobble_board_game(
|
||||||
@ -91,3 +89,4 @@ def process_scrobbles_from_imap() -> list[Scrobble] | None:
|
|||||||
)
|
)
|
||||||
return scrobbles_created
|
return scrobbles_created
|
||||||
logger.info(f"No new scrobbles found in IMAP folders")
|
logger.info(f"No new scrobbles found in IMAP folders")
|
||||||
|
return []
|
||||||
|
|||||||
@ -0,0 +1,15 @@
|
|||||||
|
from django.core.management.base import BaseCommand
|
||||||
|
from vrobbler.apps.scrobbles.imap import process_scrobbles_from_imap
|
||||||
|
|
||||||
|
|
||||||
|
class Command(BaseCommand):
|
||||||
|
def add_arguments(self, parser):
|
||||||
|
parser.add_argument(
|
||||||
|
"--restart",
|
||||||
|
action="store_true",
|
||||||
|
help="Restart failed imports",
|
||||||
|
)
|
||||||
|
|
||||||
|
def handle(self, *args, **options):
|
||||||
|
count = len(process_scrobbles_from_imap())
|
||||||
|
print(f"Started {count} IMAP imports")
|
||||||
Reference in New Issue
Block a user