| 1 | package Apache::iTunes; | ||
| 2 | 2 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 3 | |||
| 4 | 2 | ▶ | use vars qw($VERSION); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | |||
| 6 | 0 | ▶ | use Apache::Constants qw(:common); |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 7 | use Apache::Util qw(unescape_uri);; | ||
| 8 | use Mac::iTunes; | ||
| 9 | use Text::Template; | ||
| 10 | |||
| 11 | $VERSION = 0.12; | ||
| 12 | |||
| 13 | =head1 NAME | ||
| 14 | |||
| 15 | Apache::iTunes - control iTunes from mod_perl | ||
| 16 | |||
| 17 | =head1 SYNOPSIS | ||
| 18 | |||
| 19 | <Location /iTunes> | ||
| 20 | SetHandler perl-script | ||
| 21 | PerlHandler Apache::iTunes | ||
| 22 | PerlModule Mac::iTunes | ||
| 23 | PerlInitHandler Apache::StatINC | ||
| 24 | PerlSetEnv APACHE_ITUNES_HTML /web/iTunes.html | ||
| 25 | PerlSetEnv APACHE_ITUNES_URL http://10.0.1.2:8080/iTunes/ | ||
| 26 | PerlSetEnv APACHE_ITUNES 1 | ||
| 27 | </Location> | ||
| 28 | |||
| 29 | =head1 DESCRIPTION | ||
| 30 | |||
| 31 | THIS IS ALPHA SOFTWARE. | ||
| 32 | |||
| 33 | This module is currently unmaintained. If you want to take over | ||
| 34 | the care and feeding, write to modules@perl.org. | ||
| 35 | |||
| 36 | |||
| 37 | I am still developing Mac::iTunes, and this module depends | ||
| 38 | mostly on that. This handler does most of the stuff I need | ||
| 39 | it to do, so further development depends on what people | ||
| 40 | ask for or contribute. :) | ||
| 41 | |||
| 42 | =head2 URLs | ||
| 43 | |||
| 44 | After the base URL to the iTunes handler, you can add | ||
| 45 | commands in the path info. Only the first command matters. | ||
| 46 | |||
| 47 | =over 4 | ||
| 48 | |||
| 49 | =item /play, /pause, /stop, /next, /previous | ||
| 50 | |||
| 51 | Does just what it says, just like the iTunes controller. | ||
| 52 | |||
| 53 | =item /back_track | ||
| 54 | |||
| 55 | Restarts the current track | ||
| 56 | |||
| 57 | =item /volume/<number 0-100> | ||
| 58 | |||
| 59 | Sets the volume to a value between 0 and 100. Numbers below | ||
| 60 | 0 are taken as 0, and those above 100 are taken as 100. | ||
| 61 | |||
| 62 | =item /playlist/<playlist> | ||
| 63 | |||
| 64 | Changes the playlist view to <playlist> if it exists. | ||
| 65 | |||
| 66 | =item /track/<number>/<playlist> | ||
| 67 | |||
| 68 | CURRENTLY BROKEN! | ||
| 69 | |||
| 70 | Plays track number <number> in <playlist>. | ||
| 71 | |||
| 72 | =back | ||
| 73 | |||
| 74 | =head2 Template Variables | ||
| 75 | |||
| 76 | This module uses Text::Template because I expect people to | ||
| 77 | hack it for their own templating system (please send back | ||
| 78 | modifications!). | ||
| 79 | |||
| 80 | =over 4 | ||
| 81 | |||
| 82 | =item $base | ||
| 83 | |||
| 84 | The base URL (from APACHE_ITUNES_URL environment variable) | ||
| 85 | |||
| 86 | =item $current | ||
| 87 | |||
| 88 | The current track name | ||
| 89 | |||
| 90 | =item $playlist | ||
| 91 | |||
| 92 | The current playlist | ||
| 93 | |||
| 94 | =item @playlists | ||
| 95 | |||
| 96 | A list of the playlists | ||
| 97 | |||
| 98 | =item @tracks | ||
| 99 | |||
| 100 | A list of tracks in the current playlist (in $playlist) | ||
| 101 | |||
| 102 | =item $version | ||
| 103 | |||
| 104 | The version of Apache::iTunes | ||
| 105 | |||
| 106 | =back | ||
| 107 | |||
| 108 | =head2 Environment variables | ||
| 109 | |||
| 110 | =over 4 | ||
| 111 | |||
| 112 | =item APACHE_ITUNES_URL | ||
| 113 | |||
| 114 | The URL to the mod_perl handler so it can reference itself. | ||
| 115 | |||
| 116 | =item APACHE_ITUNES_HTML | ||
| 117 | |||
| 118 | The location of the template file. | ||
| 119 | |||
| 120 | =back | ||
| 121 | |||
| 122 | =head1 TO DO | ||
| 123 | |||
| 124 | * even though this is mod_perl, Mac::iTunes is still pretty slow. | ||
| 125 | when i get to the optimization stage, Mac::iTunes will get faster | ||
| 126 | and so will this. | ||
| 127 | |||
| 128 | =head1 SOURCE AVAILABILITY | ||
| 129 | |||
| 130 | This source is in GitHub | ||
| 131 | |||
| 132 | https://github.com/CPAN-Adopt-Me/Apache-iTunes | ||
| 133 | |||
| 134 | =head1 AUTHOR | ||
| 135 | |||
| 136 | This module is currently unmaintained. If you want to take over | ||
| 137 | the care and feeding, write to modules@perl.org. | ||
| 138 | |||
| 139 | brian d foy, C<< <bdfoy@cpan.org> >> | ||
| 140 | |||
| 141 | =head1 COPYRIGHT AND LICENSE | ||
| 142 | |||
| 143 | Copyright (c) 2002-2007, brian d foy, All Rights Reserved. | ||
| 144 | |||
| 145 | You may redistribute this under the same terms as Perl itself. | ||
| 146 | |||
| 147 | =cut | ||
| 148 | |||
| 149 | # share these among all mod_perl children and from | ||
| 150 | # request to request | ||
| 151 | use vars qw( $Playlist $Controller %Commands %Set $Volume ); | ||
| 152 | |||
| 153 | $Playlist = 'Library'; | ||
| 154 | $Controller = Mac::iTunes->new()->controller; | ||
| 155 | %Commands = map { $_, 1 } | ||
| 156 | qw( play stop pause back_track next previous ); | ||
| 157 | %Set = map { $_, 1 } | ||
| 158 | qw( playlist ); | ||
| 159 | $Volume = $Controller->volume; | ||
| 160 | |||
| 161 | sub handler | ||
| 162 | { | ||
| 163 | my $r = shift; | ||
| 164 | |||
| 165 | my( undef, $command, @path_info )= split m|/|, ( $r->path_info || '' ); | ||
| 166 | $command = '' unless $command; # silence warning | ||
| 167 | @path_info = map { unescape_uri( $_ ) } @path_info; | ||
| 168 | |||
| 169 | my %params = $r->args; | ||
| 170 | |||
| 171 | if( exists $Commands{ $command } ) | ||
| 172 | { | ||
| 173 | $Controller->$command; | ||
| 174 | } | ||
| 175 | elsif( $command eq 'playlist' and defined $path_info[0] | ||
| 176 | and $Controller->playlist_exists( $path_info[0] ) ) | ||
| 177 | { | ||
| 178 | $Controller->set_playlist( $path_info[0] ); | ||
| 179 | $Playlist = $path_info[0]; | ||
| 180 | } | ||
| 181 | elsif( $command eq 'track' ) | ||
| 182 | { | ||
| 183 | my $number = int( $path_info[0] || 0 ); | ||
| 184 | $path_info[1] = $Playlist unless $path_info[1]; | ||
| 185 | my $Playlist = $path_info[1] | ||
| 186 | if $Controller->playlist_exists( $path_info[1] ); | ||
| 187 | $Controller->play_track( $number, $Playlist ); | ||
| 188 | } | ||
| 189 | elsif( $command eq 'volume' ) | ||
| 190 | { | ||
| 191 | my $volume = $path_info[0]; | ||
| 192 | $volume = $volume > 100 ? 100 : $volume < 0 ? 0 : $volume; | ||
| 193 | $Volume = $Controller->volume( $volume ); | ||
| 194 | } | ||
| 195 | |||
| 196 | my %var; | ||
| 197 | |||
| 198 | $var{version} = $VERSION; | ||
| 199 | $var{base} = $ENV{APACHE_ITUNES_URL}; | ||
| 200 | $var{state} = $Controller->player_state; | ||
| 201 | $var{current} = $Controller->current_track_name; | ||
| 202 | $var{playlist} = $Playlist; | ||
| 203 | $var{playlists} = $Controller->get_playlists; | ||
| 204 | $var{tracks} = $Controller->get_track_names_in_playlist( $Playlist ); | ||
| 205 | |||
| 206 | my $html = Text::Template::fill_in_file( | ||
| 207 | $ENV{APACHE_ITUNES_HTML}, HASH => \%var ); | ||
| 208 | |||
| 209 | $r->content_type( 'text/html' ); | ||
| 210 | $r->send_http_header; | ||
| 211 | $r->print( $html ); | ||
| 212 | return OK; | ||
| 213 | } | ||
| 214 | |||
| 215 | "See why 1984 won't be like 1984"; | ||