# Buggy code
def _ep_data(self, ep_obj):
    """
    Creates an elementTree XML structure for a MediaBrowser style episode.xml
    and returns the resulting data object.

    show_obj: a Series instance to create the NFO for
    """

    eps_to_write = [ep_obj] + ep_obj.related_episodes

    my_show = self._get_show_data(ep_obj.series)
    if not my_show:
        return None

    root_node = etree.Element("details")
    movie = etree.SubElement(root_node, "movie")

    movie.attrib["isExtra"] = "false"
    movie.attrib["isSet"] = "false"
    movie.attrib["isTV"] = "true"

    # write an MediaBrowser XML containing info for all matching episodes
    for ep_to_write in eps_to_write:
        try:
            my_ep = my_show[ep_to_write.season][ep_to_write.episode]
        except (IndexerEpisodeNotFound, IndexerSeasonNotFound):
            log.info(
                "Unable to find episode {ep_num} on {indexer}..."
                " has it been removed? Should I delete from db?",
                {
                    "ep_num": episode_num(ep_to_write.season, ep_to_write.episode),
                    "indexer": indexerApi(ep_obj.series.indexer).name,
                },
            )
            return None

        if ep_to_write == ep_obj:
            # root (or single) episode

            # default to today's date for specials if firstaired is not set
            if ep_to_write.season == 0 and not getattr(my_ep, "firstaired", None):
                my_ep["firstaired"] = text_type(datetime.date.fromordinal(1))

            if not (
                getattr(my_ep, "episodename", None)
                and getattr(my_ep, "firstaired", None)
            ):
                return None

            episode = movie

            if ep_to_write.name:
                episode_name = etree.SubElement(episode, "title")
                episode_name.text = ep_to_write.name

            season_number = etree.SubElement(episode, "season")
            season_number.text = text_type(ep_to_write.season)

            episode_number = etree.SubElement(episode, "episode")
            episode_number.text = text_type(ep_to_write.episode)

            if getattr(my_show, "firstaired", None):
                try:
                    year_text = text_type(
                        datetime.datetime.strptime(
                            my_show["firstaired"], dateFormat
                        ).year
                    )
                    if year_text:
                        year = etree.SubElement(episode, "year")
                        year.text = year_text
                except Exception:
                    pass

            if getattr(my_show, "overview", None):
                plot = etree.SubElement(episode, "plot")
                plot.text = my_show["overview"]

            if ep_to_write.description:
                overview = etree.SubElement(episode, "episodeplot")
                overview.text = ep_to_write.description

            if getattr(my_show, "contentrating", None):
                mpaa = etree.SubElement(episode, "mpaa")
                mpaa.text = my_show["contentrating"]

            if not ep_obj.related_episodes and getattr(my_ep, "rating", None):
                try:
                    rating = int((float(my_ep["rating"]) * 10))
                except ValueError:
                    rating = 0

                if rating:
                    rating = etree.SubElement(episode, "rating")
                    rating.text = text_type(rating)

            if getattr(my_ep, "director", None):
                director = etree.SubElement(episode, "director")
                director.text = my_ep["director"]

            if getattr(my_ep, "writer", None):
                writer = etree.SubElement(episode, "credits")
                writer.text = my_ep["writer"]

            if getattr(my_show, "_actors", None) or getattr(my_ep, "gueststars", None):
                cast = etree.SubElement(episode, "cast")
                if getattr(my_ep, "gueststars", None) and isinstance(
                    my_ep["gueststars"], string_types
                ):
                    for actor in (
                        x.strip() for x in my_ep["gueststars"].split("|") if x.strip()
                    ):
                        cur_actor = etree.SubElement(cast, "actor")
                        cur_actor.text = actor

                if getattr(my_show, "_actors", None):
                    for actor in my_show["_actors"]:
                        if "name" in actor and actor["name"].strip():
                            cur_actor = etree.SubElement(cast, "actor")
                            cur_actor.text = actor["name"].strip()

        else:
            # append data from (if any) related episodes

            if ep_to_write.name:
                if not episode_name.text:
                    episode_name.text = ep_to_write.name
                else:
                    episode_name.text = ", ".join([episode_name.text, ep_to_write.name])

            if ep_to_write.description:
                if not overview.text:
                    overview.text = ep_to_write.description
                else:
                    overview.text = "\r".join([overview.text, ep_to_write.description])

    # Make it purdy
    helpers.indent_xml(root_node)

    data = etree.ElementTree(root_node)

    return data


# Traceback (WindowsError)
2018-04-30 19:05:43 DEBUG    SHOWQUEUE-REFRESH :: [77b2091] 76204: Retrieving/creating episode S02E05
2018-04-30 19:05:43 DEBUG    SHOWQUEUE-REFRESH :: [77b2091] Checking if \\Tower\Media\TV\The Jetsons\Season 02\metadata\The Jetsons - 2x13 - Instant Replay.xml exists: True
2018-04-30 19:05:43 DEBUG    SHOWQUEUE-REFRESH :: [77b2091] Checking if \\Tower\Media\TV\The Jetsons\Season 02\metadata\The Jetsons - 2x13 - Instant Replay.jpg exists: True
2018-04-30 19:05:43 DEBUG    SHOWQUEUE-REFRESH :: [77b2091] Checking if \\Tower\Media\TV\The Jetsons\Season 02\metadata\The Jetsons - 2x13 - Instant Replay.xml exists: True
2018-04-30 19:05:43 DEBUG    SHOWQUEUE-REFRESH :: [77b2091] Checking if \\Tower\Media\TV\The Jetsons\Season 02\metadata\The Jetsons - 2x13 - Instant Replay.jpg exists: True
2018-04-30 19:05:43 DEBUG    SHOWQUEUE-REFRESH :: [77b2091] 76204: Retrieving/creating episode S02E06
2018-04-30 19:05:43 DEBUG    SHOWQUEUE-REFRESH :: [77b2091] Checking if \\Tower\Media\TV\The Jetsons\Season 02\metadata\The Jetsons - 2x19 - Dog Daze Afternoon.xml exists: True
2018-04-30 19:05:43 DEBUG    SHOWQUEUE-REFRESH :: [77b2091] Checking if \\Tower\Media\TV\The Jetsons\Season 02\metadata\The Jetsons - 2x19 - Dog Daze Afternoon.jpg exists: True
2018-04-30 19:05:43 DEBUG    SHOWQUEUE-REFRESH :: [77b2091] Checking if \\Tower\Media\TV\The Jetsons\Season 02\metadata\The Jetsons - 2x19 - Dog Daze Afternoon.xml exists: True
2018-04-30 19:05:43 DEBUG    SHOWQUEUE-REFRESH :: [77b2091] Checking if \\Tower\Media\TV\The Jetsons\Season 02\metadata\The Jetsons - 2x19 - Dog Daze Afternoon.jpg exists: True
2018-04-30 19:05:43 DEBUG    SHOWQUEUE-REFRESH :: [77b2091] 76204: Retrieving/creating episode S02E07
2018-04-30 19:05:43 DEBUG    SHOWQUEUE-REFRESH :: [77b2091] Checking if \\Tower\Media\TV\The Jetsons\Season 02\metadata\The Jetsons - 2x23 - Grandpa and the Galactic Golddigger.xml exists: True
2018-04-30 19:05:43 DEBUG    SHOWQUEUE-REFRESH :: [77b2091] Checking if \\Tower\Media\TV\The Jetsons\Season 02\metadata\The Jetsons - 2x23 - Grandpa and the Galactic Golddigger.jpg exists: True
2018-04-30 19:05:43 DEBUG    SHOWQUEUE-REFRESH :: [77b2091] Checking if \\Tower\Media\TV\The Jetsons\Season 02\metadata\The Jetsons - 2x23 - Grandpa and the Galactic Golddigger.xml exists: True
2018-04-30 19:05:43 DEBUG    SHOWQUEUE-REFRESH :: [77b2091] Checking if \\Tower\Media\TV\The Jetsons\Season 02\metadata\The Jetsons - 2x23 - Grandpa and the Galactic Golddigger.jpg exists: True
2018-04-30 19:05:43 DEBUG    SHOWQUEUE-REFRESH :: [77b2091] 76204: Retrieving/creating episode S02E08
2018-04-30 19:05:43 DEBUG    SHOWQUEUE-REFRESH :: [77b2091] Checking if \\Tower\Media\TV\The Jetsons\Season 02\metadata\The Jetsons - 2x33 - Fugitive Fleas.xml exists: True
2018-04-30 19:05:43 DEBUG    SHOWQUEUE-REFRESH :: [77b2091] Checking if \\Tower\Media\TV\The Jetsons\Season 02\metadata\The Jetsons - 2x33 - Fugitive Fleas.jpg exists: True
2018-04-30 19:05:43 DEBUG    SHOWQUEUE-REFRESH :: [77b2091] Checking if \\Tower\Media\TV\The Jetsons\Season 02\metadata\The Jetsons - 2x33 - Fugitive Fleas.xml exists: True
2018-04-30 19:05:43 DEBUG    SHOWQUEUE-REFRESH :: [77b2091] Checking if \\Tower\Media\TV\The Jetsons\Season 02\metadata\The Jetsons - 2x33 - Fugitive Fleas.jpg exists: True
2018-04-30 19:05:43 DEBUG    SHOWQUEUE-REFRESH :: [77b2091] 76204: Retrieving/creating episode S02E09
2018-04-30 19:05:43 DEBUG    SHOWQUEUE-REFRESH :: [77b2091] Checking if \\Tower\Media\TV\The Jetsons\Season 02\metadata\The Jetsons - 2x27 - Elroy Meets Orbitty.xml exists: True
2018-04-30 19:05:43 DEBUG    SHOWQUEUE-REFRESH :: [77b2091] Checking if \\Tower\Media\TV\The Jetsons\Season 02\metadata\The Jetsons - 2x27 - Elroy Meets Orbitty.jpg exists: True
2018-04-30 19:05:43 DEBUG    SHOWQUEUE-REFRESH :: [77b2091] Checking if \\Tower\Media\TV\The Jetsons\Season 02\metadata\The Jetsons - 2x27 - Elroy Meets Orbitty.xml exists: True
2018-04-30 19:05:43 DEBUG    SHOWQUEUE-REFRESH :: [77b2091] Checking if \\Tower\Media\TV\The Jetsons\Season 02\metadata\The Jetsons - 2x27 - Elroy Meets Orbitty.jpg exists: True
2018-04-30 19:05:43 DEBUG    SHOWQUEUE-REFRESH :: [77b2091] 76204: Retrieving/creating episode S02E10
2018-04-30 19:05:43 DEBUG    SHOWQUEUE-REFRESH :: [77b2091] Checking if \\Tower\Media\TV\The Jetsons\Season 02\metadata\The Jetsons - 2x17 - Judy Takes Off.xml exists: True
2018-04-30 19:05:43 DEBUG    SHOWQUEUE-REFRESH :: [77b2091] Checking if \\Tower\Media\TV\The Jetsons\Season 02\metadata\The Jetsons - 2x17 - Judy Takes Off.jpg exists: True
2018-04-30 19:05:43 DEBUG    SHOWQUEUE-REFRESH :: [77b2091] Checking if \\Tower\Media\TV\The Jetsons\Season 02\metadata\The Jetsons - 2x17 - Judy Takes Off.xml exists: True
2018-04-30 19:05:43 DEBUG    SHOWQUEUE-REFRESH :: [77b2091] Checking if \\Tower\Media\TV\The Jetsons\Season 02\metadata\The Jetsons - 2x17 - Judy Takes Off.jpg exists: True
2018-04-30 19:05:43 DEBUG    SHOWQUEUE-REFRESH :: [77b2091] 76204: Retrieving/creating episode S02E11
2018-04-30 19:05:43 DEBUG    SHOWQUEUE-REFRESH :: [77b2091] Checking if \\Tower\Media\TV\The Jetsons\Season 02\metadata\The Jetsons - 2x24 - Boy George.xml exists: True
2018-04-30 19:05:43 DEBUG    SHOWQUEUE-REFRESH :: [77b2091] Checking if \\Tower\Media\TV\The Jetsons\Season 02\metadata\The Jetsons - 2x24 - Boy George.jpg exists: True
2018-04-30 19:05:43 DEBUG    SHOWQUEUE-REFRESH :: [77b2091] Checking if \\Tower\Media\TV\The Jetsons\Season 02\metadata\The Jetsons - 2x24 - Boy George.xml exists: True
2018-04-30 19:05:43 DEBUG    SHOWQUEUE-REFRESH :: [77b2091] Checking if \\Tower\Media\TV\The Jetsons\Season 02\metadata\The Jetsons - 2x24 - Boy George.jpg exists: True
2018-04-30 19:05:43 DEBUG    SHOWQUEUE-REFRESH :: [77b2091] 76204: Retrieving/creating episode S02E12
2018-04-30 19:05:43 DEBUG    SHOWQUEUE-REFRESH :: [77b2091] Checking if \\Tower\Media\TV\The Jetsons\Season 02\metadata\The Jetsons - 2x12 - S'no Relative.xml exists: True
2018-04-30 19:05:43 DEBUG    SHOWQUEUE-REFRESH :: [77b2091] Checking if \\Tower\Media\TV\The Jetsons\Season 02\metadata\The Jetsons - 2x12 - S'no Relative.jpg exists: True
2018-04-30 19:05:43 DEBUG    SHOWQUEUE-REFRESH :: [77b2091] Checking if \\Tower\Media\TV\The Jetsons\Season 02\metadata\The Jetsons - 2x12 - S'no Relative.xml exists: True
2018-04-30 19:05:43 DEBUG    SHOWQUEUE-REFRESH :: [77b2091] Checking if \\Tower\Media\TV\The Jetsons\Season 02\metadata\The Jetsons - 2x12 - S'no Relative.jpg exists: True
2018-04-30 19:05:43 DEBUG    SHOWQUEUE-REFRESH :: [77b2091] 76204: Retrieving/creating episode S02E13
2018-04-30 19:05:43 DEBUG    SHOWQUEUE-REFRESH :: [77b2091] Checking if \\Tower\Media\TV\The Jetsons\Season 02\metadata\The Jetsons - 2x07 - Rip-Off Rosie.xml exists: True
2018-04-30 19:05:43 DEBUG    SHOWQUEUE-REFRESH :: [77b2091] Checking if \\Tower\Media\TV\The Jetsons\Season 02\metadata\The Jetsons - 2x07 - Rip-Off Rosie.jpg exists: True
2018-04-30 19:05:43 DEBUG    SHOWQUEUE-REFRESH :: [77b2091] Checking if \\Tower\Media\TV\The Jetsons\Season 02\metadata\The Jetsons - 2x07 - Rip-Off Rosie.xml exists: True
2018-04-30 19:05:43 DEBUG    SHOWQUEUE-REFRESH :: [77b2091] Checking if \\Tower\Media\TV\The Jetsons\Season 02\metadata\The Jetsons - 2x07 - Rip-Off Rosie.jpg exists: True
2018-04-30 19:05:43 DEBUG    SHOWQUEUE-REFRESH :: [77b2091] 76204: Retrieving/creating episode S02E14
2018-04-30 19:05:43 DEBUG    SHOWQUEUE-REFRESH :: [77b2091] Checking if \\Tower\Media\TV\The Jetsons\Season 02\metadata\The Jetsons - 2x26 - Space Bong.xml exists: True
2018-04-30 19:05:43 DEBUG    SHOWQUEUE-REFRESH :: [77b2091] Checking if \\Tower\Media\TV\The Jetsons\Season 02\metadata\The Jetsons - 2x26 - Space Bong.jpg exists: True
2018-04-30 19:05:43 DEBUG    SHOWQUEUE-REFRESH :: [77b2091] Checking if \\Tower\Media\TV\The Jetsons\Season 02\metadata\The Jetsons - 2x26 - Space Bong.xml exists: True
2018-04-30 19:05:43 DEBUG    SHOWQUEUE-REFRESH :: [77b2091] Checking if \\Tower\Media\TV\The Jetsons\Season 02\metadata\The Jetsons - 2x26 - Space Bong.jpg exists: True
2018-04-30 19:05:43 DEBUG    SHOWQUEUE-REFRESH :: [77b2091] 76204: Retrieving/creating episode S02E15
2018-04-30 19:05:43 DEBUG    SHOWQUEUE-REFRESH :: [77b2091] Checking if \\Tower\Media\TV\The Jetsons\Season 02\metadata\The Jetsons - 2x32 - Family Fallout.xml exists: True
2018-04-30 19:05:43 DEBUG    SHOWQUEUE-REFRESH :: [77b2091] Checking if \\Tower\Media\TV\The Jetsons\Season 02\metadata\The Jetsons - 2x32 - Family Fallout.jpg exists: True
2018-04-30 19:05:44 DEBUG    SHOWQUEUE-REFRESH :: [77b2091] Checking if \\Tower\Media\TV\The Jetsons\Season 02\metadata\The Jetsons - 2x32 - Family Fallout.xml exists: True
2018-04-30 19:05:44 DEBUG    SHOWQUEUE-REFRESH :: [77b2091] Checking if \\Tower\Media\TV\The Jetsons\Season 02\metadata\The Jetsons - 2x32 - Family Fallout.jpg exists: True
2018-04-30 19:05:44 DEBUG    SHOWQUEUE-REFRESH :: [77b2091] 76204: Retrieving/creating episode S02E16
2018-04-30 19:05:44 DEBUG    SHOWQUEUE-REFRESH :: [77b2091] Checking if \\Tower\Media\TV\The Jetsons\Season 02\metadata\The Jetsons - 2x29 - Solar Snoops.xml exists: True
2018-04-30 19:05:44 DEBUG    SHOWQUEUE-REFRESH :: [77b2091] Checking if \\Tower\Media\TV\The Jetsons\Season 02\metadata\The Jetsons - 2x29 - Solar Snoops.jpg exists: True
2018-04-30 19:05:44 DEBUG    SHOWQUEUE-REFRESH :: [77b2091] Checking if \\Tower\Media\TV\The Jetsons\Season 02\metadata\The Jetsons - 2x29 - Solar Snoops.xml exists: True
2018-04-30 19:05:44 DEBUG    SHOWQUEUE-REFRESH :: [77b2091] Checking if \\Tower\Media\TV\The Jetsons\Season 02\metadata\The Jetsons - 2x29 - Solar Snoops.jpg exists: True
2018-04-30 19:05:44 DEBUG    SHOWQUEUE-REFRESH :: [77b2091] 76204: Retrieving/creating episode S02E17
2018-04-30 19:05:44 DEBUG    SHOWQUEUE-REFRESH :: [77b2091] Checking if \\Tower\Media\TV\The Jetsons\Season 02\metadata\The Jetsons - 2x14 - Haunted Halloween.xml exists: True
2018-04-30 19:05:44 DEBUG    SHOWQUEUE-REFRESH :: [77b2091] Checking if \\Tower\Media\TV\The Jetsons\Season 02\metadata\The Jetsons - 2x14 - Haunted Halloween.jpg exists: True
2018-04-30 19:05:44 DEBUG    SHOWQUEUE-REFRESH :: [77b2091] Checking if \\Tower\Media\TV\The Jetsons\Season 02\metadata\The Jetsons - 2x14 - Haunted Halloween.xml exists: True
2018-04-30 19:05:44 DEBUG    SHOWQUEUE-REFRESH :: [77b2091] Checking if \\Tower\Media\TV\The Jetsons\Season 02\metadata\The Jetsons - 2x14 - Haunted Halloween.jpg exists: True
2018-04-30 19:05:44 DEBUG    SHOWQUEUE-REFRESH :: [77b2091] 76204: Retrieving/creating episode S02E18
2018-04-30 19:05:44 DEBUG    SHOWQUEUE-REFRESH :: [77b2091] Episode location missing: \\Tower\Media\TV\The Jetsons\Season 02\The Jetsons - 2x41 - Elroy in Wonderland.mkv
2018-04-30 19:05:44 DEBUG    SHOWQUEUE-REFRESH :: [77b2091] Matched release \\Tower\Media\TV\The Jetsons\Season 02\The Jetsons - 2x41 - Elroy in Wonderland to a series in your database: The Jetsons
2018-04-30 19:05:44 DEBUG    SHOWQUEUE-REFRESH :: [77b2091] Parsed \\Tower\Media\TV\The Jetsons\Season 02\The Jetsons - 2x41 - Elroy in Wonderland into title: The Jetsons, season: 2, episode: [41], episode_title: Elroy in Wonderland, type: episode, parsing_time: 0.124000072479, total_time: 0.125, absolute_episode: [], quality: Unknown
2018-04-30 19:05:44 DEBUG    SHOWQUEUE-REFRESH :: [77b2091] Found codec for The Jetsons A Jetson Christmas Carol
2018-04-30 19:05:44 DEBUG    SHOWQUEUE-REFRESH :: [77b2091] 76204: Formatting pattern: %SN - S%0SE18 - %EN -> The Jetsons - S02E18 - A Jetson Christmas Carol
2018-04-30 19:05:44 DEBUG    SHOWQUEUE-REFRESH :: [77b2091] Metadata provider MediaBrowser creating episode metadata for The Jetsons - S02E18 - A Jetson Christmas Carol
2018-04-30 19:05:44 DEBUG    SHOWQUEUE-REFRESH :: [77b2091] Episode location missing: \\Tower\Media\TV\The Jetsons\Season 02\The Jetsons - 2x41 - Elroy in Wonderland.mkv
2018-04-30 19:05:44 DEBUG    SHOWQUEUE-REFRESH :: [77b2091] Metadata directory missing, creating it at 
2018-04-30 19:05:44 ERROR    SHOWQUEUE-REFRESH :: [77b2091] 76204: Error while refreshing show The Jetsons. Error: 
Traceback (most recent call last):
  File "C:\Medusa\Medusa\medusa\show_queue.py", line 640, in run
    self.show.write_metadata()
  File "C:\Medusa\Medusa\medusa\tv\series.py", line 856, in write_metadata
    self.__write_episode_nfos()
  File "C:\Medusa\Medusa\medusa\tv\series.py", line 885, in __write_episode_nfos
    cur_ep.create_meta_files()
  File "C:\Medusa\Medusa\medusa\tv\episode.py", line 1110, in create_meta_files
    self.__create_nfo(metadata_provider)
  File "C:\Medusa\Medusa\medusa\tv\episode.py", line 1124, in __create_nfo
    result = metadata_provider.create_episode_metadata(self) or result
  File "C:\Medusa\Medusa\medusa\metadata\generic.py", line 254, in create_episode_metadata
    return self.write_ep_file(ep_obj)
  File "C:\Medusa\Medusa\medusa\metadata\generic.py", line 469, in write_ep_file
    os.makedirs(nfo_file_dir)
  File "C:\Medusa\Medusa\medusa\init\filesystem.py", line 113, in patched_output
    return handle_output(f(*args, **kwargs))
  File "C:\Medusa\Python\lib\os.py", line 157, in makedirs
    mkdir(name, mode)
  File "C:\Medusa\Medusa\medusa\init\filesystem.py", line 113, in patched_output
    return handle_output(f(*args, **kwargs))
WindowsError: [Error 3] The system cannot find the path specified: u''

# Fixed code
def _ep_data(self, ep_obj):
    """
    Creates an elementTree XML structure for a MediaBrowser style episode.xml
    and returns the resulting data object.

    show_obj: a Series instance to create the NFO for
    """

    eps_to_write = [ep_obj] + ep_obj.related_episodes

    my_show = self._get_show_data(ep_obj.series)
    if not my_show:
        return None

    root_node = etree.Element("details")
    movie = etree.SubElement(root_node, "movie")

    movie.attrib["isExtra"] = "false"
    movie.attrib["isSet"] = "false"
    movie.attrib["isTV"] = "true"

    # write an MediaBrowser XML containing info for all matching episodes
    for ep_to_write in eps_to_write:
        try:
            my_ep = my_show[ep_to_write.season][ep_to_write.episode]
        except (IndexerEpisodeNotFound, IndexerSeasonNotFound):
            log.info(
                "Unable to find episode {ep_num} on {indexer}..."
                " has it been removed? Should I delete from db?",
                {
                    "ep_num": episode_num(ep_to_write.season, ep_to_write.episode),
                    "indexer": indexerApi(ep_obj.series.indexer).name,
                },
            )
            return None

        if ep_to_write == ep_obj:
            # root (or single) episode

            # default to today's date for specials if firstaired is not set
            if ep_to_write.season == 0 and not getattr(my_ep, "firstaired", None):
                my_ep["firstaired"] = str(datetime.date.fromordinal(1))

            if not (
                getattr(my_ep, "episodename", None)
                and getattr(my_ep, "firstaired", None)
            ):
                return None

            episode = movie

            if ep_to_write.name:
                episode_name = etree.SubElement(episode, "title")
                episode_name.text = ep_to_write.name

            season_number = etree.SubElement(episode, "season")
            season_number.text = str(ep_to_write.season)

            episode_number = etree.SubElement(episode, "episode")
            episode_number.text = str(ep_to_write.episode)

            if getattr(my_show, "firstaired", None):
                try:
                    year_text = str(
                        datetime.datetime.strptime(
                            my_show["firstaired"], dateFormat
                        ).year
                    )
                    if year_text:
                        year = etree.SubElement(episode, "year")
                        year.text = year_text
                except Exception:
                    pass

            if getattr(my_show, "overview", None):
                plot = etree.SubElement(episode, "plot")
                plot.text = my_show["overview"]

            if ep_to_write.description:
                overview = etree.SubElement(episode, "episodeplot")
                overview.text = ep_to_write.description

            if getattr(my_show, "contentrating", None):
                mpaa = etree.SubElement(episode, "mpaa")
                mpaa.text = my_show["contentrating"]

            if not ep_obj.related_episodes and getattr(my_ep, "rating", None):
                try:
                    rating = int((float(my_ep["rating"]) * 10))
                except ValueError:
                    rating = 0

                if rating:
                    rating = etree.SubElement(episode, "rating")
                    rating.text = str(rating)

            if getattr(my_ep, "director", None):
                director = etree.SubElement(episode, "director")
                director.text = my_ep["director"]

            if getattr(my_ep, "writer", None):
                writer = etree.SubElement(episode, "credits")
                writer.text = my_ep["writer"]

            if getattr(my_show, "_actors", None) or getattr(my_ep, "gueststars", None):
                cast = etree.SubElement(episode, "cast")
                if getattr(my_ep, "gueststars", None) and isinstance(
                    my_ep["gueststars"], string_types
                ):
                    for actor in (
                        x.strip() for x in my_ep["gueststars"].split("|") if x.strip()
                    ):
                        cur_actor = etree.SubElement(cast, "actor")
                        cur_actor.text = actor

                if getattr(my_show, "_actors", None):
                    for actor in my_show["_actors"]:
                        if "name" in actor and actor["name"].strip():
                            cur_actor = etree.SubElement(cast, "actor")
                            cur_actor.text = actor["name"].strip()

        else:
            # append data from (if any) related episodes

            if ep_to_write.name:
                if not episode_name.text:
                    episode_name.text = ep_to_write.name
                else:
                    episode_name.text = ", ".join([episode_name.text, ep_to_write.name])

            if ep_to_write.description:
                if not overview.text:
                    overview.text = ep_to_write.description
                else:
                    overview.text = "\r".join([overview.text, ep_to_write.description])

    # Make it purdy
    helpers.indent_xml(root_node)

    data = etree.ElementTree(root_node)

    return data