Skip to content

Commit b282105

Browse files
committed
First commit
0 parents  commit b282105

File tree

9 files changed

+182
-0
lines changed

9 files changed

+182
-0
lines changed

Diff for: mpdcontroller/__init__.py

Whitespace-only changes.

Diff for: mpdcontroller/admin.py

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from django.contrib import admin

Diff for: mpdcontroller/apps.py

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from django.apps import AppConfig
2+
3+
4+
class MpdControllerConfig(AppConfig):
5+
name = 'mpdcontroller'

Diff for: mpdcontroller/models.py

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import subprocess
2+
3+
from django.conf import settings
4+
5+
class MpdController():
6+
def __init__(self):
7+
self.base_command = ["mpc", "--host", settings.MPD_HOST_URL]
8+
9+
def _is_valid_mpd_command(self, command):
10+
return command in [
11+
'next',
12+
'pause',
13+
'play',
14+
'prev',
15+
'stop',
16+
'toggle',
17+
]
18+
19+
def _is_special_command(self, command):
20+
return command in [
21+
'status',
22+
]
23+
24+
def status(self):
25+
process = subprocess.run(self.base_command, stdout=subprocess.PIPE)
26+
output = process.stdout.decode('utf-8').split('\n')[0]
27+
artist, song = output.split(' - ')
28+
return {'artist': artist, 'song': song}
29+
30+
def run(self, command, **kw):
31+
if self._is_valid_mpd_command(command):
32+
process = subprocess.run(self.base_command + [command], stdout=subprocess.PIPE)
33+
result_dict = {'result': process.returncode}
34+
elif self._is_special_command(command):
35+
result_dict = getattr(self, command)()
36+
return result_dict

Diff for: mpdcontroller/templates/mpdcontroller/index.html

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<html>
2+
<head>
3+
<meta content="width=device-width, initial-scale=1" name="viewport">
4+
<style>
5+
body {
6+
background-color: black;
7+
color: white;
8+
}
9+
iframe {
10+
width: 100%;
11+
height: 40px;
12+
border: none;
13+
}
14+
tr > td:nth-child(2) {
15+
font-weight: bold;
16+
}
17+
</style>
18+
</head>
19+
<body>
20+
<table>
21+
<tr>
22+
<td>Artist :</td>
23+
<td id="artist-value"></td>
24+
</tr>
25+
<tr>
26+
<td>Song : </td>
27+
<td id="song-value"></td>
28+
</tr>
29+
</table>
30+
<p>
31+
<a href="{% url 'mpd_controller' %}prev">Prev</a>
32+
<a href="{% url 'mpd_controller' %}toggle">Toggle</a>
33+
<a href="{% url 'mpd_controller' %}next">Next</a>
34+
</p>
35+
<iframe src="{{ mpd_http_output_url }}"></iframe>
36+
<script>
37+
function updateStatus() {
38+
window.fetch('/mpd/status/', {credentials: 'same-origin'})
39+
.then(function(response) {
40+
response.json()
41+
.then(function(data) {
42+
document.querySelector('#artist-value').textContent = data['artist'];
43+
document.querySelector('#song-value').textContent = data['song'];
44+
});
45+
})
46+
.catch(function(response) {
47+
console.log(response);
48+
})
49+
}
50+
51+
function makeCallAsynchronous(event) {
52+
var url = event.target.href;
53+
window.fetch(url, {credentials: 'same-origin'})
54+
.then(function(response) {
55+
response.json()
56+
.then(function(data) {
57+
console.log(data);
58+
updateStatus();
59+
});
60+
})
61+
.catch(function(response) {
62+
console.log(response);
63+
})
64+
65+
event.preventDefault();
66+
}
67+
68+
window.onload = function window_onload() {
69+
updateStatus();
70+
a_tag_list = document.querySelectorAll('a');
71+
for (var i=0; i<a_tag_list.length; i++) {
72+
console.log(a_tag_list[i]);
73+
a_tag_list[i].addEventListener('click', makeCallAsynchronous);
74+
}
75+
}
76+
</script>
77+
</body>
78+
</html>

Diff for: mpdcontroller/tests.py

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from django.test import TestCase
2+
3+
# Create your tests here.

Diff for: mpdcontroller/urls.py

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from django.conf.urls import url
2+
import mpdcontroller.views as views
3+
4+
urlpatterns = [
5+
url(r'(?P<command>.+)/$', views.command),
6+
url(r'^$', views.controller, name='mpd_controller'),
7+
]

Diff for: mpdcontroller/views.py

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from django.conf import settings
2+
from django.contrib.auth.decorators import login_required
3+
from django.http import JsonResponse
4+
from django.shortcuts import render
5+
from mpdcontroller.models import MpdController
6+
7+
@login_required(login_url='/admin/login')
8+
def command(request, command):
9+
result_dict = {'command': command}
10+
mpd_controller = MpdController()
11+
result_dict.update(mpd_controller.run(command))
12+
return JsonResponse(result_dict)
13+
14+
15+
@login_required(login_url='/admin/login')
16+
def controller(request):
17+
return render(request, 'mpdcontroller/index.html', {
18+
'mpd_http_output_url': settings.MPD_HTTP_OUTPUT_URL,
19+
})

Diff for: setup.py

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import os
2+
from setuptools import find_packages, setup
3+
4+
# Utility function to read the README file.
5+
# Used for the long_description. It's nice, because now 1) we have a top level
6+
# README file and 2) it's easier to type in the README file than to put a raw
7+
# string in below ...
8+
def read(fname):
9+
return open(os.path.join(os.path.dirname(__file__), fname)).read()
10+
11+
setup(
12+
name = "django-mpdcontroller",
13+
version = "0.1",
14+
packages=find_packages(),
15+
author = "Nicolas Wavrant",
16+
author_email = "[email protected]",
17+
description = ("A web interface to command the music player daemon service (mpd)."),
18+
license = "BSD",
19+
keywords = "django interface mpd client",
20+
url = "https://github.com/Sebatyne/django-feed-manager",
21+
#long_description = read('README.md'),
22+
classifiers = [
23+
"Environment :: Web Environment",
24+
"Framework :: Django",
25+
"Development Status :: 4 - Beta",
26+
"Topic :: Utilities",
27+
"License :: OSI Approved :: BSD License",
28+
"Operating System :: OS Independent",
29+
"Programming Language :: Python",
30+
"Programming Language :: Python :: 3",
31+
"Topic :: Internet :: WWW/HTTP",
32+
],
33+
)

0 commit comments

Comments
 (0)