Skip to content

Instantly share code, notes, and snippets.

@pioz
Last active September 6, 2023 18:45
Show Gist options
  • Save pioz/504f32c58465d56b12e01a2c4de05ab6 to your computer and use it in GitHub Desktop.
Save pioz/504f32c58465d56b12e01a2c4de05ab6 to your computer and use it in GitHub Desktop.
One Piece Downloader
require 'open-uri'
require 'tty-progressbar'
CONCURRENCY = 8
FIRST_EPISODE_NUMBER = 1
LAST_EPISODE_NUMBER = 628
def get_bounds(episode_number)
episode_number = 1 if episode_number < 1
lower_bound = ((episode_number - 1) / 100) * 100 + 1
upper_bound = lower_bound + 100 - 1
return "#{lower_bound.to_s.rjust(3, '0')}_#{upper_bound.to_s.rjust(3, '0')}"
end
def new_bar(multibar, *arguments)
return multibar.register(*arguments) if multibar
return TTY::ProgressBar.new(*arguments)
end
def download(url, title, multibar: nil)
bar = nil
content_length_proc = ->(total) { bar = new_bar(multibar, "#{title} [:bar] :percent", total: total) if total && total >= 0 }
progress_proc = ->(progress) { bar.current = progress if bar }
URI.open(url, content_length_proc: content_length_proc, progress_proc: progress_proc) do |file|
yield(file)
end
end
def download_episode(episode_number, multibar: nil)
bounds = get_bounds(episode_number)
episode_str = episode_number.to_s.rjust(3, '0')
filename = "#{episode_str}.mp4"
url = "https://serverfile.club/anime/onepiece/ita/#{bounds}/#{filename}"
download(url, "Download episode #{episode_str}", multibar: multibar) do |file|
IO.copy_stream(file, filename)
end
rescue OpenURI::HTTPError => e
puts "#{url}: #{e.message}"
end
def run_download(queue, multibar: nil)
episode_number = queue.pop
if episode_number
download_episode(episode_number, multibar: multibar)
run_download(queue, multibar: multibar)
end
end
# Start here
multibar = TTY::ProgressBar::Multi.new
queue = Queue.new((FIRST_EPISODE_NUMBER..LAST_EPISODE_NUMBER).to_a)
threads = []
CONCURRENCY.times do
threads << Thread.new { run_download(queue, multibar: multibar) }
end
threads.each(&:join)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment