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