<commit_before>defmodule Animu.Media.Series.Collect do
  import Animu.Media.Kitsu

  alias HTTPoison.Response
  alias Animu.Media.Series.Bag

  def collect_kitsu_data(%Bag{} = bag) do
    with {:ok, bag} <- request_kitsu_data(bag),
         {:ok, bag} <- request_kitsu_episode_data(bag),
         {:ok, bag} <- download_poster_data(bag),
         {:ok, bag} <- download_cover_data(bag),
         do: {:ok, bag}
  end

  defp request_kitsu_data(%Bag{} = bag) do
    case request("anime", bag.kitsu_id) do
      {:ok, kitsu_data} ->
        kitsu_data = format_to_series(kitsu_data)
        {:ok, Bag.apply_kitsu_data(bag, kitsu_data)}
      {:error, reason} ->
        {:error, reason}
    end
  end

  defp request_kitsu_episode_data(%Bag{} = bag) do
    case request_relationship("anime", "episodes", bag.kitsu_id) do
      {:ok, episodes} ->
        episodes = Enum.map(episodes, &format_to_episode/1)
        {:ok, Map.put(bag, :episodes, episodes)}
      {:error, reason} ->
        {:error, reason}
    end
  end

  defp download_poster_data(%Bag{} = bag) do
    poster_data =
      Enum.reduce_while(bag.kitsu_data.poster_urls, %{}, &fetch_url/2)
    case poster_data do
      {:error, reason} -> {:error, reason}
      poster_data -> {:ok, Map.put(bag, :poster_image, poster_data)}<commit_msg>Fixed populate overwriting existing ep video field<commit_after>defmodule Animu.Media.Series.Collect do
  import Animu.Media.Kitsu

  alias HTTPoison.Response
  alias Animu.Media.Series.Bag

  def collect_kitsu_data(%Bag{} = bag) do
    with {:ok, bag} <- request_kitsu_data(bag),
         {:ok, bag} <- request_kitsu_episode_data(bag),
         {:ok, bag} <- download_poster_data(bag),
         {:ok, bag} <- download_cover_data(bag),
         do: {:ok, bag}
  end

  defp request_kitsu_data(%Bag{} = bag) do
    case request("anime", bag.kitsu_id) do
      {:ok, kitsu_data} ->
        kitsu_data = format_to_series(kitsu_data)
        {:ok, Bag.apply_kitsu_data(bag, kitsu_data)}
      {:error, reason} ->
        {:error, reason}
    end
  end

  defp request_kitsu_episode_data(%Bag{} = bag) do
    case request_relationship("anime", "episodes", bag.kitsu_id) do
      {:ok, episodes} ->
        episodes = Enum.map(episodes, &format_to_episode/1)
        {:ok, Map.put(bag, :kitsu_eps, episodes)}
      {:error, reason} ->
        {:error, reason}
    end
  end

  defp download_poster_data(%Bag{} = bag) do
    poster_data =
      Enum.reduce_while(bag.kitsu_data.poster_urls, %{}, &fetch_url/2)
    case poster_data do
      {:error, reason} -> {:error, reason}
      poster_data -> {:ok, Map.put(bag, :poster_image, poster_data)}
{'fraction_non_alphanumeric': 0.1417740712661107, 'fraction_numerical': 0.001516300227445034, 'mean_word_length': 3.078825347758887, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2742858', 'n_tokens_mistral': 1017, 'n_tokens_neox': 970, 'n_words': 193}