ym4r-anonymizer 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1 @@
1
+ pkg/
data/CHANGELOG ADDED
@@ -0,0 +1,2 @@
1
+ v0.0.1. first version
2
+
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source :rubygems
2
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,17 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ ym4r-anonymizer (0.0.1)
5
+ ym4r
6
+
7
+ GEM
8
+ remote: http://rubygems.org/
9
+ specs:
10
+ ym4r (0.6.1)
11
+
12
+ PLATFORMS
13
+ ruby
14
+
15
+ DEPENDENCIES
16
+ bundler (>= 1.0.0)
17
+ ym4r-anonymizer!
data/README ADDED
@@ -0,0 +1,36 @@
1
+ Ym4r is a useful gem that help us get geocode data with less code, but this is a
2
+ problem when we frequently fetch the geocode data from Google Maps API, it will
3
+ ban us then ask typing the captcha. In order to git rid of the API request
4
+ limitation, we can use an anonymize proxy(Tor is great), or generate a lot of API
5
+ keys for geocoding.
6
+
7
+ Here this gem Ym4r-Anonymizer will help you fix the API request problem:
8
+
9
+ 1. create ym4r_anonymizer.yml
10
+
11
+ :proxy:
12
+ :host: 127.0.0.1
13
+ :port: 8123
14
+ # :username: user
15
+ # :password: pwd
16
+ :google_api_keys:
17
+ - ABQIAAAAzMUFFnT9uH0xq39J0Y4kbhTJQa0g3IQ9GZqIMmInSLzwtGDKaBR6j135zrztfTGVOm2QlWnkaidDIQ
18
+ - ABQIAAAADJ6dxjPhPP3j4JMcarDN1RS3zpQkq3Dy2kJjwfk7zLSQDM4mqxRSmAnhPMLC-mwJSRi2Kd5iUlHW6w
19
+ :yahoo_app_ids:
20
+ - YellowMaps4R
21
+
22
+ 2. code in your script
23
+
24
+ require 'rubygems'
25
+ require 'ym4r/anonymizer'
26
+
27
+ Ym4r::Anonymizer.config = YAML.load_file(File.expand_path('../ym4r_anonymizer.yml', __FILE__))
28
+ Ym4r::Anonymizer.verbos = true
29
+ Ym4r::GoogleMaps::Geocoding.get('Chengdu, Sichuan, China')
30
+ Ym4r::GoogleMaps::Geocoding.get('Beijing, China')
31
+ Ym4r::GoogleMaps::Geocoding.get('Chongqing, China')
32
+ Ym4r::Anonymizer.config[:proxy] = nil
33
+ Ym4r::GoogleMaps::Geocoding.get('Chengdu, Sichuan, China')
34
+ Ym4r::GoogleMaps::Geocoding.get('Beijing, China')
35
+ Ym4r::GoogleMaps::Geocoding.get('Chongqing, China')
36
+
data/Rakefile ADDED
@@ -0,0 +1,3 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ Bundler::GemHelper.install_tasks
@@ -0,0 +1,18 @@
1
+ require 'rubygems'
2
+ begin
3
+ require 'ym4r/anonymizer'
4
+ rescue
5
+ # debug local script mode
6
+ $:.unshift File.join(File.dirname(__FILE__), '../lib')
7
+ require 'ym4r/anonymizer'
8
+ end
9
+
10
+ Ym4r::Anonymizer.config = YAML.load_file(File.expand_path('../ym4r_anonymizer.yml', __FILE__))
11
+ Ym4r::Anonymizer.verbos = true
12
+ Ym4r::GoogleMaps::Geocoding.get('Chengdu, Sichuan, China')
13
+ Ym4r::GoogleMaps::Geocoding.get('Beijing, China')
14
+ Ym4r::GoogleMaps::Geocoding.get('Chongqing, China')
15
+ Ym4r::Anonymizer.config[:proxy] = nil
16
+ Ym4r::GoogleMaps::Geocoding.get('Chengdu, Sichuan, China')
17
+ Ym4r::GoogleMaps::Geocoding.get('Beijing, China')
18
+ Ym4r::GoogleMaps::Geocoding.get('Chongqing, China')
@@ -0,0 +1,10 @@
1
+ :proxy:
2
+ :host: 127.0.0.1
3
+ :port: 8123
4
+ # :username: user
5
+ # :password: pwd
6
+ :google_api_keys:
7
+ - ABQIAAAAzMUFFnT9uH0xq39J0Y4kbhTJQa0g3IQ9GZqIMmInSLzwtGDKaBR6j135zrztfTGVOm2QlWnkaidDIQ
8
+ - ABQIAAAADJ6dxjPhPP3j4JMcarDN1RS3zpQkq3Dy2kJjwfk7zLSQDM4mqxRSmAnhPMLC-mwJSRi2Kd5iUlHW6w
9
+ :yahoo_app_ids:
10
+ - YellowMaps4R
@@ -0,0 +1,121 @@
1
+
2
+ require 'ym4r'
3
+
4
+ module Ym4r
5
+ module Anonymizer
6
+
7
+ Version = "0.0.1"
8
+
9
+ @config = {}
10
+
11
+ #Ym4r::Anonymizer.config = {
12
+ # :proxy => {
13
+ # :host => '127.0.0.1',
14
+ # :port => 8123,
15
+ # :username => 'user',
16
+ # :password => 'pwd',
17
+ # },
18
+ # :google_api_keys => [
19
+ # 'ABQIAAAAzMUFFnT9uH0xq39J0Y4kbhTJQa0g3IQ9GZqIMmInSLzwtGDKaBR6j135zrztfTGVOm2QlWnkaidDIQ',
20
+ # 'ABQIAAAADJ6dxjPhPP3j4JMcarDN1RS3zpQkq3Dy2kJjwfk7zLSQDM4mqxRSmAnhPMLC-mwJSRi2Kd5iUlHW6w',
21
+ # ],
22
+ # :yahoo_app_ids => [
23
+ # 'YellowMaps4R',
24
+ # ]
25
+ #}
26
+ def self.config=(v)
27
+ @config = v
28
+ end
29
+
30
+ def self.config
31
+ @config
32
+ end
33
+
34
+ def self.verbos=(v)
35
+ @verbos = v
36
+ end
37
+
38
+ def self.verbos?
39
+ !!@verbos
40
+ end
41
+
42
+ def self.included(klass)
43
+ klass.extend ClassMethods
44
+ end
45
+
46
+ module ClassMethods
47
+
48
+ def proxy
49
+ proxy = Ym4r::Anonymizer.config[:proxy]
50
+ end
51
+
52
+ def use_proxy?
53
+ !!proxy
54
+ end
55
+
56
+ def proxy_use_auth?
57
+ !!(proxy_username && proxy_passwprd)
58
+ end
59
+
60
+ def proxy_connection_string
61
+ proxy ? "http://#{proxy[:host]}:#{proxy[:port]}" : nil
62
+ end
63
+
64
+ def proxy_username
65
+ proxy ? proxy[:username] : nil
66
+ end
67
+
68
+ def proxy_password
69
+ proxy ? proxy[:password] : nil
70
+ end
71
+
72
+ def google_api_keys
73
+ Ym4r::Anonymizer.config[:google_api_keys] || Array(Ym4r::GoogleMaps::API_KEY)
74
+ end
75
+
76
+ def yahoo_app_ids
77
+ Ym4r::Anonymizer.config[:yahoo_app_ids] || Array(Ym4r::YahooMaps::APP_ID)
78
+ end
79
+
80
+ # iterate next API key
81
+ def url_with_next_key(url)
82
+ case self.name
83
+ when "Ym4r::GoogleMaps::Geocoding"
84
+ @@google_api_key_index ||= 0
85
+ @@google_api_key_index = 0 if @@google_api_key_index >= google_api_keys.size
86
+ key = google_api_keys[@@google_api_key_index]
87
+ @@google_api_key_index += 1
88
+ url.sub(/&key=(.*)&output=/, "&key=#{key}&output=")
89
+ when "Ym4r::YahooMaps::BuildingBlock::Geocoding"
90
+ @@yahoo_app_id_index ||= 0
91
+ @@yahoo_app_id_index = 0 if @@yahoo_app_id_index >= yahoo_app_ids.size
92
+ key = yahoo_app_ids[@@yahoo_app_id_index]
93
+ @@yahoo_app_id_index += 1
94
+ url.sub(/appid=(.+)&/, "appid=#{key}&")
95
+ else
96
+ raise "unknown Geocoder #{self.name} for anonymizing ym4r"
97
+ end
98
+ end
99
+
100
+ def open(url)
101
+ url = url_with_next_key(url)
102
+ puts "[#{Time.now}] fetching #{url}#{' with proxy ' + proxy_connection_string if use_proxy?}" if Ym4r::Anonymizer.verbos?
103
+ if use_proxy?
104
+ opts = if proxy_use_auth?
105
+ {:proxy_http_basic_authentication => [proxy_connection_string, proxy_username, proxy_password]}
106
+ else
107
+ {:proxy => proxy_connection_string}
108
+ end
109
+ Kernel.open(url, opts)
110
+ else
111
+ Kernel.open(url)
112
+ end
113
+ end
114
+ end
115
+ end
116
+ end
117
+
118
+ # include Anonymizer methods with Geocoders
119
+ Ym4r::GoogleMaps::Geocoding.send :include, Ym4r::Anonymizer
120
+ Ym4r::YahooMaps::BuildingBlock::Geocoding.send :include, Ym4r::Anonymizer
121
+
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/ym4r/anonymizer', __FILE__)
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = %q{ym4r-anonymizer}
6
+ s.version = Ym4r::Anonymizer::Version
7
+ s.date = %q{2011-05-11}
8
+ s.authors = ["Zhang, Xiaohui"]
9
+ s.email = %q{xiaohui@zhangxh.net}
10
+ s.homepage = %q{http://github.com/xiaohui-zhangxh/ym4r-anonymizer}
11
+ s.summary = %q{An extension of Ym4r, allow us geocode address with proxy and automatically iterate API keys}
12
+ s.description = %q{An extension of Ym4r, allow us geocode address with proxy and automatically iterate API keys}
13
+
14
+ s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
15
+ s.add_development_dependency "bundler", ">= 1.0.0"
16
+
17
+ s.files = `git ls-files`.split("\n")
18
+ s.require_path = "lib"
19
+
20
+ s.add_runtime_dependency(%q<ym4r>, [">= 0"])
21
+ end
metadata ADDED
@@ -0,0 +1,104 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ym4r-anonymizer
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Zhang, Xiaohui
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-05-11 00:00:00 Z
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: bundler
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ hash: 23
29
+ segments:
30
+ - 1
31
+ - 0
32
+ - 0
33
+ version: 1.0.0
34
+ type: :development
35
+ version_requirements: *id001
36
+ - !ruby/object:Gem::Dependency
37
+ name: ym4r
38
+ prerelease: false
39
+ requirement: &id002 !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ hash: 3
45
+ segments:
46
+ - 0
47
+ version: "0"
48
+ type: :runtime
49
+ version_requirements: *id002
50
+ description: An extension of Ym4r, allow us geocode address with proxy and automatically iterate API keys
51
+ email: xiaohui@zhangxh.net
52
+ executables: []
53
+
54
+ extensions: []
55
+
56
+ extra_rdoc_files: []
57
+
58
+ files:
59
+ - .gitignore
60
+ - CHANGELOG
61
+ - Gemfile
62
+ - Gemfile.lock
63
+ - README
64
+ - Rakefile
65
+ - examples/geocode.rb
66
+ - examples/ym4r_anonymizer.yml
67
+ - lib/ym4r/anonymizer.rb
68
+ - ym4r-anonymizer.gemspec
69
+ homepage: http://github.com/xiaohui-zhangxh/ym4r-anonymizer
70
+ licenses: []
71
+
72
+ post_install_message:
73
+ rdoc_options: []
74
+
75
+ require_paths:
76
+ - lib
77
+ required_ruby_version: !ruby/object:Gem::Requirement
78
+ none: false
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ hash: 3
83
+ segments:
84
+ - 0
85
+ version: "0"
86
+ required_rubygems_version: !ruby/object:Gem::Requirement
87
+ none: false
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ hash: 11
92
+ segments:
93
+ - 1
94
+ - 2
95
+ version: "1.2"
96
+ requirements: []
97
+
98
+ rubyforge_project:
99
+ rubygems_version: 1.7.2
100
+ signing_key:
101
+ specification_version: 3
102
+ summary: An extension of Ym4r, allow us geocode address with proxy and automatically iterate API keys
103
+ test_files: []
104
+