This repository has been archived by the owner on Aug 30, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
opmldiff
executable file
·93 lines (74 loc) · 2.45 KB
/
opmldiff
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#!/usr/bin/ruby
# ompldiff
# Author: Steve Hoeksema <[email protected]>
# http://www.seven.net.nz
# License: Creative Commons Attribution-Share Alike 3.0 New Zealand
# http://creativecommons.org/licenses/by-sa/3.0/nz/
require 'pathname'
require 'open-uri'
require 'rubygems'
require "rexml/document"
require 'builder'
class Pair
attr_accessor :opml1, :opml2, :backup
def initialize(opml1, opml2)
@opml1, @opml2 = Pathname.new(opml1), Pathname.new(opml2)
@backup = @opml1.dirname + (@opml1.basename.to_s + '.backup')
end
end
pairs = []
feeds = []
unless ARGV.size.modulo(2) == 0 && ARGV.size > 0
$stderr.puts "opmldiff 0.1"
$stderr.puts "Expects pairs of OPML files, the first which must be a local file, and the second can be a URL or a local file."
$stderr.puts
$stderr.puts "Usage:"
$stderr.puts " opmldiff <OPML1> <OPML2>[ <OPML3> <OPML4>...]"
$stderr.puts "Example:"
$stderr.puts " opmldiff /path/to/opml.xml http://www.example.com/opml.xml"
$stderr.puts
exit
end
(0...ARGV.size).step(2) do |i|
pairs << Pair.new(ARGV[i], ARGV[i+1])
end
for pair in pairs
raise "Original OPML file must be readable" unless pair.opml1.readable?
raise "Original OPML file must be writable" unless pair.opml1.writable?
if pair.backup.exist?
raise "Backup OPML file must be writable" unless pair.backup.writable?
else
raise "Backup directory must be writable" unless pair.backup.dirname.writable?
end
existing_feed_urls = []
existing_opml = REXML::Document.new open(pair.opml1.to_s)
existing_opml.elements.each('/opml/body/outline') do |feed|
existing_feed_urls << feed.attributes['xmlUrl']
end
new_opml = REXML::Document.new open(pair.opml2.to_s)
new_opml.elements.each('/opml/body/outline') do |feed|
feeds << feed unless existing_feed_urls.include? feed.attributes['xmlUrl']
end
File.open(pair.backup, 'w') { |f| f.write(existing_opml.to_s) }
File.open(pair.opml1, 'w') { |f| f.write(new_opml.to_s) }
end
xml = Builder::XmlMarkup.new(:target => $stdout, :indent => 2)
xml.instruct!
xml.opml do
xml.head do
xml.title "OPMLDiff"
xml.dateModified Time.now
xml.ownerName "Steve Hoeksema"
xml.ownerEmail "[email protected]"
end
xml.body do
for feed in feeds do
xml.outline(
:type => feed.attributes['type'],
:text => feed.attributes['text'],
:title => feed.attributes['title'],
:xmlUrl => feed.attributes['xmlUrl']
)
end
end
end