chinacity 0.0.1
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 +7 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +45 -0
- data/Rakefile +1 -0
- data/areas.csv +3516 -0
- data/areas.json +14068 -0
- data/bin/chinacity +6 -0
- data/chinacity.gemspec +27 -0
- data/lib/chinacity/cli.rb +107 -0
- data/lib/chinacity/version.rb +3 -0
- data/lib/chinacity.rb +28 -0
- metadata +140 -0
data/bin/chinacity
ADDED
data/chinacity.gemspec
ADDED
|
@@ -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 'chinacity/version'
|
|
5
|
+
|
|
6
|
+
Gem::Specification.new do |spec|
|
|
7
|
+
spec.name = "chinacity"
|
|
8
|
+
spec.version = Chinacity::VERSION
|
|
9
|
+
spec.authors = ["Ryan Wang"]
|
|
10
|
+
spec.email = ["wongyouth@gmail.com"]
|
|
11
|
+
spec.summary = %q{Get China cities data from China Official site}
|
|
12
|
+
spec.description = %q{Get China cities data from China official site}
|
|
13
|
+
spec.homepage = "http://github.com/wongyouth/chinacity"
|
|
14
|
+
spec.license = "MIT"
|
|
15
|
+
|
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
|
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 "chinese_pinyin"
|
|
22
|
+
spec.add_dependency "json"
|
|
23
|
+
spec.add_dependency "nokogiri"
|
|
24
|
+
spec.add_dependency "httparty"
|
|
25
|
+
spec.add_development_dependency "bundler", "~> 1.5"
|
|
26
|
+
spec.add_development_dependency "rake"
|
|
27
|
+
end
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
require 'nokogiri'
|
|
3
|
+
require 'chinese_pinyin'
|
|
4
|
+
require 'json'
|
|
5
|
+
require 'httparty'
|
|
6
|
+
|
|
7
|
+
module Chinacity
|
|
8
|
+
HOST = 'http://www.stats.gov.cn'
|
|
9
|
+
LIST_PATH = '/tjsj/tjbz/xzqhdm/'
|
|
10
|
+
LIST_URL = HOST + LIST_PATH
|
|
11
|
+
|
|
12
|
+
class Cli
|
|
13
|
+
def initialize options
|
|
14
|
+
@short = options[:short]
|
|
15
|
+
@foramt = options[:format]
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def start
|
|
19
|
+
doc = Nokogiri::HTML(get_data)
|
|
20
|
+
get_cities(doc)
|
|
21
|
+
|
|
22
|
+
add_short_name if @short
|
|
23
|
+
|
|
24
|
+
output (@foramt == :json ? to_json : to_csv)
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
private
|
|
28
|
+
def get_data
|
|
29
|
+
# puts LIST_URL
|
|
30
|
+
|
|
31
|
+
# get newest city url
|
|
32
|
+
data = HTTParty.get(LIST_URL).body
|
|
33
|
+
doc = Nokogiri::HTML data
|
|
34
|
+
url = LIST_URL + doc.css('.center_list a')[0][:href]
|
|
35
|
+
# puts url
|
|
36
|
+
|
|
37
|
+
# get newest city data
|
|
38
|
+
HTTParty.get(url).body
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def get_cities(doc)
|
|
42
|
+
@cities = doc.css('p.MsoNormal').map do |data|
|
|
43
|
+
id, text = data.text.gsub(/[[:space:]]+/, ' ').split
|
|
44
|
+
|
|
45
|
+
{id: id, text: text} if id
|
|
46
|
+
end.compact
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def add_short_name
|
|
50
|
+
@cities.each do |city|
|
|
51
|
+
# puts city
|
|
52
|
+
city[:short] = Pinyin.t(city[:text]).split.map{|w|w[0]}.join.upcase
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
self
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
def nested_cities
|
|
59
|
+
pretty = {}
|
|
60
|
+
|
|
61
|
+
@cities.each do |city|
|
|
62
|
+
if city[:id].end_with? '0000'
|
|
63
|
+
pretty[:province] ||= []
|
|
64
|
+
pretty[:province] << city
|
|
65
|
+
elsif city[:id].end_with? '00'
|
|
66
|
+
pretty[:city] ||= []
|
|
67
|
+
pretty[:city] << city
|
|
68
|
+
else
|
|
69
|
+
pretty[:district] ||= []
|
|
70
|
+
pretty[:district] << city
|
|
71
|
+
end
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
pretty
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
def to_json
|
|
78
|
+
JSON.pretty_generate(nested_cities)
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
def to_csv
|
|
82
|
+
list = @short ? [ "id, 名称, 缩写, 层级" ] : ["id, 名称, 层级"]
|
|
83
|
+
|
|
84
|
+
list += @cities.map do |city|
|
|
85
|
+
level = if city[:id].end_with? '0000'
|
|
86
|
+
1
|
|
87
|
+
elsif city[:id].end_with? '00'
|
|
88
|
+
2
|
|
89
|
+
else
|
|
90
|
+
3
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
if @short
|
|
94
|
+
"#{city[:id]}, #{city[:text]}, #{city[:short]}, #{level}"
|
|
95
|
+
else
|
|
96
|
+
"#{city[:id]}, #{city[:text]}, #{level}"
|
|
97
|
+
end
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
list.join("\n")
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
def output data
|
|
104
|
+
STDOUT.write data.to_s
|
|
105
|
+
end
|
|
106
|
+
end
|
|
107
|
+
end
|
data/lib/chinacity.rb
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
require 'optparse'
|
|
2
|
+
require "chinacity/version"
|
|
3
|
+
require "chinacity/cli"
|
|
4
|
+
|
|
5
|
+
options = {
|
|
6
|
+
short: false,
|
|
7
|
+
format: :json
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
parser = OptionParser.new do |opts|
|
|
11
|
+
opts.banner = "Usage: chinacity [options]"
|
|
12
|
+
opts.separator ""
|
|
13
|
+
opts.separator "options:"
|
|
14
|
+
|
|
15
|
+
opts.on('-s', '--short', 'Short name, First charactor of every word in pinyin') do
|
|
16
|
+
options[:short] = true
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
opts.on('-c', '--csv', 'Output CSV format') do
|
|
20
|
+
options[:format] = :csv
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
parser.parse!
|
|
25
|
+
|
|
26
|
+
cli = Chinacity::Cli.new options
|
|
27
|
+
cli.start
|
|
28
|
+
|
metadata
ADDED
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: chinacity
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.0.1
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Ryan Wang
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2014-06-30 00:00:00.000000000 Z
|
|
12
|
+
dependencies:
|
|
13
|
+
- !ruby/object:Gem::Dependency
|
|
14
|
+
name: chinese_pinyin
|
|
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: json
|
|
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: nokogiri
|
|
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: httparty
|
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
|
58
|
+
requirements:
|
|
59
|
+
- - ">="
|
|
60
|
+
- !ruby/object:Gem::Version
|
|
61
|
+
version: '0'
|
|
62
|
+
type: :runtime
|
|
63
|
+
prerelease: false
|
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
65
|
+
requirements:
|
|
66
|
+
- - ">="
|
|
67
|
+
- !ruby/object:Gem::Version
|
|
68
|
+
version: '0'
|
|
69
|
+
- !ruby/object:Gem::Dependency
|
|
70
|
+
name: bundler
|
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
|
72
|
+
requirements:
|
|
73
|
+
- - "~>"
|
|
74
|
+
- !ruby/object:Gem::Version
|
|
75
|
+
version: '1.5'
|
|
76
|
+
type: :development
|
|
77
|
+
prerelease: false
|
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
79
|
+
requirements:
|
|
80
|
+
- - "~>"
|
|
81
|
+
- !ruby/object:Gem::Version
|
|
82
|
+
version: '1.5'
|
|
83
|
+
- !ruby/object:Gem::Dependency
|
|
84
|
+
name: rake
|
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
|
86
|
+
requirements:
|
|
87
|
+
- - ">="
|
|
88
|
+
- !ruby/object:Gem::Version
|
|
89
|
+
version: '0'
|
|
90
|
+
type: :development
|
|
91
|
+
prerelease: false
|
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
93
|
+
requirements:
|
|
94
|
+
- - ">="
|
|
95
|
+
- !ruby/object:Gem::Version
|
|
96
|
+
version: '0'
|
|
97
|
+
description: Get China cities data from China official site
|
|
98
|
+
email:
|
|
99
|
+
- wongyouth@gmail.com
|
|
100
|
+
executables:
|
|
101
|
+
- chinacity
|
|
102
|
+
extensions: []
|
|
103
|
+
extra_rdoc_files: []
|
|
104
|
+
files:
|
|
105
|
+
- Gemfile
|
|
106
|
+
- LICENSE.txt
|
|
107
|
+
- README.md
|
|
108
|
+
- Rakefile
|
|
109
|
+
- areas.csv
|
|
110
|
+
- areas.json
|
|
111
|
+
- bin/chinacity
|
|
112
|
+
- chinacity.gemspec
|
|
113
|
+
- lib/chinacity.rb
|
|
114
|
+
- lib/chinacity/cli.rb
|
|
115
|
+
- lib/chinacity/version.rb
|
|
116
|
+
homepage: http://github.com/wongyouth/chinacity
|
|
117
|
+
licenses:
|
|
118
|
+
- MIT
|
|
119
|
+
metadata: {}
|
|
120
|
+
post_install_message:
|
|
121
|
+
rdoc_options: []
|
|
122
|
+
require_paths:
|
|
123
|
+
- lib
|
|
124
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
125
|
+
requirements:
|
|
126
|
+
- - ">="
|
|
127
|
+
- !ruby/object:Gem::Version
|
|
128
|
+
version: '0'
|
|
129
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
130
|
+
requirements:
|
|
131
|
+
- - ">="
|
|
132
|
+
- !ruby/object:Gem::Version
|
|
133
|
+
version: '0'
|
|
134
|
+
requirements: []
|
|
135
|
+
rubyforge_project:
|
|
136
|
+
rubygems_version: 2.2.0
|
|
137
|
+
signing_key:
|
|
138
|
+
specification_version: 4
|
|
139
|
+
summary: Get China cities data from China Official site
|
|
140
|
+
test_files: []
|