forked from abishekk92/gimme-song
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.rb
152 lines (116 loc) · 3.66 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
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
require "sinatra"
require 'koala'
require 'json'
require 'youtube_search'
require 'net/http'
require 'uri'
enable :sessions
set :raise_errors, false
set :show_exceptions, false
configure :development do
ENV["FACEBOOK_APP_ID"]="391867550875701"
ENV["FACEBOOK_SECRET"]="ba24216257756039c68ad2f050c63f0d"
end
FACEBOOK_SCOPE = 'user_likes,user_photos,user_photo_video_tags,user_interests'
unless ENV["FACEBOOK_APP_ID"] && ENV["FACEBOOK_SECRET"]
abort("missing env vars: please set FACEBOOK_APP_ID and FACEBOOK_SECRET with your app credentials")
end
interest_graph=Array.new
before do
# HTTPS redirect
if settings.environment == :production && request.scheme != 'https'
redirect "https://#{request.env['HTTP_HOST']}"
end
end
helpers do
def host
request.env['HTTP_HOST']
end
def scheme
request.scheme
end
def url_no_scheme(path = '')
"//#{host}#{path}"
end
def url(path = '')
"#{scheme}://#{host}#{path}"
end
def authenticator
@authenticator ||= Koala::Facebook::OAuth.new(ENV["FACEBOOK_APP_ID"], ENV["FACEBOOK_SECRET"], url("/auth/facebook/callback"))
end
end
# the facebook session expired! reset ours and restart the process
error(Koala::Facebook::APIError) do
session[:access_token] = nil
redirect "/auth/facebook"
end
get "/" do
# Get base API Connection
@graph = Koala::Facebook::API.new(session[:access_token])
# Get public details of current application
@app = @graph.get_object(ENV["FACEBOOK_APP_ID"])
#user_interest_graph=Array.new
if session[:access_token]
@user = @graph.get_object("me")
@friends = @graph.get_connections('me', 'friends')
@photos = @graph.get_connections('me', 'photos')
@likes = @graph.get_connections('me', 'likes').first(4)
@music = @graph.get_connections('me', 'music')
@friends_using_app = @graph.fql_query("SELECT uid, name, is_app_user, pic_square FROM user WHERE uid in (SELECT uid2 FROM friend WHERE uid1 = me()) AND is_app_user = 1")
user_interest_graph = @music.each { |music| music['name'] }
File.open('user_graph/'+"#{@user['name']}.json","w") do |f|
f.write(user_interest_graph.to_json)
end
end
erb :index
end
# used by Canvas apps - redirect the POST to be a regular GET
post "/" do
redirect "/"
end
# used to close the browser window opened to post to wall/send to friends
get "/close" do
"<body onload='window.close();'/>"
end
get "/sign_out" do
session[:access_token] = nil
redirect '/'
end
get "/auth/facebook" do
session[:access_token] = nil
redirect authenticator.url_for_oauth_code(:permissions => FACEBOOK_SCOPE)
end
get '/auth/facebook/callback' do
session[:access_token] = authenticator.get_access_token(params[:code])
redirect '/'
end
get '/upload' do
erb :upload
end
get '/music/discover/:user' do
current_user=params[:user]
#sentiment=compute_sentiment("http://192.168.1.1")
interest_graph_json=File.read("user_graph/#{current_user}.json")
interest_graph=JSON.parse(interest_graph_json)
discovery_array=interest_graph.map{ |interest| interest['name'] }
#sentiment_array=sentiment.map{|key,value| song['value'] }
#discovery_array=discovery_array+sentiment_array
@youtube = discovery_array[0..15].map do |search_term|
YoutubeSearch.search(search_term).first['video_id']
end
def compute_sentiment(api_uri)
uri=URI.parse(api_uri)
http=Net::HTTP.new(uri.host,uri.port)
request=Net::HTTP::Get.new(uri.request_uri)
response=http.request(request)
json_response= response.body
return JSON.parse(json_response)
end
erb :video
end
post '/upload' do
File.open('music/' + 'song_buffer.mp3' ,"w") do |f|
f.write(params['myfile'][:tempfile].read)
end
redirect '/music/discover'
end