zipcode_zh_cn 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +10 -0
- data/.rspec +2 -0
- data/.travis.yml +4 -0
- data/Gemfile +7 -0
- data/LICENSE.txt +21 -0
- data/README.md +25 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/zipcode_zh_cn/version.rb +3 -0
- data/lib/zipcode_zh_cn.rb +58 -0
- data/zipcode_zh_cn.gemspec +27 -0
- metadata +127 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: b27c2dc5d4e769bddafc8383d21109074bd3c79e
|
4
|
+
data.tar.gz: b0da1cd4e31a9e16911a897dc6ad61251f6e4666
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 05008eaa280cf12726693007602f80443d9d1ec20cb2da4c083ef45ec74829e606b64fa9c501a44af12c728bbd56e0614e65fef5495e0575d9454097e2c61b8d
|
7
|
+
data.tar.gz: 4a639ac0ef9246bb1e7c2e07447fdb976d30358984e85cfca46a5d41cde1a4d21ec29fc265cbdb78ab39c257909af6c9caf0ff74878722fa5c87bb3bae2b64cb
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 RubyCat
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# ZipcodeZhCn [![Gem Version](https://badge.fury.io/rb/zipcode_zh_cn.svg)](http://badge.fury.io/rb/zipcode_zh_cn)
|
2
|
+
|
3
|
+
通过地址字符串在线获取中国大陆邮编。 数据来源 [http://opendata.baidu.com/post/s](http://opendata.baidu.com/post/s)
|
4
|
+
|
5
|
+
## 安装
|
6
|
+
$ gem install zipcode_zh_cn
|
7
|
+
|
8
|
+
## 使用
|
9
|
+
|
10
|
+
```ruby
|
11
|
+
ZipcodeZhCn.get_zip_code("your address")
|
12
|
+
```
|
13
|
+
或者命令行:
|
14
|
+
|
15
|
+
$ ZipcodeZhCN 昆山市
|
16
|
+
|
17
|
+
|
18
|
+
## Thanks
|
19
|
+
|
20
|
+
- Baidu. [http://opendata.baidu.com/post/s](http://opendata.baidu.com/post/s)
|
21
|
+
|
22
|
+
## License
|
23
|
+
|
24
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
25
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "zipcode_zh_cn"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
require "zipcode_zh_cn/version"
|
2
|
+
require "http"
|
3
|
+
require "nokogiri"
|
4
|
+
|
5
|
+
module ZipcodeZhCn
|
6
|
+
ApiHost = "http://opendata.baidu.com/"
|
7
|
+
ApiAction = "/post/s"
|
8
|
+
ApiUrl = ApiHost + ApiAction
|
9
|
+
DefaultParams = {:rn => 1, :ie => "utf-8"}.freeze
|
10
|
+
DefaultZipCode = ""
|
11
|
+
|
12
|
+
## get zipcode by address string
|
13
|
+
def self.get_zipcode(str, http = HTTP)
|
14
|
+
#special handle
|
15
|
+
addr = str
|
16
|
+
addr.gsub!('省直辖行政单位','')
|
17
|
+
addr.gsub!('省直辖县级行政单位','')
|
18
|
+
addr.gsub!('自治区直辖县级行政区划','')
|
19
|
+
addr.gsub!('省直辖县级行政区划','')
|
20
|
+
addr.gsub!("广东省广州市越秀区", "广州市越秀区")
|
21
|
+
addr.gsub!("广西壮族自治区南宁市西乡塘区", "南宁市西乡塘区")
|
22
|
+
|
23
|
+
params = {:wd => addr}.merge(DefaultParams)
|
24
|
+
zipcode = DefaultZipCode
|
25
|
+
try_count = 0
|
26
|
+
begin
|
27
|
+
try_count += 1
|
28
|
+
res = http.get(ApiUrl, :params => params).flush
|
29
|
+
|
30
|
+
if res.code == 200
|
31
|
+
doc = Nokogiri::HTML.parse(res.body, nil, "gbk")
|
32
|
+
first_e = doc.xpath("//section//article//ul//li//a").first
|
33
|
+
zipcode = first_e.text.split(" ").last if first_e
|
34
|
+
if zipcode == DefaultZipCode
|
35
|
+
element = doc.xpath("//section//article//table//tr//td").first
|
36
|
+
zipcode = element.text if element
|
37
|
+
end
|
38
|
+
|
39
|
+
if zipcode.length != 6
|
40
|
+
puts "zipcode is not right "
|
41
|
+
end
|
42
|
+
end
|
43
|
+
rescue Exception => e
|
44
|
+
if try_count < 6
|
45
|
+
http.close if http
|
46
|
+
http = HTTP.persistent ApiHost
|
47
|
+
puts "try_count = #{try_count} :sleep #{try_count} seconds : Exception ====> #{e}"
|
48
|
+
sleep(try_count)
|
49
|
+
retry
|
50
|
+
else
|
51
|
+
puts "try out=>#{addr}"
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
zipcode
|
56
|
+
end
|
57
|
+
|
58
|
+
end
|
@@ -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 'zipcode_zh_cn/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "zipcode_zh_cn"
|
8
|
+
spec.version = ZipcodeZhCn::VERSION
|
9
|
+
spec.authors = ["rubycat"]
|
10
|
+
spec.email = ["chenxueping1819@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{Get zipcode for Mainland China by searching with address string}
|
13
|
+
spec.description = %q{Zipcode data is fetched from Baidu }
|
14
|
+
spec.homepage = "https://github.com/a598799539/zipcode_zh_cn"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
18
|
+
spec.bindir = "exe"
|
19
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
20
|
+
spec.require_paths = ["lib"]
|
21
|
+
|
22
|
+
spec.add_development_dependency "bundler", "~> 1.11"
|
23
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
24
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
25
|
+
spec.add_development_dependency "http", "~> 2.0"
|
26
|
+
spec.add_development_dependency "nokogiri", "~> 1.6"
|
27
|
+
end
|
metadata
ADDED
@@ -0,0 +1,127 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: zipcode_zh_cn
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- rubycat
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-07-22 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.11'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.11'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ~>
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ~>
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ~>
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ~>
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: http
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ~>
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '2.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ~>
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '2.0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: nokogiri
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ~>
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '1.6'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ~>
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '1.6'
|
83
|
+
description: 'Zipcode data is fetched from Baidu '
|
84
|
+
email:
|
85
|
+
- chenxueping1819@gmail.com
|
86
|
+
executables: []
|
87
|
+
extensions: []
|
88
|
+
extra_rdoc_files: []
|
89
|
+
files:
|
90
|
+
- .gitignore
|
91
|
+
- .rspec
|
92
|
+
- .travis.yml
|
93
|
+
- Gemfile
|
94
|
+
- LICENSE.txt
|
95
|
+
- README.md
|
96
|
+
- Rakefile
|
97
|
+
- bin/console
|
98
|
+
- bin/setup
|
99
|
+
- lib/zipcode_zh_cn.rb
|
100
|
+
- lib/zipcode_zh_cn/version.rb
|
101
|
+
- zipcode_zh_cn.gemspec
|
102
|
+
homepage: https://github.com/a598799539/zipcode_zh_cn
|
103
|
+
licenses:
|
104
|
+
- MIT
|
105
|
+
metadata: {}
|
106
|
+
post_install_message:
|
107
|
+
rdoc_options: []
|
108
|
+
require_paths:
|
109
|
+
- lib
|
110
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
111
|
+
requirements:
|
112
|
+
- - '>='
|
113
|
+
- !ruby/object:Gem::Version
|
114
|
+
version: '0'
|
115
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
116
|
+
requirements:
|
117
|
+
- - '>='
|
118
|
+
- !ruby/object:Gem::Version
|
119
|
+
version: '0'
|
120
|
+
requirements: []
|
121
|
+
rubyforge_project:
|
122
|
+
rubygems_version: 2.4.8
|
123
|
+
signing_key:
|
124
|
+
specification_version: 4
|
125
|
+
summary: Get zipcode for Mainland China by searching with address string
|
126
|
+
test_files: []
|
127
|
+
has_rdoc:
|