Skip to content

Instantly share code, notes, and snippets.

@Coro365
Created May 21, 2020 11:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Coro365/410521304fbd9f164c1ce3d87bf9dc71 to your computer and use it in GitHub Desktop.
Save Coro365/410521304fbd9f164c1ce3d87bf9dc71 to your computer and use it in GitHub Desktop.
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