query-nyc 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,3 @@
1
+ pkg/*
2
+ *.gem
3
+ .bundle
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in query-nyc.gemspec
4
+ gemspec
5
+
6
+ gem "nokogiri"
7
+ gem "rest-client"
@@ -0,0 +1,2 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
@@ -0,0 +1,151 @@
1
+ module Query
2
+ module Nyc
3
+ class << self; attr_accessor :address end
4
+
5
+ def self.fetch_uri(houseno, street, boro)
6
+
7
+ boro_option = name_to_number(boro)
8
+
9
+ uri = "http://a810-bisweb.nyc.gov/bisweb/PropertyProfileOverviewServlet?" + "boro=#{boro_option}" + "&houseno=#{houseno}" + "&street=#{CGI.escape(street)}"
10
+
11
+ end
12
+
13
+ def self.building_at(houseno, street, boro)
14
+ require "nokogiri"
15
+
16
+ boro_option = name_to_number(boro)
17
+
18
+ uri = "http://a810-bisweb.nyc.gov/bisweb/PropertyProfileOverviewServlet?" + "boro=#{boro_option}" + "&houseno=#{houseno}" + "&street=#{CGI.escape(street)}"
19
+
20
+ @page = Nokogiri::HTML(open(uri))
21
+
22
+ retObj = {:landlords=>[],:building=>[]}
23
+
24
+ msg = @page.css(".errormsg").first.content rescue nil
25
+ if msg == nil
26
+ tax_block = @page.xpath("//table[3]/tr[2]/td[9]/text()").to_s.sub(/:/,'').to_i
27
+ tax_lot = @page.xpath("//table[3]/tr[3]/td[9]/text()").to_s.sub(/:/,'').to_i
28
+
29
+ violations_count = @page.xpath("//td//tr[(((count(preceding-sibling::*) + 1) = 3) and parent::*)]//*[(((count(preceding-sibling::*) + 1) = 2) and parent::*)]/text()").to_s
30
+ open_violations = @page.xpath("//td//tr[(((count(preceding-sibling::*) + 1) = 3) and parent::*)]//*[(((count(preceding-sibling::*) + 1) = 3) and parent::*)]/text()").to_s
31
+ violations_link = "http://a810-bisweb.nyc.gov/bisweb/" + @page.xpath("//tr[(((count(preceding-sibling::*) + 1) = 3) and parent::*)]//b//a").attribute("href").to_s rescue ""
32
+
33
+ complaints_count = @page.xpath("//td//tr[(((count(preceding-sibling::*) + 1) = 2) and parent::*)]//*[(((count(preceding-sibling::*) + 1) = 2) and parent::*)]/text()")[0].to_s
34
+ open_complaints = @page.xpath("//td//tr[(((count(preceding-sibling::*) + 1) = 2) and parent::*)]//*[(((count(preceding-sibling::*) + 1) = 3) and parent::*)]/text()").to_s
35
+ complaints_link = "http://a810-bisweb.nyc.gov/bisweb/" + @page.xpath("//tr[(((count(preceding-sibling::*) + 1) = 2) and parent::*)]//b//a").attribute("href").to_s rescue ""
36
+
37
+ landmark_status = @page.xpath('//tr[(((count(preceding-sibling::*) + 1) = 6) and parent::*)]//*[contains(concat( " ", @class, " " ), concat( " ", "content", " " )) and (((count(preceding-sibling::*) + 1) = 2) and parent::*)]/text()').to_s
38
+
39
+ @page = Nokogiri::HTML(open('http://api.blocksandlots.com/blankslate/json/data/743cd788-eb98-4fb6-af18-0811261ad168/records/search?apikey=cvq842zthjdvr25cq9p5s6db&Block='+tax_block.to_s+'&Lot='+tax_lot.to_s+'&rp=350&em=true&_=1289069608577'))
40
+
41
+ arr_of_landlords = retObj[:landlords]
42
+ arr_of_building = retObj[:building]
43
+ jdata = JSON.parse(@page.content)
44
+ owners_array = jdata["application"][0]["entity"][0]["record"]
45
+ owners_array.each do |owner|
46
+ ret = {}
47
+ skip = false
48
+ owner["field"].each { |f|
49
+ case f["fieldName"]
50
+ when "Owner Name"
51
+ if f["fieldValue"].strip.present?
52
+ ret[:owner] = f["fieldValue"].split(/\s+/).each{ |word| word.capitalize! }.join(' ')
53
+ else
54
+ skip = true
55
+ end
56
+ end
57
+ }
58
+ arr_of_landlords.push(ret) unless skip
59
+ end
60
+
61
+ r = {}
62
+ owners_array[0]["field"].each do |f|
63
+ case f["fieldName"]
64
+ when "Actual Land Value"; r[:land_value] = f["fieldValue"]
65
+ when "Actual Total Value"; r[:total_value] = f["fieldValue"]
66
+ when "Market Value"; r[:market_value] = f["fieldValue"]
67
+ when "Assessment Year"; r[:assessment_year] = f["fieldValue"]
68
+ end
69
+ end
70
+ arr_of_building.push(r)
71
+
72
+ arr_of_building[0][:tax_block] = tax_block
73
+ arr_of_building[0][:tax_lot] = tax_lot
74
+ arr_of_building[0][:violations_count] = violations_count
75
+ arr_of_building[0][:open_violations] = open_violations
76
+ arr_of_building[0][:violations_link] = violations_link
77
+ arr_of_building[0][:complaints_count] = complaints_count
78
+ arr_of_building[0][:open_complaints] = open_complaints
79
+ arr_of_building[0][:complaints_link] = complaints_link
80
+ arr_of_building[0][:landmark_status] = landmark_status.present?
81
+ end
82
+ # Returns a hash of landlords, and the building.
83
+ retObj
84
+ end
85
+
86
+ def self.name_to_number(name)
87
+
88
+ case name
89
+ when "Manhattan"
90
+ 1
91
+ when "The Bronx"
92
+ 2
93
+ when "Brooklyn"
94
+ 3
95
+ when "Queens"
96
+ 4
97
+ when "Staten Island"
98
+ 5
99
+ else
100
+ 0
101
+ end
102
+ end
103
+
104
+ def self.check_existence_with_google(address)
105
+ base = "http://maps.googleapis.com/maps/api/geocode/json?address="
106
+ addr = "#{address},NY".gsub(/\s+/,'+')
107
+ uri = "#{base}#{addr}&sensor=false"
108
+
109
+ google = open(uri).read
110
+ json = JSON.parse(google)
111
+
112
+ partial_match = json["results"][0]["partial_match"] rescue false
113
+ approximate = json["results"][0]["geometry"]["location_type"] == "APPROXIMATE"
114
+
115
+ if (approximate.present? or partial_match.present?)
116
+ false
117
+ else
118
+ # Scoop the address here
119
+ self.address = json["results"][0]["formatted_address"] rescue ""
120
+
121
+ true
122
+ end
123
+ # Returns true or false
124
+ end
125
+
126
+ def self.geocode(address)
127
+ require 'uri'
128
+ require 'rest-client'
129
+
130
+ j = ActiveSupport::JSON
131
+ base_url="http://maps.googleapis.com/maps/api/geocode/json?address="
132
+ url = "#{base_url}#{CGI.escape(address)}&sensor=false"
133
+ retObj = {:lat=>"",:lng=>""}
134
+ RestClient.get(url) do |response, request, result|
135
+ case response.code
136
+ when 200
137
+ res = j.decode(response)["results"][0]
138
+ loc = res["geometry"]["location"] rescue ""
139
+ addr = res["address_components"] rescue ""
140
+
141
+ retObj[:lat] = loc["lat"] rescue ""
142
+ retObj[:lng] = loc["lng"] rescue ""
143
+ retObj[:sublocality] = addr[2]["long_name"] rescue ""
144
+ else
145
+ "BAD RESPONSE"
146
+ end
147
+ end
148
+ retObj
149
+ end
150
+ end
151
+ end
@@ -0,0 +1,5 @@
1
+ module Query
2
+ module Nyc
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "query-nyc/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "query-nyc"
7
+ s.version = Query::Nyc::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Volkan Unsal"]
10
+ s.email = ["spocksplanet@gmail.com"]
11
+ s.homepage = ""
12
+ s.summary = %q{Algorithm for parsing the various data mines of NYC to fetch human readable data about landlords.}
13
+ s.description = %q{This gem started as part of Great Urban Hack. It looks up the records of NYC landlords from openly available public directories.}
14
+
15
+ s.rubyforge_project = "query-nyc"
16
+
17
+ s.files = `git ls-files`.split("\n")
18
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
+ s.require_paths = ["lib"]
21
+ end
metadata ADDED
@@ -0,0 +1,70 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: query-nyc
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 1
9
+ version: 0.0.1
10
+ platform: ruby
11
+ authors:
12
+ - Volkan Unsal
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2011-01-27 00:00:00 -05:00
18
+ default_executable:
19
+ dependencies: []
20
+
21
+ description: This gem started as part of Great Urban Hack. It looks up the records of NYC landlords from openly available public directories.
22
+ email:
23
+ - spocksplanet@gmail.com
24
+ executables: []
25
+
26
+ extensions: []
27
+
28
+ extra_rdoc_files: []
29
+
30
+ files:
31
+ - .gitignore
32
+ - Gemfile
33
+ - Rakefile
34
+ - lib/query-nyc.rb
35
+ - lib/query-nyc/version.rb
36
+ - query-nyc.gemspec
37
+ has_rdoc: true
38
+ homepage: ""
39
+ licenses: []
40
+
41
+ post_install_message:
42
+ rdoc_options: []
43
+
44
+ require_paths:
45
+ - lib
46
+ required_ruby_version: !ruby/object:Gem::Requirement
47
+ none: false
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ segments:
52
+ - 0
53
+ version: "0"
54
+ required_rubygems_version: !ruby/object:Gem::Requirement
55
+ none: false
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ segments:
60
+ - 0
61
+ version: "0"
62
+ requirements: []
63
+
64
+ rubyforge_project: query-nyc
65
+ rubygems_version: 1.3.7
66
+ signing_key:
67
+ specification_version: 3
68
+ summary: Algorithm for parsing the various data mines of NYC to fetch human readable data about landlords.
69
+ test_files: []
70
+