| 1 | # $Id: TiVo.pm 63 2007-03-29 14:09:37Z boumenot $ | ||||||||||
| 2 | # Author: Christopher Boumenot <boumenot@gmail.com> | ||||||||||
| 3 | ###################################################################### | ||||||||||
| 4 | # | ||||||||||
| 5 | # Copyright 2006-2007 by Christopher Boumenot. This program is free | ||||||||||
| 6 | # software; you can redistribute it and/or modify it under the same | ||||||||||
| 7 | # terms as Perl itself. | ||||||||||
| 8 | # | ||||||||||
| 9 | ###################################################################### | ||||||||||
| 10 | |||||||||||
| 11 | package Net::TiVo; | ||||||||||
| 12 | |||||||||||
| 13 | 4 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 14 | 4 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 15 | |||||||||||
| 16 | 4 | our $VERSION = '0.11'; | |||||||||
| 17 | |||||||||||
| 18 | 4 | ▶ | use LWP::UserAgent; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 19 | 4 | ▶ | use HTTP::Request; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 20 | 4 | ▶ | use XML::Simple; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 21 | 4 | ▶ | use Data::Dumper; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 22 | 4 | ▶ | use Log::Log4perl qw(:easy get_logger); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 23 | 4 | ▶ | use Net::TiVo::Folder; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 24 | 4 | ▶ | use Net::TiVo::Show; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 25 | |||||||||||
| 26 | 4 | ▶ | use constant TIVO_URL => '/TiVoConnect?Command=QueryContainer&Container=%2FNowPlaying'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 27 | |||||||||||
| 28 | 3 | ▶ | sub new { | ||||||||
|
Subroutine
new: called CC=5 SCAR=16.6
Pod
undocumented
| |||||||||||
| 29 | 3 | my $class = shift; | |||||||||
| 30 | 3 | my $self = {username => 'tivo', | |||||||||
| 31 | realm => 'TiVo DVR', | ||||||||||
| 32 | @_}; | ||||||||||
| 33 | |||||||||||
| 34 | 3 | ▶ | $self->{host} || die "%Error: no host was defined!\n"; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 35 | 3 | ▶ | $self->{mac} || die "%Error: no mac was defined!\n"; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 36 | 3 | ▶ | $self->{username} || die "%Error: no username was defined!\n"; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 37 | |||||||||||
| 38 | 3 | ▶ | $self->{ua} = LWP::UserAgent->new() or | ||||||||
|
Branch: 1 branch
| |||||||||||
| 39 | die "%Error: failed to create a LWP::UserAgent!"; | ||||||||||
| 40 | |||||||||||
| 41 | $self->{ua}->credentials($self->{host}.':443', | ||||||||||
| 42 | $self->{realm}, | ||||||||||
| 43 | 3 | $self->{username} => $self->{mac}); | |||||||||
| 44 | |||||||||||
| 45 | 3 | $self->{url} = 'https://'.$self->{host}.TIVO_URL; | |||||||||
| 46 | |||||||||||
| 47 | 3 | bless $self, $class; | |||||||||
| 48 | 3 | return $self; | |||||||||
| 49 | } | ||||||||||
| 50 | |||||||||||
| 51 | |||||||||||
| 52 | 0 | ▶ | sub folders { | ||||||||
|
Subroutine
folders: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||
| 53 | 0 | my $self = shift; | |||||||||
| 54 | |||||||||||
| 55 | 0 | my $resp = $self->_fetch($self->{url}); | |||||||||
| 56 | |||||||||||
| 57 | 0 | ▶ | if ($resp->is_success()) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 58 | 0 | my @folders; | |||||||||
| 59 | 0 | $self->_parse_content($resp->content(), \@folders); | |||||||||
| 60 | |||||||||||
| 61 | 0 | ▶ | return unless @folders; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 62 | 0 | ▶ | return (wantarray) ? @folders : \@folders; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 63 | } | ||||||||||
| 64 | |||||||||||
| 65 | 0 | print "%Error: $resp->status_line()!\n"; | |||||||||
| 66 | 0 | return; | |||||||||
| 67 | } | ||||||||||
| 68 | |||||||||||
| 69 | |||||||||||
| 70 | 0 | ▶ | sub _fetch { | ||||||||
|
Subroutine
_fetch: not called CC=5 SCAR=34.0
| |||||||||||
| 71 | 0 | my ($self, $url) = @_; | |||||||||
| 72 | 0 | my $resp; | |||||||||
| 73 | |||||||||||
| 74 | 0 | INFO("fetching $url"); | |||||||||
| 75 | |||||||||||
| 76 | 0 | ▶ | if (exists $self->{cache}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 77 | 0 | $resp = $self->{cache}->get($url, $resp); | |||||||||
| 78 | 0 | ▶ | if (defined $resp) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 79 | 0 | INFO("cache hit"); | |||||||||
| 80 | 0 | return $resp; | |||||||||
| 81 | } | ||||||||||
| 82 | 0 | INFO("cache miss"); | |||||||||
| 83 | } | ||||||||||
| 84 | |||||||||||
| 85 | 0 | $resp = $self->{ua}->request(HTTP::Request->new(GET => $url)); | |||||||||
| 86 | 0 | ▶ | die "%Error: fetch failed, " . $resp->status_line() . "!\n" unless $resp->is_success(); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 87 | |||||||||||
| 88 | 0 | ▶ | if (exists $self->{cache}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 89 | 0 | $self->{cache}->set($url, $resp); | |||||||||
| 90 | } | ||||||||||
| 91 | |||||||||||
| 92 | 0 | return $resp; | |||||||||
| 93 | } | ||||||||||
| 94 | |||||||||||
| 95 | 3 | ▶ | sub _parse_content { | ||||||||
|
Subroutine
_parse_content: called CC=5 SCAR=19.0
| |||||||||||
| 96 | 3 | my ($self, $cont, $folder_aref) = @_; | |||||||||
| 97 | |||||||||||
| 98 | 0 | ▶ | DEBUG(sub { "Received [" . $cont . "]"}); | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||
| 99 | |||||||||||
| 100 | 3 | my $xmlref = XMLin($cont, ForceArray => ['Item']); | |||||||||
| 101 | 3 | ▶ | unless (defined $xmlref->{Item}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 102 | 0 | INFO("No content to parse, skipping ..."); | |||||||||
| 103 | 0 | return; | |||||||||
| 104 | } | ||||||||||
| 105 | |||||||||||
| 106 | 0 | ▶ | DEBUG(sub { Dumper($xmlref) }); | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||
| 107 | |||||||||||
| 108 | # TiVo only allows you to create one folder to hold shows, but the | ||||||||||
| 109 | # top most folder, Now Playing, as to be accounted for too. If we | ||||||||||
| 110 | # haven't created any folders yet, then this is the Now Playing | ||||||||||
| 111 | # folder, and needs to be treated specially. | ||||||||||
| 112 | 3 | push @$folder_aref, Net::TiVo::Folder->new(xmlref => $xmlref); | |||||||||
| 113 | |||||||||||
| 114 | # 2006/12/29 - RHARMAN: TiVo Suggestions can exist but contain zero videos | ||||||||||
| 115 | 3 | ▶ | if ($folder_aref->[-1]->total_items() > 0) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 116 | 3 | INFO("added the folder " . $folder_aref->[-1]->name()); | |||||||||
| 117 | } else { | ||||||||||
| 118 | 0 | INFO("skipped the folder " , $folder_aref->[-1]->name(), " because it was empty."); | |||||||||
| 119 | 0 | pop @$folder_aref; | |||||||||
| 120 | } | ||||||||||
| 121 | |||||||||||
| 122 | 3 | for my $i (@{$xmlref->{Item}}) { | |||||||||
| 123 | 8 | my $ct = $i->{Links}->{Content}->{ContentType}; | |||||||||
| 124 | |||||||||||
| 125 | |||||||||||
| 126 | 8 | ▶ | if ($ct eq 'x-tivo-container/folder') { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 127 | 0 | my $resp = $self->_fetch($i->{Links}->{Content}->{Url}); | |||||||||
| 128 | 0 | $self->_parse_content($resp->content(), $folder_aref); | |||||||||
| 129 | } else { | ||||||||||
| 130 | 8 | INFO("skipping the content for $ct"); | |||||||||
| 131 | } | ||||||||||
| 132 | } | ||||||||||
| 133 | } | ||||||||||
| 134 | |||||||||||
| 135 | |||||||||||
| 136 | 4 | 1; | |||||||||
| 137 | __END__ | ||||||||||