Skip to content

Instantly share code, notes, and snippets.

@lucivaldo
Created October 30, 2016 19:55
Show Gist options
  • Save lucivaldo/d31ceb41c17dc8525750bbc5e6ae94ac to your computer and use it in GitHub Desktop.
Save lucivaldo/d31ceb41c17dc8525750bbc5e6ae94ac to your computer and use it in GitHub Desktop.
Fazer download dos vídeos de Dragon Ball Super do site de animes http://www.animakai.tv/
require 'open-uri'
require 'open_uri_redirections'
require 'nokogiri'
require 'pry'
LINKS_FILE = 'links.txt'
BASE_URL = 'http://www.animakai.tv'
NAMESPACE = 'anime/1877'
VIDEOS_DIRECTORY = 'videos'
URL = "#{BASE_URL}/#{NAMESPACE}"
def download_video(link, file_name)
puts "Downloading video #{file_name}..."
File.open("#{VIDEOS_DIRECTORY}/#{file_name}.mp4", 'wb') do |file|
file << open(link, allow_redirections: :safe).read
end
puts "Donwload complete\n"
end
unless File.file?(LINKS_FILE)
home_html = Nokogiri::HTML(open(URL))
episode_links = []
home_html.css('.item-pg-anime').each do |item_pg_anime|
link = item_pg_anime.at_css('a:first-child')
episode_links << link['href']
end
download_links = []
episode_links.each do |episode_link|
episode_num = episode_link.split('/').last
episode_url = "#{BASE_URL}#{episode_link}"
episode_html = Nokogiri::HTML(open(episode_url))
download_link = episode_html.at_css("source[type^='video/mp4']")
download_links << [download_link['src'], episode_num]
puts download_link['src']
end
Dir.mkdir VIDEOS_DIRECTORY unless Dir.exist?(VIDEOS_DIRECTORY)
download_links.delete_if { |link| link[0].strip.empty? }
download_links.each do |link|
File.open(LINKS_FILE, 'a') { |file| file.puts("#{link[0]}, #{link[1]}") }
end
download_links.each { |link| download_video(link[0], link[1]) }
else
File.foreach(LINKS_FILE) do |line|
link, file_name = line.split(',').map(&:strip)
download_video(link, file_name)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment