Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env ruby
- # youtube-rss-dl
- # Customize CHANNELS and DESTINATION, then add to cron.
- require 'logger'
- require 'json'
- require 'open-uri'
- require 'simple-rss'
- require 'youtube-dl.rb'
- PROGNAME = '.youtube-rss-dl'
- VERSION = '1.0.2'
- SELFDIR = File.expand_path(File.dirname(__FILE__))
- RSS_BASE_URL = 'https://www.youtube.com/feeds/videos.xml?channel_id='
- CHANNELS = {
- 'ADVChina' => 'UCwNPa8fSXzzAZuT9859GVhg',
- 'Brian Lunduke' => 'UCkK9UDm_ZNrq_rIXCz3xCGA',
- 'Lazy Game Reviews' => 'UCLx053rWZxCiYWsBETgdKrQ',
- 'Linus Tech Tips' => 'UCXuqSBlHAE6Xw-yeJA0Tunw',
- 'Marques Brownlee' => 'UCBJycsmduvYEL83R_U4JriQ',
- 'Primitave Technology' => 'UCAL3JXZSzSm8AlZyD3nQdBA'
- }
- DESTINATION = SELFDIR
- YOUTUBEDL_OPTIONS = {
- force_ipv4: true,
- format: 'bestvideo[ext=mp4][width<=1920][height<=1080]+bestaudio[ext=m4a]',
- add_metadata: true,
- xattrs: true,
- embed_thumbnail: true,
- output: "#{DESTINATION}/%(uploader)s - %(upload_date)s - %(title)s.%(ext)s"
- }
- # load/create log file
- logger = Logger.new(File.join(SELFDIR, PROGNAME + '.log'))
- logger.level = Logger::WARN
- # load/create database json
- json = File.join(SELFDIR, PROGNAME + '.json')
- File.write(json, JSON.generate({})) unless File.file?(json)
- database = JSON.parse(File.read(json))
- # parse each channel
- CHANNELS.each do |channel_name, channel_id|
- # check hash key
- database[channel_name] ||= []
- puts "Checking: #{channel_name}"
- # get the feed
- begin
- rss = SimpleRSS.parse open(RSS_BASE_URL + channel_id)
- rescue => e
- logger.error('RSS') {
- "'#{channel_name}':#{channel_id} - #{e}"
- }
- raise e
- end
- # parse each rss item
- rss.items.each do |video|
- next if database[channel_name].include?(video.id)
- puts " Downloading: #{video.title}"
- # download video
- begin
- ### ### ### ### Comment next line to update JSON without downloading
- YoutubeDL.download video.link, YOUTUBEDL_OPTIONS
- logger.info('YoutubeDL') {
- "'#{channel_name}' '#{video.title}' OK"
- }
- rescue => e
- logger.error('YoutubeDL') {
- "'#{channel_name}':#{channel_id} #{video.id}:#{video.link} - #{e}"
- }
- raise e
- end
- # add video id to json
- database[channel_name] << video.id
- File.write(json, JSON.pretty_generate(database))
- end
- end
Add Comment
Please, Sign In to add comment