Last active
August 29, 2015 14:12
-
-
Save ababup1192/76d2f6375497ecafb5c3 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
# coding: utf-8 | |
require 'rubygems' | |
require 'parallel' | |
require 'open-uri' | |
require 'timeout' | |
class Main | |
TIME_OUT = 0.7 | |
SHIKII = 20 | |
STEP = 6 | |
# ダウンロードするURL | |
def initialize() | |
if(ARGV.length == 0) then | |
puts "開始の数字を入れてください。" | |
exit | |
else | |
@start = ARGV[0].to_i | |
end | |
@base = 'http://edge.hls.vods.hitbox.tv/static/videos/vods/minoru4160/5d46dadd8ad880ec7c94a9a303747dc545e4f196-54a3e823e4e0f/minoru4160/' | |
@schedules = [] | |
@times = [] | |
STEP.times do |i| | |
@schedules.push(@start - ((i+1)*10000)) | |
end | |
loop{ | |
# ダウンロード可能な数字を列挙 | |
list_enable_videos() | |
if @schedules.empty? then | |
break | |
else | |
p @schedules | |
end | |
} | |
@times.uniq.sort | |
pal_download() | |
puts "完了" | |
end | |
# ダウンロード関数 | |
def download(path, url) | |
open(path, 'wb') do |output| | |
open(url, 'rb') do |data| | |
output.write(data.read) | |
end | |
end | |
end | |
# 使えるビデオ番号を列挙 範囲 start ~ end 見つかったときにすっ飛ばす範囲 interval | |
def list_enable_videos() | |
@schedules.each do |num| | |
# しきい値 | |
area = num-SHIKII .. num+SHIKII | |
Parallel.each(area, in_threads: 8) {|i| | |
if add_enable_video(i) then | |
puts "#{i}: true" | |
@times.push(i) | |
@schedules.delete(num) | |
raise Parallel::Break | |
end | |
} | |
end | |
end | |
# ダウンロード可能なURLの追加 | |
def add_enable_video(time) | |
url = @base + time.to_s + '.ts' | |
begin | |
timeout(TIME_OUT){ | |
open(url) | |
return true | |
} | |
rescue TimeoutError => e; return false | |
rescue => e; return false | |
end | |
end | |
# 並列ダウンロード スレッド数指定 | |
def pal_download | |
Parallel.each(@times, in_threads: 4) {|time| | |
url = @base + time.to_s + '.ts' | |
file_name = File.basename(url) | |
# 保存先を設定 | |
file_path = "./tmp/#{file_name}" | |
puts "Dowload: #{file_path}" | |
# ダウンロード関数呼び出し | |
download(file_path, url) | |
} | |
end | |
end | |
Main.new() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment