-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.rb
More file actions
101 lines (77 loc) · 2.32 KB
/
Copy pathapp.rb
File metadata and controls
101 lines (77 loc) · 2.32 KB
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
require 'rubygems'
require 'json/pure'
require 'httparty'
require 'ap'
module FB
class BaseClient
include HTTParty
end
module Graph
class Client < BaseClient
base_uri "https://graph.facebook.com"
def self.access_token= value
default_params :access_token => value
end
end
end
module API
class Client < BaseClient
base_uri "https://api.facebook.com"
def self.access_token= value
default_params :token => value
end
end
end
def self.access_token= value
API::Client.access_token= value
Graph::Client.access_token= value
end
end
class GoogleGeocoder
include HTTParty
#required params
# address or latlng
# sensor
#optional
# bounds
# region
# language
#
base_uri "http://maps.google.com/maps/api/geocode/json"
def self.geocode address
get '', :query => {:address => address, :sensor => false}
end
end
access_token = "1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111"
FB.access_token= access_token
result = FB::Graph::Client.get('/me/friends', :query => {:fields => ['location', 'name', 'hometown']})
friends = result.delete "data"
located_friends = friends.select{|f| f["location"] && f["location"]["name"]}
locationless_friends = friends.reject{|f| f["location"] && f["location"]["name"]}
hometown_friends = friends.select {|f| f["hometown"] && f["hometown"]["name"]}
friends_by_location = located_friends.inject({}) do |addresses,friend|
address = friend["location"]["name"]
addresses[address] ||= {"current_people"=>[]}
addresses[address]["current_people"] << friend
addresses
end
hometown_friends.each do |friend|
address = friend["hometown"]["name"]
friends_by_location[address] ||= {}
friends_by_location[address]["hometown_people"] ||= []
friends_by_location[address]["hometown_people"] << friend
end
ap friends_by_location
exit
friends_by_location.each do |location,attrs|
result = GoogleGeocoder.geocode location
if result["status"] != "OK"
next if result["status"] =="ZERO_RESULTS"
end
attrs["latlng"] = result["results"].first["geometry"]["location"]
end
File.open "output.json","w" do |f|
f.puts "awesome(#{
{:located => friends_by_location.map{|loc,attrs| attrs["name"]=loc;attrs},
:missing => locationless_friends}.to_json})"
end