<commit_before># This file should contain all the record creation needed to seed the database with its default values.
# The data can then be loaded with the rails db:seed command (or created alongside the database with db:setup).
#
# Examples:
#
#   movies = Movie.create([{ name: 'Star Wars' }, { name: 'Lord of the Rings' }])
#   Character.create(name: 'Luke', movie: movies.first)

query = {
  "api_key" => ENV['TMDB_API_KEY'],
  "language" => 'en-US',
  "sort_by" => "popularity.desc"
}

response = HTTParty.get(
  "https://api.themoviedb.org/3/discover/tv",
  :query => query
)

SAVED_SHOW_PROPERTIES = %w( name overview id )

response.parsed_response['results'].each {|fetched_show|
  Show.find_or_create_by(tmdb_id: fetched_show['id']) do |show|
    show.name = fetched_show['name']
    show.overview = fetched_show['overview']
    show.tmdb_id = fetched_show['id']
    show.tmdb_data = fetched_show.reject {|k,v| SAVED_SHOW_PROPERTIES.include?(k)}
  end
}<commit_msg>Avoid running seeds when the TMDB API key is unavailable<commit_after># This file should contain all the record creation needed to seed the database with its default values.
# The data can then be loaded with the rails db:seed command (or created alongside the database with db:setup).
#
# Examples:
#
#   movies = Movie.create([{ name: 'Star Wars' }, { name: 'Lord of the Rings' }])
#   Character.create(name: 'Luke', movie: movies.first)

unless ENV.has_key? "TMDB_API_KEY" or ENV.fetch === ""
  query = {
    "api_key" => ENV["TMDB_API_KEY"],
    "language" => "en-US",
    "sort_by" => "popularity.desc",
  }

  response = HTTParty.get(
    "https://api.themoviedb.org/3/discover/tv",
    :query => query,
  )

  SAVED_SHOW_PROPERTIES = %w( name overview id )

  response.parsed_response["results"].each { |fetched_show|
    Show.find_or_create_by(tmdb_id: fetched_show["id"]) do |show|
      show.name = fetched_show["name"]
      show.overview = fetched_show["overview"]
      show.tmdb_id = fetched_show["id"]
      show.tmdb_data = fetched_show.reject { |k, v| SAVED_SHOW_PROPERTIES.include?(k) }
    end
  }
end
{'fraction_non_alphanumeric': 0.14122137404580154, 'fraction_numerical': 0.0009541984732824427, 'mean_word_length': 4.1905940594059405, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1138748', 'n_tokens_mistral': 761, 'n_tokens_neox': 742, 'n_words': 186}