Created
November 14, 2014 03:57
-
-
Save NuckChorris/a4a34a58942b60dc5153 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
desc "Bulk import info from TVDB" | |
task :bulk_import_tvdb, [:shows] => [:environment] do |t, args| | |
require 'parallel' | |
ActiveRecord::Base.connection_pool.disconnect! | |
config = ActiveRecord::Base.configurations[Rails.env] | |
config['pool'] = 20 | |
ActiveRecord::Base.establish_connection(config) | |
# This fixes some threaded autoloading issues | |
Episode | |
$results = { | |
failure: 0, | |
success: 0 | |
} | |
KEY = '21F012C4F48D4288' | |
def tvdb(path) | |
hash = Hash.from_xml(open("http://thetvdb.com/api/#{KEY}/#{path}")) | |
hash.deep_transform_keys! { |k| k.to_s.underscore.to_sym } | |
hash[:data] | |
end | |
def tvdb_series(id) | |
tvdb("series/#{id}/all/") | |
end | |
shows = Anime.select(:id, :title, :alt_title, :thetvdb_series_id, :thetvdb_season_id, :episode_count) | |
.where.not(thetvdb_series_id: "") | |
Parallel.each(shows, in_threads: 8) do |anime| | |
tvdb_anime = tvdb_series(anime.thetvdb_series_id) | |
season_id = anime.thetvdb_season_id.split(',') | |
season_id = nil if season_id[0] == '' | |
tvdb_season = tvdb_anime[:episode].select do |ep| | |
if season_id[0] == "ALL" | |
ep[:season_number].to_i >= 1 | |
elsif !season_id.nil? | |
season_id.include?(ep[:seasonid]) | |
else | |
ep[:season_number].to_i == 1 | |
end && ep[:episode_number].to_i >= 1 | |
end | |
# We don't wanna fuck with the episode count unless we expect to import "all" | |
if anime.episode_count == tvdb_season.count || season_id[0] == 'ALL' | |
tvdb_season.each do |ep| | |
thumb_url = URI("http://thetvdb.com/banners/#{ep[:filename]}") | |
thumb_type = Net::HTTP.start(thumb_url.host, thumb_url.port).head(thumb_url)['Content-Type'] | |
Episode.create_or_update_from_hash({ | |
anime: anime, | |
episode: ep[:episode_number], | |
season: ep[:season_number], | |
title: ep[:episode_name], | |
synopsis: ep[:description], | |
thumbnail: (thumb_url if thumb_type.starts_with? 'image/') | |
}) | |
end | |
$results[:success] += 1 | |
else | |
$stderr.printf "%s Count Mismatch (%d != %d, SeriesID=%s)\n", anime.title, anime.episode_count, tvdb_season.count, anime.thetvdb_series_id | |
$results[:failure] += 1 | |
end | |
end | |
$stderr.printf "Success: %s | Failure: %s\n", $results[:success], $results[:failure] | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment