This repository has been archived by the owner on Apr 15, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.rb
85 lines (73 loc) · 2.38 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
# encoding=utf-8
require 'sinatra'
require 'logger'
require 'json'
require 'digest'
require 'open-uri'
require 'nokogiri'
require_relative 'backend/google_playlist'
LASTFM_API_URL = 'http://ws.audioscrobbler.com/2.0/'
enable :sessions, :logging
set :session_secret, ENV['SESSION_KEY']
before do
logger.level = Logger::DEBUG
end
if ENV['RACK_ENV'] == 'production'
set :public_folder, 'dist'
set :assets_folder, 'dist'
set :bower_root, 'dist'
else
set :public_folder, 'app'
set :assets_folder, '.tmp'
set :bower_root, File.dirname(__FILE__)
end
def strip_smart_quotes str
str.gsub(/[‘’]/, "'").gsub(/[”“]/, '"')
end
use Rack::Static, urls: ['/styles', '/scripts/controllers', '/scripts/services',
'/scripts/models'], root: settings.assets_folder
use Rack::Static, urls: ['/views', '/images'], root: settings.public_folder
use Rack::Static, urls: ['/bower_components'], root: settings.bower_root
get '/' do
send_file File.join(settings.public_folder, 'index.html')
end
get '/config.json' do
content_type 'application/json'
{lastfm_api_key: ENV['LASTFM_API_KEY']}.to_json
end
get '/scripts/app.js' do
send_file File.join(settings.public_folder, 'scripts', 'app.js')
end
get '/scripts/routes.js' do
send_file File.join(settings.public_folder, 'scripts', 'routes.js')
end
get '/lastfm_auth' do
token = params[:token]
query_params = {api_key: ENV['LASTFM_API_KEY'],
method: 'auth.getsession',
token: token}
signature = query_params.map {|k, v| "#{k}#{v}" }.join('') +
ENV['LASTFM_API_SECRET']
signature = Digest::MD5.hexdigest(signature)
# See http://www.last.fm/api/show/auth.getSession
url = "#{LASTFM_API_URL}?" +
query_params.map {|k, v| "#{k}=#{v}" }.join('&') +
"&api_sig=#{signature}"
lastfm_session_xml = open(url).read
doc = Nokogiri::XML(lastfm_session_xml)
lastfm_user = doc.at_xpath('//lfm//session//name').content
lastfm_session_key = doc.at_xpath('//lfm//session//key').content
session[:lastfm_user] = lastfm_user
session[:lastfm_session_key] = lastfm_session_key
redirect "/index.html#/lastfm_auth/#{lastfm_user}"
end
post '/google/playlist' do
content_type :json
playlist = GooglePlaylist.new(params)
response = playlist.set_cookie
if response.is_a?(Net::HTTPSuccess)
response = playlist.create
end
status response.code
response.body.to_json
end