]> CyberLeo.Net >> Repos - CDN/metadata.git/blob - app.rb
Hopefully the last screwing around
[CDN/metadata.git] / app.rb
1 require 'sinatra/base'
2 require 'sinatra/json'
3 require 'yajl'
4 require 'yajl/json_gem'
5 require 'csv'
6
7 $set = "cheesy2"
8 $metafile = "tmp/songtitle"
9
10 def load_tracklist()
11   file = "sets/#{$set}.csv"
12   csv = CSV.read(File.open(file))
13   names = csv.shift
14   $tracklist = csv.map do |row|
15     Hash[*names.zip(row).flatten]
16   end
17 end
18
19 load_tracklist
20
21 def tracklist_json
22   $tracklist.to_json
23 end
24
25 class App < Sinatra::Base
26   configure do
27     set :protection, :except => :frame_options
28   end
29
30   get '/tracklist.json' do
31     json $tracklist
32   end
33
34   post '/commit' do
35     index = params["index"].to_i
36     if ( index < 0 || index > $tracklist.length )
37       status 404
38       return "No such track index."
39     end
40
41     title = ( index < 1 ) ? "-" : $tracklist[index - 1]["title"]
42
43     File.open($metafile, "a+") do |fp|
44       fp.flock(File::LOCK_EX)
45       fp.seek(0)
46       fp.truncate(0)
47       fp.puts(title)
48       fp.flush()
49       fp.flock(File::LOCK_UN)
50     end
51     "Updated."
52   end
53
54   get '/js/handler.js' do
55     content_type 'text/javascript'
56     erb :handler_js
57   end
58
59   get '/css/meta.css' do
60     content_type 'text/css'
61     erb :meta_css
62   end
63
64   get '/' do
65     erb :index, :locals => { :mixtitle => $set, :tracklist => tracklist_json }
66   end
67 end