rightmove_wrangler 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -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 rightmove_wrangler.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Robert May
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.
@@ -0,0 +1,29 @@
1
+ # RightmoveWrangler
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'rightmove_wrangler'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install rightmove_wrangler
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ $:.unshift File.dirname(__FILE__) + '/../lib'
4
+ require 'rightmove_wrangler'
5
+
6
+ processor = RightmoveWrangler::Processor.new(ARGV)
7
+ processor.run
8
+
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+ require 'rubygems'
3
+ require 'daemons'
4
+
5
+ Daemons.run "rightmove_wrangler"
6
+
@@ -0,0 +1,201 @@
1
+ require "rubygems"
2
+ require "rightmove_wrangler/version"
3
+ require "fileutils"
4
+ require "rightmove"
5
+ require "optparse"
6
+ require "faraday"
7
+ require "addressable/uri"
8
+
9
+ module RightmoveWrangler
10
+ class Processor
11
+ attr_accessor :directory_list
12
+ IMAGE_REGEX = /\.(jpg|jpeg|png|gif)/i
13
+
14
+ def initialize(args)
15
+ @args = args
16
+ @options = {}
17
+ end
18
+
19
+ def run
20
+ @opts = OptionParser.new(&method(:set_opts))
21
+ @opts.parse!(@args)
22
+ $stdout.puts @options.inspect
23
+ if @options.empty? || @options[:path].nil? || !File.directory?(@options[:path])
24
+ $stderr.puts 'Invalid path specified'
25
+ exit 1
26
+ else
27
+ loop do
28
+ begin
29
+ process!
30
+ rescue Exception => ex
31
+ raise ex if @options[:trace] || SystemExit === ex
32
+ $stderr.print "#{ex.class}: " if ex.class != RuntimeError
33
+ $stderr.puts ex.message
34
+ end
35
+ sleep 5
36
+ end
37
+ end
38
+ end
39
+
40
+ protected
41
+ def set_opts(opts)
42
+ opts.banner = "rightmove_wrangler is used to monitor a directory, parse RM format zip files, and submit them via post to an API"
43
+
44
+ opts.on('-p', '--path PATH', 'Path to watch') do |path|
45
+ @options[:path] = path
46
+ end
47
+
48
+ opts.on('-u', '--url URL', 'URL to post the data to') do |url|
49
+ @options[:url] = url
50
+ end
51
+
52
+ opts.on_tail('-h', '--help', 'Show this message') do
53
+ puts opts
54
+ exit
55
+ end
56
+
57
+ opts.on_tail('-v', '--version', 'Print version') do
58
+ puts "rightmove_wrangler #{RightmoveWrangler::VERSION}"
59
+ exit
60
+ end
61
+ end
62
+
63
+ private
64
+ def process!
65
+ files = Dir.entries(@options[:path])
66
+ if directory_list == files
67
+ $stdout.puts 'Nothing changed since last poll'
68
+ return
69
+ else
70
+ self.directory_list = files
71
+ end
72
+
73
+ begin
74
+ threads = []
75
+ Dir.foreach(@options[:path]) do |file|
76
+ $stdout.puts "Checking #{file}"
77
+ if match = file =~ /\.(zip|blm)/i
78
+ threads << Thread.new do
79
+ $stdout.puts "Working on #{file}"
80
+ send("work_#{match[1]}_file".to_sym)
81
+ end
82
+ end
83
+ end
84
+ threads.each do |t|
85
+ t.join
86
+ $stdout.puts t[:output]
87
+ end
88
+ rescue Exception => ex
89
+ $stderr.puts ex.inspect
90
+ $stderr.puts ex.backtrace
91
+ end
92
+ end
93
+
94
+ def work_blm_file(file)
95
+ blm = BLM.new( File.open(file, "r").read )
96
+ rows = blm.data.collect do |row|
97
+ row_hash = {}
98
+ row.attributes.each do |key, value|
99
+ row_hash[key] = if value =~ IMAGE_REGEX
100
+ $stdout.puts "Instantiated file #{value}"
101
+ instantiate_from_dir(value, @options[:path])
102
+ else
103
+ value
104
+ end
105
+ end
106
+ end
107
+ end
108
+
109
+ def work_zip_file(file)
110
+ zip_file = Zip::ZipFile.open("#{@options[:path]}/#{file}")
111
+ archive = Rightmove::Archive.new(zip_file)
112
+
113
+ rows = archive.document.data.collect do |row|
114
+ row_hash = {}
115
+ # This is a bit odd, but essentially it actually turns media rows into files
116
+ row.attributes.each do |key, value|
117
+ row_hash[key] = if value =~ IMAGE_REGEX
118
+ $stdout.puts "Instantiated file #{value}"
119
+ instantiate_from_zip_file(value, zip_file)
120
+ else
121
+ value
122
+ end
123
+ end
124
+ row_hash
125
+ end
126
+
127
+ payload = {
128
+ row_set: {
129
+ tag: archive.branch_id,
130
+ timestamp: archive.timestamp.to_i,
131
+ rows: rows
132
+ }
133
+ }
134
+ post!(payload)
135
+ end
136
+
137
+ def instantiate_from_dir(file_name, dir)
138
+ path = File.join(file_name, dir)
139
+ if File.exists?(path)
140
+ $stdout.puts "Found file: #{path}"
141
+ instantiate_file File.open(path)
142
+ else
143
+ $stdout.puts "Couldn't find file: #{path}"
144
+ file_name
145
+ end
146
+ end
147
+
148
+ def instantiate_from_zip_file(file_name, zip_file)
149
+ matching_files = zip_file.entries.select {|v| v.to_s =~ /#{file_name}/ }
150
+ if matching_files.empty?
151
+ $stdout.puts "Couldn't find file: #{file_name}"
152
+ file_name
153
+ else
154
+ $stdout.puts "Found file: #{file_name}"
155
+ file = StringIO.new( zip_file.read(matching_files.first) )
156
+ instantiate_file(file, file_name)
157
+ end
158
+ end
159
+
160
+ def instantiate_file(file, file_name = nil, content_type = nil)
161
+ if !file.respond_to?(:original_filename)
162
+ file.class.class_eval { attr_accessor :original_filename }
163
+ file.original_filename = file_name
164
+ end
165
+
166
+ if !file.respond_to?(:content_type)
167
+ content_type ||= "image/jpg"
168
+ file.class.class_eval { attr_accessor :content_type }
169
+ file.content_type = content_type
170
+ end
171
+
172
+ Faraday::UploadIO.new(file, file_name, file.content_type)
173
+ end
174
+
175
+ def post!(payload)
176
+ if @options[:url].nil?
177
+ Thread.current[:output] = "Missing a URL to post the data to"
178
+ end
179
+
180
+ uri = Addressable::URI.parse(@options[:url])
181
+ conn = Faraday.new(url: uri.origin) do |builder|
182
+ builder.request :multipart
183
+ builder.request :url_encoded
184
+ builder.response :logger
185
+ builder.adapter :net_http
186
+ end
187
+
188
+ params = uri.query_values.merge(payload)
189
+ response = conn.post uri.path, params
190
+
191
+ if response.status == 200
192
+ Thread.current[:output] = "Server returned a successful response"
193
+ return true
194
+ else
195
+ Thread.current[:output] = "Non 200 response returned from API: #{response.inspect}"
196
+ return false
197
+ end
198
+ end
199
+ end
200
+
201
+ end
@@ -0,0 +1,3 @@
1
+ module RightmoveWrangler
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,24 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/rightmove_wrangler/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Robert May"]
6
+ gem.email = ["robotmay@gmail.com"]
7
+ gem.description = %q{rightmove_wrangler is a command line utility for processing a directory of Rightmove .zip files and submitting them to an API.}
8
+ gem.summary = %q{A command-line utility for processing a directory of Rightmove .zip files}
9
+ gem.homepage = "http://github.com/corewebdesign/rightmove_wrangler"
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "rightmove_wrangler"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = RightmoveWrangler::VERSION
17
+
18
+ gem.add_runtime_dependency 'rightmove'
19
+ gem.add_runtime_dependency 'blm'
20
+ gem.add_runtime_dependency 'faraday'
21
+ gem.add_runtime_dependency 'multipart-post'
22
+ gem.add_runtime_dependency 'addressable'
23
+ gem.add_runtime_dependency 'daemons'
24
+ end
metadata ADDED
@@ -0,0 +1,154 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rightmove_wrangler
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Robert May
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-08-21 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rightmove
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: blm
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: faraday
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :runtime
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
+ - !ruby/object:Gem::Dependency
63
+ name: multipart-post
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :runtime
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ - !ruby/object:Gem::Dependency
79
+ name: addressable
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :runtime
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ - !ruby/object:Gem::Dependency
95
+ name: daemons
96
+ requirement: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ! '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ type: :runtime
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ description: rightmove_wrangler is a command line utility for processing a directory
111
+ of Rightmove .zip files and submitting them to an API.
112
+ email:
113
+ - robotmay@gmail.com
114
+ executables:
115
+ - rightmove_wrangler
116
+ - rightmove_wranglerd
117
+ extensions: []
118
+ extra_rdoc_files: []
119
+ files:
120
+ - .gitignore
121
+ - Gemfile
122
+ - LICENSE
123
+ - README.md
124
+ - Rakefile
125
+ - bin/rightmove_wrangler
126
+ - bin/rightmove_wranglerd
127
+ - lib/rightmove_wrangler.rb
128
+ - lib/rightmove_wrangler/version.rb
129
+ - rightmove_wrangler.gemspec
130
+ homepage: http://github.com/corewebdesign/rightmove_wrangler
131
+ licenses: []
132
+ post_install_message:
133
+ rdoc_options: []
134
+ require_paths:
135
+ - lib
136
+ required_ruby_version: !ruby/object:Gem::Requirement
137
+ none: false
138
+ requirements:
139
+ - - ! '>='
140
+ - !ruby/object:Gem::Version
141
+ version: '0'
142
+ required_rubygems_version: !ruby/object:Gem::Requirement
143
+ none: false
144
+ requirements:
145
+ - - ! '>='
146
+ - !ruby/object:Gem::Version
147
+ version: '0'
148
+ requirements: []
149
+ rubyforge_project:
150
+ rubygems_version: 1.8.24
151
+ signing_key:
152
+ specification_version: 3
153
+ summary: A command-line utility for processing a directory of Rightmove .zip files
154
+ test_files: []