-
Notifications
You must be signed in to change notification settings - Fork 3
/
scratch.rb
160 lines (137 loc) · 3.74 KB
/
scratch.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#! /usr/bin/env ruby
require 'csv'
require 'Time'
require 'Date'
require 'chronic'
require 'geocoder'
require 'dotenv'
require 'json'
Dotenv.load
Geocoder.configure(
lookup: :opencagedata,
api_key: ENV['OPENCAGE_API_KEY'],
set_timeout: 15
)
# urls = %w(www.thomsonreuters.com http://www.lib.virginia.edu/ https://openvault.wgbh.org http://www.library.northwestern.edu www.cni.org)
def clean_website(link)
link = "http://#{link}" unless link.nil? || link[/^https?:\/\//]
link
end
@ndsa_users = 'tmp/NDSA_Members_current.csv'
@responses = 'tmp/ndsa_responses.csv'
@geocoded_responses = 'tmp/geocoded_ndsa_responses.csv'
def generate_table_data
json_string = '---
layout: null
permalink: /data/members.json
---
['
CSV.foreach(@responses, encoding: 'ISO-8859-1', headers: true) do |row|
json_string += "{
\"organization\": \"#{row['Organization Name']}\",
\"state\": \"#{row['State']}\",
\"focus\": \"#{row['Digital Preservation Focus']}\"
},"
end
json_string += ']'
end
# puts generate_table_data
def geocode_csv
CSV.open(@geocoded_responses, 'wb') do |csv|
CSV.foreach(@responses, encoding: 'ISO-8859-1', headers: true) do |row|
csv << row
puts row.inspect
end
end
end
# puts geocode_csv
# puts generate_table_data
def generate_map_data
map_nodes = 'tmp/ndsa_nodes.csv'
counter = 0
json_string = 'var members = {
"type": "FeatureCollection",' \
'"features": ['
CSV.foreach(map_nodes, encoding: 'ISO-8859-1', headers: true) do |row|
counter += 1
organization = row['Organizati'].to_json
date = Chronic.parse(row['Timestamp']).strftime('%m/%d/%Y')
# address = "#{row['Street Address']}, #{row['City']}, #{row['State']} #{row['Zip Code']}"
website = clean_website row['Website']
popup = <<-EOT
<h2><a href="#{website}">#{organization}</a></h2><p>Partner since <strong>#{date}</strong>.</p>
EOT
# result = geocode(address)
# lat,lon = 0
#
# lon = result[:longitude] unless result[:longitude] == nil?
# lat = result[:latitude] unless result[:latitude] == nil?
json_string += '{"geometry":{"type":"Point","coordinates":['
json_string += "#{row['x']}, #{row['y']}]},"
json_string += '"type": "Feature", "properties": { "popupContent": '
json_string += popup.strip!.to_json
json_string += "\n" + '},'
json_string += "\n" + '"id": ' + counter.to_s
json_string += '}'
json_string += ','
end
json_string += ']};'
end
puts generate_map_data
def geocode(address)
result = Geocoder.search(address).first
{
latitude: result.latitude,
longitude: result.longitude
}
end
# puts generate_map_data
# def convert_data
# CSV.open('tmp/ndsa_responses.csv', 'wb') do |csv|
# CSV.foreach(ndsa_users, encoding: 'ISO-8859-1', headers: true).each do |row|
# counter += 1
#
# timestamp = Chronic.parse(row[0])
# org_name = row[1]
# address = row[2]
# row[3] =~ /^([^,]*),\s*(\w*)\s*(\d*)?$/
#
# website = clean_website row[4]
# name = row[5]
# phone = row[7]
# email = row[8]
# sector = ''
# groups = ''
# description = ''
# comm_name = row[9]
# comm_phone = row[11]
# comm_email = row[12]
# auth_name = row[13]
# auth_phone = row[15]
# auth_email = row[16]
#
# csv << [
# timestamp,
# org_name,
# address,
# Regexp.last_match(1),
# Regexp.last_match(2),
# Regexp.last_match(3),
# website,
# name,
# phone,
# email,
# sector,
# groups,
# description,
# comm_name,
# comm_phone,
# comm_email,
# auth_name,
# auth_phone,
# auth_email
# ]
# end
# end
# end
# convert_data