trackchange 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in trackchange.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 phil
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,55 @@
1
+ # Trackchange
2
+
3
+ Track changes to websites by probing them on a regular basis. Changes
4
+ will be reported via email and/or RSS feed.
5
+
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ gem 'trackchange'
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install trackchange
20
+
21
+
22
+ ## Usage
23
+
24
+ # Set email
25
+ trackchange email <email>
26
+
27
+ # Set path to store RSS feed
28
+ trackchange rss <path>
29
+
30
+ # Add site
31
+ trackchange add <url>
32
+
33
+ # List sites
34
+ trackchange list
35
+
36
+ # Remove site
37
+ trackchange remove <pos>
38
+
39
+ # Probe
40
+ trackchange probe
41
+
42
+ # Install probe into crontab
43
+ trackchange install
44
+
45
+ # Uninstall probe from crontab
46
+ trackchange uninstall
47
+
48
+
49
+ ## Contributing
50
+
51
+ 1. Fork it
52
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
53
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
54
+ 4. Push to the branch (`git push origin my-new-feature`)
55
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/bin/trackchange ADDED
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ begin
4
+ require File.expand_path('../../lib/trackchange', __FILE__)
5
+ Trackchange::Exec.run(ARGV)
6
+ rescue Exception => e
7
+ raise e if ENV['DEBUG']
8
+ warn e.message
9
+ exit 1
10
+ end
@@ -0,0 +1,10 @@
1
+ $:.unshift File.dirname(__FILE__)
2
+
3
+ require "trackchange/version"
4
+ require "trackchange/exec"
5
+ require "trackchange/probe"
6
+ require "trackchange/dependencies"
7
+
8
+ module Trackchange
9
+ # Your code goes here...
10
+ end
@@ -0,0 +1,22 @@
1
+ module Trackchange
2
+ class Dependencies
3
+
4
+ class << self
5
+
6
+ def check
7
+ quit('no diff?') if %x[ which diff ].empty?
8
+ quit('no lynx?') if %x[ which lynx ].empty?
9
+ quit('no mail?') if %x[ which mail ].empty?
10
+ end
11
+
12
+ def quit(msg)
13
+ puts msg
14
+ exit 1
15
+ end
16
+
17
+ end
18
+
19
+ end
20
+ end
21
+
22
+ Trackchange::Dependencies.check
@@ -0,0 +1,91 @@
1
+ require 'fileutils'
2
+ require 'ostruct'
3
+ require 'yaml'
4
+
5
+ require 'cronedit'
6
+
7
+ module Trackchange
8
+ class Exec < Struct.new(:args)
9
+
10
+ CRON_LINE = '0 7 * * * trackchange probe >/dev/null'
11
+
12
+ class << self
13
+ def run(args)
14
+ new(args).run
15
+ end
16
+ end
17
+
18
+ def run
19
+ FileUtils.mkdir_p(path) unless File.exist?(path)
20
+ cmd = args.shift
21
+ raise "No command" unless cmd
22
+ return send(cmd) if respond_to?(cmd)
23
+ raise "Unknown command #{cmd}"
24
+ end
25
+
26
+ # commands
27
+
28
+ def probe
29
+ Probe.run(config)
30
+ end
31
+
32
+ def email
33
+ config.email = args.first
34
+ store_config!
35
+ end
36
+
37
+ def rss
38
+ config.rss_path = args.first
39
+ store_config!
40
+ end
41
+
42
+ def add
43
+ config.sites ||= []
44
+ config.sites |= [ args.first ]
45
+ store_config!
46
+ end
47
+
48
+ def list
49
+ config.sites.each_with_index do |url, pos|
50
+ puts "% 4s %s" % [pos+1, url]
51
+ end
52
+ end
53
+
54
+ def remove
55
+ pos = args.first.to_i - 1
56
+ raise "Invalid position" if pos == -1
57
+ config.sites.delete_at(pos)
58
+ store_config!
59
+ end
60
+
61
+ def install
62
+ CronEdit::Crontab.Add('trackchange', CRON_LINE)
63
+ end
64
+
65
+ def uninstall
66
+ CronEdit::Crontab.Remove('trackchange')
67
+ end
68
+
69
+ private
70
+
71
+ def path
72
+ File.expand_path('~/.trackchange')
73
+ end
74
+
75
+ def config_path
76
+ File.join(path, 'config.yml')
77
+ end
78
+
79
+ def config
80
+ return @config if @config
81
+ data = { version: VERSION }
82
+ data = YAML.load(File.read(config_path)) if File.exist?(config_path)
83
+ @config = OpenStruct.new(data)
84
+ end
85
+
86
+ def store_config!
87
+ File.open(config_path, 'w') { |f| f.print(YAML.dump(config.marshal_dump)) }
88
+ end
89
+
90
+ end
91
+ end
@@ -0,0 +1,78 @@
1
+ require 'logger'
2
+ require 'fileutils'
3
+ require 'tempfile'
4
+ require 'rss'
5
+
6
+ module Trackchange
7
+ class Probe < Struct.new(:config)
8
+
9
+ class << self
10
+ def run(config)
11
+ new(config).run
12
+ end
13
+ end
14
+
15
+ def run
16
+ config.sites.each { |site| probe(site) }
17
+ end
18
+
19
+ def probe(site)
20
+ fname = site.sub(%r{https?://}, '').tr_s('/?&', '...')
21
+ site_path = File.expand_path(fname, '~/.trackchange')
22
+ lynx = "lynx -dump '#{site}' | uniq > #{site_path}.new"
23
+ logger.debug "% #{lynx}"
24
+ system lynx
25
+
26
+ unless File.exist?(site_path) # new site
27
+ logger.warn "new site #{site}"
28
+ FileUtils.mv("#{site_path}.new", site_path)
29
+ return
30
+ end
31
+
32
+ diff = "diff -u #{site_path} #{site_path}.new"
33
+ logger.debug "% #{diff}"
34
+ result = %x[ #{diff} ]
35
+
36
+ if result.empty? # same old
37
+ logger.info "same old #{site}"
38
+ FileUtils.rm_f("#{site_path}.new")
39
+ return
40
+ end
41
+
42
+ # changed
43
+ logger.warn "changed #{site}"
44
+ if pat = config.archive_pattern
45
+ time = File.ctime(site_path).strftime(pat)
46
+ logger.info "archived #{site_path}.#{time}"
47
+ FileUtils.mv(site_path, "#{site_path}.#{time}")
48
+ end
49
+ FileUtils.mv("#{site_path}.new", site_path)
50
+
51
+ if email = config.email # send email
52
+ logger.info "sending notification to #{email}"
53
+ begin
54
+ file = Tempfile.new('changetrack')
55
+ file.write "#{site}\n\n#{result}"
56
+ file.close
57
+ mail = "cat #{file.path} | mail -s 'Change detected on #{site}' #{email}"
58
+ logger.debug mail
59
+ system mail
60
+ ensure
61
+ file.unlink
62
+ end
63
+ end
64
+
65
+ if rss_path = config.rss_path # write rss
66
+ logger.info "update feed #{rss_path} (not realy, yet)"
67
+ end
68
+ end
69
+
70
+ def logger
71
+ return @logger if @logger
72
+ @logger = Logger.new(STDOUT).tap do |logger|
73
+ logger.level = (config.log_level || 2).to_i
74
+ end
75
+ end
76
+
77
+ end
78
+ end
@@ -0,0 +1,3 @@
1
+ module Trackchange
2
+ VERSION = "0.2.0"
3
+ end
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'trackchange/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "trackchange"
8
+ spec.version = Trackchange::VERSION
9
+ spec.authors = ["phil"]
10
+ spec.email = ["phil@branch14.org"]
11
+ spec.description = %q{Track change of websites}
12
+ spec.summary = %q{Track change of websites}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "cronedit"
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.3"
24
+ spec.add_development_dependency "rake"
25
+ end
metadata ADDED
@@ -0,0 +1,113 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: trackchange
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - phil
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2014-12-20 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: cronedit
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: bundler
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: '1.3'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: '1.3'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rake
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ description: Track change of websites
63
+ email:
64
+ - phil@branch14.org
65
+ executables:
66
+ - trackchange
67
+ extensions: []
68
+ extra_rdoc_files: []
69
+ files:
70
+ - .gitignore
71
+ - Gemfile
72
+ - LICENSE.txt
73
+ - README.md
74
+ - Rakefile
75
+ - bin/trackchange
76
+ - lib/trackchange.rb
77
+ - lib/trackchange/dependencies.rb
78
+ - lib/trackchange/exec.rb
79
+ - lib/trackchange/probe.rb
80
+ - lib/trackchange/version.rb
81
+ - trackchange.gemspec
82
+ homepage: ''
83
+ licenses:
84
+ - MIT
85
+ post_install_message:
86
+ rdoc_options: []
87
+ require_paths:
88
+ - lib
89
+ required_ruby_version: !ruby/object:Gem::Requirement
90
+ none: false
91
+ requirements:
92
+ - - ! '>='
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ segments:
96
+ - 0
97
+ hash: -700185853529785580
98
+ required_rubygems_version: !ruby/object:Gem::Requirement
99
+ none: false
100
+ requirements:
101
+ - - ! '>='
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ segments:
105
+ - 0
106
+ hash: -700185853529785580
107
+ requirements: []
108
+ rubyforge_project:
109
+ rubygems_version: 1.8.23
110
+ signing_key:
111
+ specification_version: 3
112
+ summary: Track change of websites
113
+ test_files: []