Source code for berhoel.django.media_ooimport.console

#! /usr/bin/env python
# -*- coding: utf-8 -*-
"""Load Data into Django model.
"""

# Standard library imports.
import os
import pdb
import sys
import argparse
import traceback
from pathlib import Path
from functools import cached_property

# Django library imports.
import django

# First party library imports.
from berhoel import helper
from berhoel.odf.ods import Ods

__date__ = "2022/12/13 19:38:39 hoel"
__author__ = "Berthold Höllmann"
__copyright__ = "Copyright © 2015,2020 by Berthold Höllmann"
__credits__ = ["Berthold Höllmann"]
__maintainer__ = "Berthold Höllmann"
__email__ = "berhoel@gmail.com"

try:
    # Local library imports.
    from .._version import __version__
except ImportError:
    __version__ = "0.0.0+unknown0"

# Full path to your django project directory
DJANGOPROJECT_HOME = Path(__file__).resolve().parents[4]
sys.path.append(f"{DJANGOPROJECT_HOME}")
if os.environ.get("DJANGO_SETTINGS_MODULE") is None:
    os.environ["DJANGO_SETTINGS_MODULE"] = "berhoel.django.media_ooimport.settings"

django.setup()

# These modules can only be included after django setup.
from ..models import (  # isort:skip # noqa: E402
    NameMediaMap,
    NameCinemaMap,
    NameRentalMap,
    ConvertedSheet,
    NameStreamingServiceMap,
)
from .kuv import KaufenUndVerkaufen  # isort:skip # noqa: E402
from .filme import Filme  # isort:skip # noqa: E402
from .serie import Serie  # isort:skip # noqa: E402
from berhoel.django.media.models import Broadcast, Person  # isort:skip # noqa: E402


# Aliases for broadcasting services.
BCS = {
    "3SAT": ("3SAT", "3sat"),
    "7MAXX": ("7MAXX",),
    "ARD": ("ARD",),
    "Arte": ("Arte", "ARTE", "Arte Mediathek", "ARTE Mediathek"),
    "BR": ("BR",),
    "HR": ("HR",),
    "Kabel 1": ("Kabel 1", "kabeleins"),
    "NDR": ("NDR",),
    "one": ("one",),
    "PRO 7": ("PRO 7", "Pro7", "PRO7"),
    "RTL": ("RTL",),
    "RTL II": (
        "RTL II",
        "Rtl II",
    ),
    "RTL NITRO": ("RTL NITRO",),
    "SAT 1": (
        "SAT 1",
        "Sat1",
        "SAT1",
    ),
    "SIXX": ("SIXX",),
    "Super RTL": ("Super RTL",),
    "TELE5": ("TELE5",),
    "VOX": (
        "VOX",
        "VOX Now",
        "Vox",
    ),
    "ZDF": ("ZDF",),
    "ZDFneo": (
        "ZDFneo",
        "ZDF Neo",
        "zdf neo",
        "ZDF neo",
        "ZDFNeo",
    ),
    "mdr": ("mdr", "MDR"),
    "nitro": (
        "nitro",
        "NITRO",
    ),
}

# Aliases for cinemas.
CINEMAS = {
    "Abaton Kino": ("Abaton Kino",),
    "CinemaxX Hamburg-Dammtor": ("CinemaxX Hamburg-Dammtor",),
    "CinemaxX Hamburg-Wandsbek": ("CinemaxX Hamburg-Wandsbek",),
    "CineStar KristallPalast Bremen": ("CineStar KristallPalast Bremen",),
    "Cineworld Lünen": ("Cineworld Lünen",),
    "Holi": ("Holi", "HOLI"),
    "Passage Kino": ("Passage Kino",),
    "Planetarium Hamburg": ("Planetarium HH",),
    "Studio Bernsdoffstr.": ("Studio Bernsdoffstr.",),
    "UCI Mundsburg": ("UCI Mundsburg",),
    "UCI Othmarschen": ("UCI Othmarschen",),
    "UCI Smart City": ("UCI Smart City",),
    "Zeise Kino": ("Zeise Kino",),
    "alabama kino": ("alabama kino",),
}

# Aliases for vide rental provider.
VIDEO_RENTAL = {"Bücherhalle": ("Bücherhalle",)}

# Aliases for streaming services.
STREAMING_SERVICES = {
    "Disney+": ("Disney+",),
    "NETFLIX": (
        "NETFLIX",
        "Netflix",
        "netflix",
    ),
    "Star Trek New Voyages": ("Star Trek New Voyages",),
    "YouTube": ("YouTube",),
    "amazon prime": (
        "Amazon Prime",
        "amazon Prime",
        "Amazon prime",
    ),
    "Joyn": ("Joyn",),
}


[docs]class SeriesOO(object): "Import data from LibreOffice spreadsheet."
[docs] def __init__(self, fname): self.fname = fname self.fill_media()
@cached_property def ods(self) -> Ods: """Provide ods instance.""" with helper.timed_process_msg_context(f"opening {self.fname}"): ods = Ods(self.fname) return ods
[docs] def process(self) -> None: tables = self.ods.tables for table in tables: name = table.name if ConvertedSheet.objects.filter(saved=name).exists(): continue with helper.timed_process_msg_context(f"Importing '{name}'"): if name == "k&v": processor = KaufenUndVerkaufen(table) elif name == "Film": processor = Filme(table) else: processor = Serie(table) processor.process() ConvertedSheet.objects.create(saved=name)
[docs] @staticmethod def fill_bcs() -> None: "Fill information on broadcast services media." for name, aliases in BCS.items(): pers, _ = Person.objects.get_or_create(name=name) bcs, _ = Broadcast.objects.get_or_create(name=name, broadcast_service=pers) for alias in aliases: NameMediaMap.objects.get_or_create(name=alias, media=bcs)
[docs] @staticmethod def fill_person_maps(values, table) -> None: "Fill information in cinemas" for name, aliases in values.items(): pers = Person.objects.get_or_create(name=name)[0] for alias in aliases: table.objects.get_or_create(name=alias, person=pers)
[docs] def fill_media(self) -> None: "Prepare information in media." with helper.timed_process_msg_context("Preparing Broadcast Services"): self.fill_bcs() with helper.timed_process_msg_context("Preparing Cinemas"): self.fill_person_maps(CINEMAS, NameCinemaMap) with helper.timed_process_msg_context("Preparing Rental Companies"): self.fill_person_maps(VIDEO_RENTAL, NameRentalMap) with helper.timed_process_msg_context("Preparing Streaming Service Companies"): self.fill_person_maps(STREAMING_SERVICES, NameStreamingServiceMap)
PARSER = argparse.ArgumentParser( description="Import data from LibreOffice spreadsheet into django media." ) PARSER.add_argument("fname", type=Path, help="input ods file")
[docs]def main() -> None: args = PARSER.parse_args() try: sheet_ignore = {"suche", "Übersicht"} for sheet in sheet_ignore: ConvertedSheet.objects.get_or_create(saved=sheet) series_oo = SeriesOO(args.fname) with helper.timed_process_msg_context(f"Processing {args.fname}"): series_oo.process() except Exception: print() _, _, tb = sys.exc_info() traceback.print_exc() pdb.post_mortem(tb)
if __name__ == "__main__": main() # Local Variables: # mode: python # compile-command: "poetry run tox" # time-stamp-pattern: "30/__date__ = \"%:y/%02m/%02d %02H:%02M:%02S %u\"" # End: