-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcronjob.rb
111 lines (91 loc) · 3.64 KB
/
cronjob.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
#!/usr/bin/env ruby
require 'bundler/setup'
Bundler.require
require_relative './env.rb'
require 'pry'
users = DB[:users].all
def update_facebook(country, region, locality, access_token)
resp = RestClient.post('https://graph.facebook.com/me/everydaycity:arrive_in', {
city: "http://everydaycity.com/city/#{country}/#{region}/#{locality}",
access_token: access_token
}) {|response, request, result| response }
JSON.parse resp, symbolize_names: true
end
users.each do |user|
begin
last = Geoloqi.get user[:lq_access_token], 'location/last'
rescue Geoloqi::ApiError => e
if e.type == 'no_recent_location'
puts "no recent location, skipping user #{user[:geoloqi_user_id]}"
next
else
fail
end
end
lat, lng = last[:location][:position][:latitude], last[:location][:position][:longitude]
bing_url = "http://dev.virtualearth.net/REST/v1/Locations/#{lat},#{lng}?includeEntityTypes=PopulatedPlace&key=#{$config['bing_geocoding_key']}"
bing_resp = JSON.parse(RestClient.get(bing_url), symbolize_names: true)[:resourceSets].first[:resources].first
# If geocoder returns nothing, go to next.
if bing_resp.nil?
puts bing_url
puts "BING returned no city, skip"
next
end
city_args = {
name: bing_resp[:name],
bbox: bing_resp[:bbox].join(','),
lat: bing_resp[:point][:coordinates][0],
lng: bing_resp[:point][:coordinates][1],
locality: bing_resp[:address][:locality],
region: bing_resp[:address][:adminDistrict],
country: bing_resp[:address][:countryRegion],
date_created: Time.now
}
city = DB[:cities].filter(country: city_args[:country], region: city_args[:region], locality: city_args[:locality]).first
if city.nil?
DB[:cities] << city_args
city = DB[:cities][name: bing_resp[:name]]
end
# If token expires within an hour (or earlier), refresh the token.
if Time.at(user[:fb_expiration_date] || 0) - Time.now < 3600
args = {
client_id: $config['fb_client_id'],
client_secret: $config['fb_client_secret'],
grant_type: 'fb_exchange_token',
fb_exchange_token: user[:fb_access_token]
}
res = RestClient.post("https://graph.facebook.com/oauth/access_token", args) {|response, request, result| response }
res = Rack::Utils.parse_query res
unless res['error']
puts "UPDATING TOKEN"
DB[:users].filter(geoloqi_user_id: user[:geoloqi_user_id]).update(fb_access_token: res['access_token'], fb_expiration_date: Time.now.to_i+res['expires'].to_i)
end
end
if user[:current_city] != city[:name]
puts "user current city #{user[:current_city]} is old, changing to #{city[:name]}"
DB[:users].filter(geoloqi_user_id: user[:geoloqi_user_id]).update current_city: city[:name]
retry_attempt = 0
while true
fb_resp = update_facebook city[:country], city[:region], city[:locality], user[:fb_access_token]
if fb_resp[:error] && fb_resp[:error][:message] =~ /Transfer failed/
retry_attempt += 1
puts "TRANSFER FAILED, RETRYING (attempt ##{retry_attempt})"
else
break
end
if retry_attempt == 5
puts "TRANSFER FAILED #{retry_attempt} times for user #{user[:geoloqi_user_id]}, skipping"
next
end
end
puts "FB RESPONSE: #{fb_resp}"
DB[:visits] << {
user_id: user[:id],
city_id: city[:id],
date_visited: Time.now,
fb_post_id: fb_resp[:id]
}
# Send a message to the user
Geoloqi.post $config['geoloqi_app_token'], 'message/send', :text => "Welcome to #{city[:name]}!", :user_id => user[:geoloqi_user_id], :layer_id => $config['geoloqi_layer_id']
end
end