<commit_before>import logging

import xml.etree.ElementTree as ET
from svtplay_dl.utils.output import formatname
from svtplay_dl.utils.parser import Options
from datetime import datetime


def write_nfo_episode(output, config):
    root = ET.Element("episodedetails")
    ET.SubElement(root, "title").text = output["title_nice"]
    ET.SubElement(root, "showtitle").text = output["episodename"]
    ET.SubElement(root, "season").text = output["season"]
    ET.SubElement(root, "episode").text = output["episode"]
    ET.SubElement(root, "plot").text = output["showdescription"]
    if output["publishing_datetime"] is not None:
        ET.SubElement(root, "aired").text = datetime.fromtimestamp(output["publishing_datetime"]).isoformat()
    if not config.get("thumbnail"):
        # Set the thumbnail path to download link if not thumbnail downloaded
        ET.SubElement(root, "thumb").text = output["showthumbnailurl"]

    filename = formatname(output.copy(), config, extension="nfo")
    logging.info("NFO episode: %s", filename)

    tree = ET.ElementTree(root)
    tree.write(filename, encoding="UTF-8", xml_declaration=True)


def write_nfo_tvshow(output, config):
    # Config for tvshow nfo file
    root = ET.Element("tvshow")
    ET.SubElement(root, "title").text = output["title_nice"]
    ET.SubElement(root, "plot").text = output["episodedescription"]
    if config.get("thumbnail"):
        # Set the thumbnail relative path to downloaded thumbnail
        ET.SubElement(root, "thumb").text = "{}.tvshow.tbn".format(output["title"])
    else:
        # Set the thumbnail path to download link if not thumbnail downloaded
        ET.SubElement(root, "thumb").text = output["episodethumbnailurl"]

    cconfig = Options()
    cconfig.set("output", config.get("output"))
    cconfig.set("path", config.get("path"))
    cconfig.set("subfolder", config.get("subfolder"))
    cconfig.set("filename", "tvshow.{ext}")
    filename = formatname(output.copy(), cconfig, extension="nfo")<commit_msg>Added some checks in the NFO writing to not throw exception if some metadata is None. Also skips NFO if not even the title is available.<commit_after>import logging

import xml.etree.ElementTree as ET
from svtplay_dl.utils.output import formatname
from svtplay_dl.utils.parser import Options
from datetime import datetime


def write_nfo_episode(output, config):
    if not output["title_nice"]:
        # If we don't even have the title, skip the NFO
        return
    root = ET.Element("episodedetails")
    ET.SubElement(root, "title").text = output["title_nice"]
    if output["episodename"]:
        ET.SubElement(root, "showtitle").text = output["episodename"]
    if output["season"]:
        ET.SubElement(root, "season").text = str(output["season"])
    if output["episode"]:
        ET.SubElement(root, "episode").text = str(output["episode"])
    ET.SubElement(root, "plot").text = output["showdescription"]
    if output["publishing_datetime"] is not None:
        ET.SubElement(root, "aired").text = datetime.fromtimestamp(output["publishing_datetime"]).isoformat()
    if not config.get("thumbnail") and output["showthumbnailurl"]:
        # Set the thumbnail path to download link if not thumbnail downloaded
        ET.SubElement(root, "thumb").text = output["showthumbnailurl"]

    filename = formatname(output.copy(), config, extension="nfo")
    logging.info("NFO episode: %s", filename)

    tree = ET.ElementTree(root)
    tree.write(filename, encoding="UTF-8", xml_declaration=True)


def write_nfo_tvshow(output, config):
    # Config for tvshow nfo file
    if not output["title_nice"]:
        # If we don't even have the title, skip the NFO
        return
    root = ET.Element("tvshow")
    ET.SubElement(root, "title").text = output["title_nice"]
    if output["episodedescription"]:
        ET.SubElement(root, "plot").text = output["episodedescription"]
    if config.get("thumbnail"):
        # Set the thumbnail relative path to downloaded thumbnail
        ET.SubElement(root, "thumb").text = "{}.tvshow.tbn".format(output["title"])
    elif output["episodethumbnailurl"]:
        # Set the thumbnail path to download link if not thumbnail downloaded
        ET.SubElement(root, "thumb").text = output["episodethumbnailurl"]

    cconfig = Options()
    cconfig.set("output", config.get("output"))
    cconfig.set("path", config.get("path"))
    cconfig.set("subfolder", config.get("subfolder"))
    cconfig.set("filename", "tvshow.{ext}")
    filename = formatname(output.copy(), cconfig, extension="nfo")
{'fraction_non_alphanumeric': 0.13814342278926528, 'fraction_numerical': 0.0004399472063352398, 'mean_word_length': 4.220436280137773, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7362100', 'n_tokens_mistral': 1380, 'n_tokens_neox': 1369, 'n_words': 331}