-
Notifications
You must be signed in to change notification settings - Fork 39
/
app.rb
89 lines (73 loc) · 3.21 KB
/
app.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
require 'rubygems'
require 'bundler/setup'
require 'yaml'
Bundler.require :default
CREDENTIALS = File.open('./keys.yml') { |y| YAML::load(y) }
require './goog_geocoder'
CLIENT_ID = CREDENTIALS['instagram_client_id']
CLIENT_SECRET = CREDENTIALS['instagram_secret']
REQUEST_URI = CREDENTIALS['root_uri']
class QIS < Sinatra::Base
enable :sessions
set :raise_errors, Proc.new { false }
set :show_exceptions, false
get '/' do
if !params["code"] && !session[:access_token]
redirect "https://api.instagram.com/oauth/authorize/?client_id=#{CLIENT_ID}&redirect_uri=#{REQUEST_URI}&response_type=code"
end
if params["code"]
auth_obj = JSON.parse(RestClient.post("https://api.instagram.com/oauth/access_token", {
:client_id => CLIENT_ID,
:client_secret => CLIENT_SECRET,
:grant_type => "authorization_code",
:redirect_uri => REQUEST_URI,
:code => params["code"]
}))
session[:access_token] = auth_obj["access_token"]
end
erb :index
end
post '/result' do
@search_params = {}
if params["place"]
goog = GoogGeocoder.new(params["place"])
if !goog.resp["results"].empty?
geometry = goog.resp["results"].first["geometry"]["location"]
@search_params[:lng] = geometry["lng"]
@search_params[:lat] = geometry["lat"]
end
end
@search_params[:text] = params["text"] if params["text"]
@search_params[:min_timestamp] = Time.parse("#{params["start_date"]} #{params["start_time"]}").to_i if params["start_date"] && params["start_time"]
@search_params[:max_timestamp] = Time.parse("#{params["end_date"]} #{params["end_time"]}").to_i if params["end_date"] && params["end_time"]
@search_params[:distance] = params["distance"] if params["distance"]
@search_params[:user] = params["user"].gsub(/@/,"") if params["user"] && params["user"].length > 0
@search_params[:tag] = params["tag"].gsub(/#/,"") if params["tag"] && params["tag"].length > 0
if @search_params[:user]
users = JSON.parse(RestClient.get("https://api.instagram.com/v1/users/search", {:params => {:q => @search_params[:user], :count => 1, :access_token => session[:access_token]}}))
if users['data'].length > 0
user_id = users['data'][0]['id']
results = RestClient.get("https://api.instagram.com/v1/users/#{user_id}/media/recent", :params => {
:access_token => session[:access_token],
:min_timestamp => @search_params[:min_timestamp],
:max_timestamp => @search_params[:max_timestamp],
:count => 1000
})
end
elsif @search_params[:tag]
results = RestClient.get("https://api.instagram.com/v1/tags/#{@search_params[:tag]}/media/recent", {:params => {:access_token => session[:access_token], :count => 1000}})
else
results = RestClient.get("https://api.instagram.com/v1/media/search", {:params => @search_params.merge!({:access_token => session[:access_token]})})
end
@results = JSON.parse(results)
erb :result
end
error do
err = request.env['sinatra.error']
if err.class == RestClient::BadRequest
@error = JSON.parse(err.response)
end
erb :err
end
run! if app_file == $0
end