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