This repository has been archived on 2025-07-13. You can view files and clone it, but you cannot make any changes to it's state, such as pushing and creating new issues, pull requests or comments.
rss2ydl/rss2ydl.rb

56 lines
1.5 KiB
Ruby
Raw Normal View History

2018-04-29 15:22:53 +03:00
#!/bin/env ruby
require 'rss'
require 'open-uri'
require 'youtube-dl'
2018-04-29 16:30:42 +03:00
require 'sequel'
require 'date'
2018-04-29 17:10:48 +03:00
require 'optparse'
2018-04-30 00:00:55 +03:00
require 'filesize'
2018-04-29 15:22:53 +03:00
2018-04-29 17:10:48 +03:00
options={}
OptionParser.new do |parser|
parser.on("-f", "--feed FEED", "Feed to download videos from") do |feed|
options[:feed]=feed
end
2018-04-30 00:00:55 +03:00
parser.on("-m", "--max-storage SIZE", "Maximum total size on disk to occupy with videos") do |maxstorage|
options[:maxstorage]=maxstorage
end
2018-04-29 17:10:48 +03:00
end.parse!
2018-04-29 15:22:53 +03:00
2018-04-29 17:10:48 +03:00
raise OptionParser::MissingArgument, "You must specify a feed (-f)" if options[:feed].nil?
2018-04-29 15:22:53 +03:00
2018-04-29 16:30:42 +03:00
DB = Sequel.connect('sqlite://videos.db')
DB.create_table? :videos do
primary_key :id
String :url
String :filename
Integer :filesize
DateTime :downloaded
end
videos=DB[:videos]
2018-04-29 17:10:48 +03:00
open(options[:feed]) do |rss|
2018-04-29 15:22:53 +03:00
feed=RSS::Parser.parse(rss)
feed.items.each do |item|
2018-04-29 23:42:41 +03:00
if not videos.where(:url => "#{item.link}").count.eql? 0
next
end
2018-04-29 16:30:42 +03:00
video=YoutubeDL.download "#{item.link}"
videos.insert(:url => "#{item.link}",
:filename => "#{video.filename}",
:filesize => File.size("#{video.filename}"),
:downloaded => DateTime.now)
if not options[:maxstorage].empty?
2018-04-30 00:00:55 +03:00
while videos.sum(:filesize) > Filesize.from(options[:maxstorage]).to_i
oldest=videos.order(:downloaded).first
File.delete(oldest[:filename])
videos.where(:id => oldest[:id]).delete
end
end
2018-04-29 15:22:53 +03:00
end
end