QUESTA ROBA È UNA MERDA

This commit is contained in:
encrypt 2017-02-17 10:57:50 +00:00
commit 7d3d9de0e6
3 changed files with 88 additions and 0 deletions

38
app.rb Normal file
View file

@ -0,0 +1,38 @@
require 'sinatra'
require 'sinatra/contrib'
require 'json'
get '/' do
erb :index
end
post '/add' do
puts params.to_s
if params[:link] and params[:link] =~ /http(s):\/\/(?:www\.)?youtu(?:be\.com\/watch\?v=|\.be\/)([\w\-]+)(&(amp;)?[\w\?=]*)?/
File.open('/tmp/radio', 'w') do |f|
f.puts(params[:link])
f.flush
end
200
else
403
end
end
get '/playlist', :provides => [:html, :json] do
songs = `mpc playlist | cut -d '#' -f 2 | base64 -d`
songs = songs.split("\n")
respond_to do |format|
format.json { songs.to_json }
format.html { erb :playlist, locals: { songs: songs } }
end
end
get '/info' do
title = `mpc | head -n 1 | cut -d '#' -f 2 | base64 -d`
title.gsub("\n", "")
content_type :json
{
title: title
}.to_json
end

45
views/index.erb Normal file
View file

@ -0,0 +1,45 @@
<!doctype html>
<html>
<head>
<title>RadioMolestia</title>
<style>
body {
text-align: center;
}
.date {
font-size: 9pt;
}
form {
padding-top: 25%;
}
input {
padding: .375rem .75rem;
font-size: 1rem;
line-height: 1.5;
color: #55595c;
background-color: #fff;
background-image: none;
border: 1px solid #ccc;
border-radius: .25rem;
}
</style>
<script>
function addSong() {
var request = new XMLHttpRequest();
request.open('POST', '/add', true);
request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
data = "link="+document.getElementsByName('link')[0].value
request.send(data);
}
</script>
</head>
<body>
<form>
<input type="text" name="link"></link>
<input type="button" onclick="addSong()" value="play"/>
</form>
<br/>
<a href="/listen">/listen</a><br/>
<a href="/playlist">/playlist</a>
</body>
</html>

5
views/playlist.erb Normal file
View file

@ -0,0 +1,5 @@
<ul>
<% songs.each do |song| %>
<li><%= song %></li>
<% end %>
</ul>