<commit_before># encoding=utf-8
require 'sinatra'
require 'logger'
require 'json'
require 'omniauth'
require 'omniauth-rdio'
require 'rdio'
require 'digest'
require 'open-uri'
require 'nokogiri'
require_relative 'backend/google_playlist'

RDIO_API_KEY = ENV['RDIO_API_KEY']
RDIO_API_SHARED_SECRET = ENV['RDIO_API_SHARED_SECRET']

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 get_rdio_client session
  client = Rdio::Client.new(RDIO_API_KEY, RDIO_API_SHARED_SECRET)
  if session[:rdio_access_token] && session[:rdio_access_secret]
    client.access_token = session[:rdio_access_token]
    client.secret = session[:rdio_access_secret]
  end
  client
end

def strip_smart_quotes str
  str.gsub(/[‘’]/, "'").gsub(/[”“]/, '"')
end

use OmniAuth::Builder do
  provider :rdio, RDIO_API_KEY, RDIO_API_SHARED_SECRET
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 = 'http://ws.audioscrobbler.com/2.0/?' +
        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<commit_msg>Move Last.fm API URL into constant in .rb<commit_after># encoding=utf-8
require 'sinatra'
require 'logger'
require 'json'
require 'omniauth'
require 'omniauth-rdio'
require 'rdio'
require 'digest'
require 'open-uri'
require 'nokogiri'
require_relative 'backend/google_playlist'

RDIO_API_KEY = ENV['RDIO_API_KEY']
RDIO_API_SHARED_SECRET = ENV['RDIO_API_SHARED_SECRET']
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 get_rdio_client session
  client = Rdio::Client.new(RDIO_API_KEY, RDIO_API_SHARED_SECRET)
  if session[:rdio_access_token] && session[:rdio_access_secret]
    client.access_token = session[:rdio_access_token]
    client.secret = session[:rdio_access_secret]
  end
  client
end

def strip_smart_quotes str
  str.gsub(/[‘’]/, "'").gsub(/[”“]/, '"')
end

use OmniAuth::Builder do
  provider :rdio, RDIO_API_KEY, RDIO_API_SHARED_SECRET
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
{'fraction_non_alphanumeric': 0.15455417066155322, 'fraction_numerical': 0.0015340364333652923, 'mean_word_length': 4.543039319872476, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 4}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4895993', 'n_tokens_mistral': 1958, 'n_tokens_neox': 1947, 'n_words': 374}