Skip to content

Instantly share code, notes, and snippets.

@Coro365

Coro365/afp_video.rb

Created May 21, 2020
Embed
What would you like to do?
Download afp video
require 'open-uri'
require 'fileutils'
def add_log(recode)
recode = [Time.now, recode + "\n"].join(",\s")
File.write(File.join(__dir__, 'downlod.log'), recode, mode: 'a')
end
def load_log
File.open(File.join(__dir__, 'downlod.log')).read
end
def downlod(url)
dldir = File.join(__dir__, '/mp4')
Dir.mkdir(dldir) unless Dir.exist?(dldir)
Dir.chdir(dldir)
cmd = ['youtube-dl', url]
system(*cmd)
end
def scan_video_articles
url = 'https://www.afpbb.com/subcategory/m_afp_afpmov'
html = URI.open(url).read
reg = %r[a href="/articles/-/(\d{7})" class="block move">]
html.scan(reg).uniq.flatten
end
saved_video_ids = load_log
video_articles = scan_video_articles.reject { |article_id| saved_video_ids.include?(article_id) }
puts("Download #{video_articles.size}video")
video_articles.each do |article_id|
url = "https://www.afpbb.com/articles/-/#{article_id}"
downlod(url) || add_log(article_id)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.