newsboat-export-opml-tagged (1248B)
1 #!/usr/bin/env ruby 2 # Exports a newsboat URLs file to an OPML, with tags, for use in e.g. Miniflux. 3 ( warn "Usage: newsboat-export-opml-tagged /path/to/urls /path/to/destination.opml"; exit 1 ) unless ARGV.length == 2 4 ( warn "#{ARGV[0]} is not readable."; exit 1 ) unless File.readable? ARGV[0] 5 urls = File.readlines(ARGV[0]) 6 .reject { |u| u.start_with?('"query:') || u =~ /^#/ || u =~ /^\s*$/} 7 .map { |line| line.chomp.split(/\s+(?=")/).map { |e| e.gsub('"', '') } } 8 9 tags = urls.flatten.select { |u| u !~ /^https?/ && u !~ /^~/ }.uniq 10 11 urls = urls.map do |u| 12 name=u.select { |e| e =~ /^~/ }.first.gsub("~", "") 13 url=u.select { |e| e =~ /^http/ }.first 14 {name: name, url: url, tags: u-[name]-[url]} 15 end 16 17 urls_by_tag = {} 18 tags.each do |tag| 19 urls_by_tag[tag] = urls.select { |u| u[:tags].include? tag } 20 end 21 22 outlines = "" 23 outlines += <<~EOF 24 <?xml version="1.0" encoding="UTF-8"?> 25 <opml version="2.0"> 26 <body> 27 EOF 28 urls_by_tag.each do |tag,urls| 29 outlines += "<outline text=\"#{tag}\">\n" 30 urls.each do |url| 31 outlines += "<outline title=\"#{url[:name]}\" text=\"#{url[:name]}\" xmlUrl=\"#{url[:url]}\"></outline>\n" 32 end 33 outlines += "</outline>\n" 34 end 35 36 outlines += <<~EOF 37 </body> 38 </opml> 39 EOF 40 41 File.write(ARGV[1], outlines)