Add support for view-only mode.
[patchfork.git] / player / nowplaying.php
1 <?php
2 /* 
3     Pitchfork Music Player Daemon Client
4     Copyright (C) 2007  Roger Bystrøm
5
6     This program is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; version 2 of the License.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License along
16     with this program; if not, write to the Free Software Foundation, Inc.,
17     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20         /* This is a slightly hidden feature
21          * Also goes to show how impractical it is to program against pitchfork core,
22          * but then again that never was the point :)
23          *
24          * usage: curl -s http://yourmpdhost/path/player/nowplaying.php
25          *
26          * in irssi you would do: 
27          * /alias np exec - -o curl -s http://..
28          *
29          * Never invoke it directly using php command, should always go through web-server
30          * (think of permissions!)
31          *
32          */
33
34         /* If you're using a password you have a little case with specifying the password thing
35          * The only solution as of yet is to not require a password to see now playing
36          *
37          * This does not grant access to anything else though 
38          *
39          * Just uncomment this line if that is something you want to do, I might add this option to the 
40          * configure screen some day though
41          *
42          */
43         $no_require_login = "true";
44
45
46
47         require_once("../inc/base.php");
48         $iamincluded = true;
49         require_once("../player/metadata.php"); 
50         header("Content-Type: text/plain");
51         
52         try {
53                 $pl = get_playback();
54                 $info = $pl->getCurrentSong();
55                 $pl->disconnect();
56
57                 $rlyric = "";
58
59                 if(isset($info['Artist'])&&isset($info['Title'])) {
60                         $file = get_lyric_filename($info['Artist'], $info['Title']);
61                         $lyric = false; 
62                         if(file_exists($file)) {
63                                 $lyric = _get_lyric_cache($file, $info['file']);
64                         }
65                         else {
66                                 $lyric = @_get_lyric_lyricwiki($info['Artist'], $info['Title'], $info['file']);
67                         }
68
69                         if($lyric!==FALSE) {
70                                 $lyric = simplexml_load_string($lyric);
71                                 if($lyric&&$lyric->result->lyric) {
72                                         $lyric = (string) $lyric->result->lyric;
73                                         $lyric = explode("\n", $lyric);
74                                         $lines = count($lyric);
75                                         $tmp = "";
76                                         /* try until we hit something */
77                                         while(strlen($tmp)<=0&&$lines>0) {
78                                                 $tmp = $lyric[rand(0, count($lyric)-1)];
79                                                 if(strlen($tmp)) {
80                                                         $rlyric = substr(trim($tmp), 0, 60);
81                                                         if($rlyric) {
82                                                                 $rlyric = " -~\"" . $rlyric . "\"";
83
84                                                         }
85                                                 }
86                                                 $lines--;
87                                         }
88                                 }
89                         }
90                         echo "np: " . $info['Artist'] . " - " . $info['Title'] . $rlyric;
91                 }
92                 else {
93                         echo "np: ";
94                         if( isset($info['Artist']))
95                                 echo $info['Artist'] . " - ";
96                         if(isset($info['Title'])) 
97                                 echo $info['Title'] . " - ";
98                         if(isset($info['file']))
99                                 echo $info['file'];
100                         else echo "not playing";
101                 }
102         }
103         catch(PEARException $e) {
104                 echo "error contacting mpd";
105         }
106         echo "\n";
107 ?>
This page took 0.022944 seconds and 4 git commands to generate.