fias_parser 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ MmUxMDRhMjRmMmNhY2Y3ZDM1Y2M1MjM4NjYxYmNlOTBmNjY3NWM3Zg==
5
+ data.tar.gz: !binary |-
6
+ NDgyOTVhMTM4MzE4NjIwMjQ4NTRiNDQyNDhjMzAyNDhjYTc0YzEwMQ==
7
+ SHA512:
8
+ metadata.gz: !binary |-
9
+ MzgxODRmOTE0MzM5ZTA5NzBjMTkxYjFiNTM1Mzc2NzI3Zjg0OGNjOWM2MzA4
10
+ YWY2NWI1NTVlNmJhODIyOWJkNDQ4OTE2NmJkYzc5YzJkZWZkZDc4Nzk3NDYz
11
+ N2U2MzE5YTQxZDAyM2VhMjMwMzQ3ZmU5ZjczNTFhYTk5NWUxMzM=
12
+ data.tar.gz: !binary |-
13
+ MDhjNmM1YzdiNDA4MWMzOWI4MjNjOTM4OTkxMTI3ZDVlMTkzNWEwMTIwNjNh
14
+ MWQyYjIxZGZjMGJkZjMxODhkOGRmMWU2YTVlZTJmZjBmOWZkOGUwNGZkNjJh
15
+ ZTI3MTUzMjFlZTdjMzViYzgzMGNjNTFlNzQ2ZjRhOWMwNmZkMWU=
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/.ruby-gemset ADDED
@@ -0,0 +1 @@
1
+ fias_parser
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.0.0-p353
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in fias_parser.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Sergey Malykh
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,94 @@
1
+ # FiasParser
2
+
3
+ This gem grabs FIAS database from official site (http://fias.nalog.ru/Public/DownloadPage.aspx) and parses data to usable format.
4
+
5
+ ## Installation
6
+
7
+ 1. Install system dependecies:
8
+
9
+ $ sudo apt-get install unar wget
10
+
11
+ 2. Add this line to your application's Gemfile:
12
+
13
+ ```ruby
14
+ gem 'fias_parser'
15
+ ```
16
+
17
+ And then execute:
18
+
19
+ $ bundle
20
+
21
+ Or install it yourself as:
22
+
23
+ $ gem install fias_parser
24
+
25
+ ## Usage
26
+
27
+ Example rake-task (Mongoid):
28
+
29
+ ```ruby
30
+ # app/models/street.rb
31
+ class Street
32
+ include Mongoid::Document
33
+
34
+ field :name
35
+ field :prefix
36
+ field :guid
37
+ field :okato
38
+ end
39
+
40
+ # lib/tasks/fias.rake
41
+ namespace :fias do
42
+ desc "Stores streets to database"
43
+
44
+ task install: :environment do
45
+ parser = FiasParser::Parser.new( base_dir: Rails.root.join( 'tmp' ) )
46
+
47
+ parser.get_latest
48
+
49
+ parser.process( 'as_addrobj', batch_size: 100 ) do |batch|
50
+ batch.each do |item|
51
+ # only active items
52
+ next if item[:ACTSTATUS].to_i != 1
53
+ # streets
54
+ next if item[:AOLEVEL].to_i != 7
55
+ # just Moscow
56
+ next unless /^45/ =~ item[:OKATO]
57
+
58
+ Street.create!(
59
+ name: item[:OFFNAME],
60
+ prefix: item[:SHORTNAME],
61
+ okato: item[:OKATO],
62
+ guid: item[:AOGUID]
63
+ )
64
+ end
65
+ end
66
+ end
67
+ end
68
+ ```
69
+
70
+ Available xmls:
71
+ AS_ACTSTAT
72
+ AS_ADDROBJ
73
+ AS_CENTERST
74
+ AS_CURENTST
75
+ AS_ESTSTAT
76
+ AS_HOUSE
77
+ AS_HSTSTAT
78
+ AS_INTVSTAT
79
+ AS_LANDMARK
80
+ AS_NDOCTYPE
81
+ AS_NORMDOC
82
+ AS_OPERSTAT
83
+ AS_SOCRBASE
84
+ AS_STRSTAT
85
+
86
+ XSD schemas look on official site (http://fias.nalog.ru/Public/DownloadPage.aspx)
87
+
88
+ ## Contributing
89
+
90
+ 1. Fork it
91
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
92
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
93
+ 4. Push to the branch (`git push origin my-new-feature`)
94
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'fias_parser/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "fias_parser"
8
+ spec.version = FiasParser::VERSION
9
+ spec.authors = ["Sergey Malykh"]
10
+ spec.email = ["xronos.i.am@gmail.com"]
11
+ spec.description = %q{Parse and install FIAS database}
12
+ spec.summary = %q{Parse and install FIAS database http://fias.nalog.ru/Public/DownloadPage.aspx}
13
+ spec.homepage = "https://github.com/xronos-i-am/fias_parser"
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_runtime_dependency "mechanize"
22
+ spec.add_runtime_dependency "ox"
23
+ spec.add_runtime_dependency "cocaine"
24
+
25
+ spec.add_development_dependency "bundler", "~> 1.3"
26
+ spec.add_development_dependency "rake"
27
+ end
@@ -0,0 +1,3 @@
1
+ module FiasParser
2
+ VERSION = "1.0.0"
3
+ end
@@ -0,0 +1,164 @@
1
+ require "fias_parser/version"
2
+ require 'mechanize'
3
+ require 'ox'
4
+ require 'cocaine'
5
+
6
+ module FiasParser
7
+ class Document
8
+ def initialize( options, block )
9
+ @block = block
10
+ @batch = []
11
+ @batch_size = options[:batch_size] || 10
12
+
13
+ @root_name = nil
14
+ @item_name = nil
15
+ end
16
+
17
+ def start_element( name )
18
+ @item = nil
19
+
20
+ if @root_name.nil?
21
+ @root_name = name
22
+
23
+ return
24
+ end
25
+
26
+ @item_name = name
27
+ @item = {}
28
+ end
29
+
30
+ def attr( name, value )
31
+ @item[ name ] = value unless @item.nil?
32
+ end
33
+
34
+ def end_element( name )
35
+ self.yield_batch if name == @root_name && @batch.any?
36
+
37
+ return if @item.nil? || name != @item_name
38
+
39
+ @batch << @item
40
+
41
+ self.yield_batch if @batch.size >= @batch_size
42
+ end
43
+
44
+ def yield_batch
45
+ @block.call( @batch )
46
+ @batch = []
47
+ end
48
+ end
49
+
50
+ class Parser
51
+ attr_accessor :date
52
+
53
+ def initialize( options = {} )
54
+ @base_dir = options[:base_dir] || Dir.tmpdir
55
+ end
56
+
57
+ def get_latest
58
+ return unless self.check_executables
59
+
60
+ self.download_latest
61
+ self.unpack
62
+ end
63
+
64
+ def process( term, options = {}, &block )
65
+ file_name = Dir.entries( self.archive_path ).find { |f| f =~ /#{term}/i }
66
+
67
+ if file_name.nil?
68
+ puts "File /#{term}/ not found in '#{self.archive_path}'"
69
+
70
+ return
71
+ end
72
+
73
+ File.open( File.join( self.archive_path, file_name ) ) do |file|
74
+ ::Ox.sax_parse( Document.new( options, block ), file )
75
+ end
76
+ end
77
+
78
+ def check_executables
79
+ line = Cocaine::CommandLine.new( "wget", "-h" )
80
+
81
+ begin
82
+ line.run
83
+ rescue Cocaine::CommandNotFoundError => e
84
+ puts 'wget is not installed. Run "sudo apt-get install wget"'
85
+
86
+ return false
87
+ end
88
+
89
+ line = Cocaine::CommandLine.new( "unar", "-h" )
90
+
91
+ begin
92
+ line.run
93
+ rescue Cocaine::CommandNotFoundError => e
94
+ puts 'unar is not installed. Run "sudo apt-get install unar"'
95
+
96
+ return false
97
+ end
98
+
99
+ true
100
+ end
101
+
102
+ def download_latest
103
+ agent = Mechanize.new
104
+ agent.user_agent = 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:10.0.1) Gecko/20100101 Firefox/10.0.1'
105
+
106
+ url = 'http://fias.nalog.ru/Public/DownloadPage.aspx'
107
+
108
+ doc = agent.get( url ).root
109
+
110
+ post_data = {}
111
+
112
+ doc.css( 'form input[type="hidden"]' ).each do |input|
113
+ post_data[ input['name'] ] = input['value']
114
+ end
115
+
116
+ @date = doc.css( '#ctl00_contentPlaceHolder_downloadRadGrid_ctl00__0 tr td:first-child' ).text.gsub( /[^\d]/, '' )
117
+
118
+ post_data['__EVENTTARGET'] = 'ctl00$contentPlaceHolder$downloadRadGrid$ctl00$ctl04$fullSZLinkButton'
119
+
120
+ if File.exists?( self.archive_file_path )
121
+ puts "File '#{self.archive_file_path}' already exists."
122
+
123
+ return
124
+ end
125
+
126
+ line = Cocaine::CommandLine.new( "wget", "--output-document=:out --post-data=:post_data :url" )
127
+
128
+ line.run( {
129
+ out: self.archive_file_path,
130
+ post_data: Mechanize::Util.build_query_string( post_data ),
131
+ url: url,
132
+ } )
133
+ end
134
+
135
+ def unpack
136
+ dir = File.join( @base_dir )
137
+
138
+ FileUtils.mkdir_p ( dir ) unless File.exists?( dir )
139
+
140
+ line = Cocaine::CommandLine.new( "unar", "-o :dir :archive" )
141
+
142
+ line.run( {
143
+ dir: dir,
144
+ archive: self.archive_file_path,
145
+ } )
146
+ end
147
+
148
+ def archive_path
149
+ @archive_path ||= get_archive_path
150
+ end
151
+
152
+ def get_archive_path
153
+ File.join( @base_dir, @date )
154
+ end
155
+
156
+ def archive_file_path
157
+ @archive_file_path ||= get_archive_file_path
158
+ end
159
+
160
+ def get_archive_file_path
161
+ File.join( @base_dir, "#{@date}.rar" )
162
+ end
163
+ end
164
+ end
metadata ADDED
@@ -0,0 +1,124 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fias_parser
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Sergey Malykh
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-11-26 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: mechanize
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ! '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ! '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: ox
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ! '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: cocaine
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ! '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: '1.3'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: '1.3'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ! '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ! '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: Parse and install FIAS database
84
+ email:
85
+ - xronos.i.am@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - .gitignore
91
+ - .ruby-gemset
92
+ - .ruby-version
93
+ - Gemfile
94
+ - LICENSE.txt
95
+ - README.md
96
+ - Rakefile
97
+ - fias_parser.gemspec
98
+ - lib/fias_parser.rb
99
+ - lib/fias_parser/version.rb
100
+ homepage: https://github.com/xronos-i-am/fias_parser
101
+ licenses:
102
+ - MIT
103
+ metadata: {}
104
+ post_install_message:
105
+ rdoc_options: []
106
+ require_paths:
107
+ - lib
108
+ required_ruby_version: !ruby/object:Gem::Requirement
109
+ requirements:
110
+ - - ! '>='
111
+ - !ruby/object:Gem::Version
112
+ version: '0'
113
+ required_rubygems_version: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ! '>='
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ requirements: []
119
+ rubyforge_project:
120
+ rubygems_version: 2.1.11
121
+ signing_key:
122
+ specification_version: 4
123
+ summary: Parse and install FIAS database http://fias.nalog.ru/Public/DownloadPage.aspx
124
+ test_files: []