package ZTN::Radio;

use warnings;
use strict;
use Carp qw(croak cluck);
use LWP::UserAgent;

=head1 NAME

ZTN::Radio - Interface for ZTNRadio

=head1 VERSION

Version 1.00

=cut

our $VERSION = '1.00';

sub new {
	my $class = shift;
	my $self  = bless { @_ }, $class;
	$self->{ua} = new LWP::UserAgent;
	if ($self->{ua}) {
		$self->{ua}->agent('Mozilla');
		$self->{ua}->timeout('4');
	}
	return $self;
}

=head1 SYNOPSIS

This module provides a sane perl interface for ZTNRadio

Example:

    use ZTN::Radio;

    my $radio = ZTN::Radio->new();
    ...

=head1 EXPORT

A list of functions that can be exported.  You can delete this section
if you don't export anything, such as for a purely object-oriented module.

=head1 SUBROUTINES/METHODS

=head2 status

Retrieves the current status of the stream.

=cut

sub status {
	my $self = shift;
	my $rep = $self->{ua}->get("http://hd1.ztnradio.com:8000/7.html");
	if ($rep->is_success) {
		my $data = $rep->content;
		$data =~ s/<(\/|!)?[-.a-zA-Z0-9]*.*?>//g;
		my @ca = split(',', $data);
		my %hash = (
			return => 1,
			listeners => $ca[0],
			status => $ca[1],
			peak => $ca[2],
			max => $ca[3],
			channels => $ca[4],
			bitrate => $ca[5],
			np => $ca[6],
		);
		return %hash;
	}
	else {
		my %hash = (
			return => 0,
			error => $rep->status_line,
		);
		return %hash;
	}
}

=head2 listeners

Retrieves the current number of listeners

=cut

sub listeners {
	my $self = shift;
	my $rep = $self->{ua}->get("http://radio.ztecnet.net:8000/7.html");
	if ($rep->is_success) {
		my $data = $rep->content;
		$data =~ s/<(\/|!)?[-.a-zA-Z0-9]*.*?>//g;
		my @ca = split(',', $data);
		return $ca[1];
	}
	else {
		return 0
	}
}

=head2 request

Requests a song.

=cut

sub request {
	my ($self, $id) = @_;
	my $rep = $self->{ua}->get("http://ztnradio.com/include_req.php?id=".$id);
	my $data = $rep->content;
	$data =~ s/[\n\r\t]+//g;
	$data =~ s/<script[^>]*>[^<]*<\/script>//gs;
	$data =~ s/<(\/|!)?[-.a-zA-Z0-9]*.*?>//g;
	if ($rep->is_success && $data eq "Request approved!") {
		return 'REQUEST_APPROVED';
	}
	else {
		return $data;
	}
}

=head2 np

Retrieves the current song playing

=cut

sub np {
	my $self = shift;
		my $rep = $self->{ua}->get("http://radio.ztecnet.net:8000/7.html");
	if ($rep->is_success) {
		my $data = $rep->content;
		$data =~ s/<(\/|!)?[-.a-zA-Z0-9]*.*?>//g;
		my @ca = split(',', $data);
		return $ca[6];
	}
	else {
		return 0;
	}
}

=head1 AUTHOR

Matthew Barksdale, C<< <matthew at ztecnet.net> >>

=head1 BUGS

Please report any bugs or feature requests to C<bug-ztn-radio at rt.cpan.org>, or through
the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=ZTN-Radio>.  I will be notified, and then you'll
automatically be notified of progress on your bug as I make changes.




=head1 SUPPORT

You can find documentation for this module with the perldoc command.

    perldoc ZTN::Radio


You can also look for information at:

=over 4

=item * RT: CPAN's request tracker

L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=ZTN-Radio>

=item * AnnoCPAN: Annotated CPAN documentation

L<http://annocpan.org/dist/ZTN-Radio>

=item * CPAN Ratings

L<http://cpanratings.perl.org/d/ZTN-Radio>

=item * Search CPAN

L<http://search.cpan.org/dist/ZTN-Radio/>

=back


=head1 ACKNOWLEDGEMENTS


=head1 LICENSE AND COPYRIGHT

Copyright 2010 Matthew Barksdale.

This program is free software; you can redistribute it and/or modify it
under the terms of either: the GNU General Public License as published
by the Free Software Foundation; or the Artistic License.

See http://dev.perl.org/licenses/ for more information.


=cut

1; # End of ZTN::Radio
