@@ -41,6 +41,7 @@ sub new {
4141
4242 $log -> info( ' Remote streaming Times Radio : ' . $streamUrl . ' actual url' . $song -> track()-> url);
4343
44+ my $isLive = (getType($song -> track()-> url) eq ' live' );
4445
4546 my $sock = $class -> SUPER::new(
4647 {
@@ -55,7 +56,7 @@ sub new {
5556 ${*$sock }{' client' } = $args -> {' client' };
5657 ${*$sock }{' vars' } = {
5758 ' metaDataCheck' => time (),
58- ' isLive' => (getType( $song -> track() -> url) eq ' live ' ) ,
59+ ' isLive' => $isLive ,
5960 };
6061
6162 return $sock ;
@@ -83,31 +84,62 @@ sub readMetaData {
8384 Plugins::TimesRadio::TimesRadioAPI::getOnAir(
8485 sub {
8586 my $json = shift ;
87+ main::DEBUGLOG && $log -> is_debug && $log -> debug(' on Air : ' . Dumper($json -> {' data' }-> {' radioOnAirNow' }));
88+ my $duration = str2time( $json -> {' data' }-> {' radioOnAirNow' }-> {' endTime' }) - str2time( $json -> {' data' }-> {' radioOnAirNow' }-> {' startTime' });
89+
90+ my $image ;
91+ if (scalar @{$json -> {' data' }-> {' radioOnAirNow' }-> {' images' }}) {
92+ my @thumbnails = grep { $_ -> {' width' } == 720 && $_ -> {' metadata' }[0] eq ' thumbnail' } @{$json -> {' data' }-> {' radioOnAirNow' }-> {' images' }};
93+ $image = $thumbnails [0]-> {' url' };
94+ }
95+
8696 my $meta = {
8797 type => ' MP3 (Times Radio)' ,
8898 title => $json -> {' data' }-> {' radioOnAirNow' }-> {' title' },
8999 artist => $json -> {' data' }-> {' radioOnAirNow' }-> {' description' },
90- icon => $json -> {' data' }-> {' radioOnAirNow' }-> {' images' }[0]-> {' url' },
91- cover => $json -> {' data' }-> {' radioOnAirNow' }-> {' images' }[0]-> {' url' },
100+ icon => $image ,
101+ cover => $image ,
102+ duration => $duration ,
103+ secs => $duration ,
92104 };
93-
105+ $song -> duration( $duration );
94106 # place on the song
95107 $song -> pluginData( meta => $meta );
96108
97109 # when do we need to check again
98- $v -> {' metaDataCheck' } = str2time( $json -> {' data' }-> {' radioOnAirNow' }-> {' endTime' });
110+ $v -> {' metaDataCheck' } = str2time( $json -> {' data' }-> {' radioOnAirNow' }-> {' endTime' }) + 5 ;
99111
100112 # protection for their api
101113
102- if ($v -> {' metaDataCheck' } < (time () + 180 )) {
103- $v -> {' metaDataCheck' } = time () + 180 ;
114+ if ($v -> {' metaDataCheck' } < (time () + 30 )) {
115+ $v -> {' metaDataCheck' } = time () + 30 ;
104116 }
105117
106118 main::INFOLOG && $log -> is_info && $log -> info(' We will check again ' . $v -> {' metaDataCheck' } );
107119
108120
109121 my $client = ${*$self }{' client' };
110-
122+ my $offset = time () - str2time( $json -> {' data' }-> {' radioOnAirNow' }-> {' startTime' } );
123+
124+ main::INFOLOG && $log -> is_info && $log -> info(" Offset is $offset from " . time ());
125+
126+ my $position_in_seconds = $client -> songElapsedSeconds();
127+
128+
129+ # fix progress bar
130+ $client -> playingSong()-> can(' startOffset' )
131+ ? $client -> playingSong()-> startOffset( $offset - $position_in_seconds )
132+ : ( $client -> playingSong()-> {startOffset } = ($offset - $position_in_seconds ) );
133+
134+ $client -> master()-> remoteStreamStartTime( Time::HiRes::time () - $offset );
135+
136+ $client -> playingSong()-> duration( $duration );
137+ $song -> track-> secs( $duration );
138+
139+ Slim::Music::Info::setCurrentTitle( Slim::Player::Playlist::url($client ), $json -> {' data' }-> {' radioOnAirNow' }-> {' title' }, $client );
140+ Slim::Control::Request::notifyFromArray( $client , [' newmetadata' ] );
141+
142+
111143 Slim::Control::Request::notifyFromArray( $client , [' newmetadata' ] );
112144
113145 main::INFOLOG && $log -> is_info && $log -> info(' meta data update' );
@@ -288,9 +320,6 @@ sub getMetadataFor {
288320
289321 if (my $meta = $song -> pluginData(' meta' )) {
290322
291- # if live, the only place it will be is on the song
292- main::DEBUGLOG && $log -> is_debug && $log -> debug(" meta from song" );
293-
294323 return $meta ;
295324 }
296325
@@ -329,7 +358,12 @@ sub getMetadataFor {
329358
330359 my $title = $item -> {title } . ' ' . substr ($item -> {startTime },0,10);
331360 my $description = $item -> {description };
332- my $image = $item -> {images }[0]-> {url };
361+
362+ my $image ;
363+ if (scalar @{$item -> {images }}) {
364+ my @thumbnails = grep { $_ -> {' width' } == 720 && $_ -> {' metadata' }[0] eq ' thumbnail' } @{$item -> {images }};
365+ $image = $thumbnails [0]-> {' url' };
366+ }
333367
334368 my $duration = str2time($item -> {endTime }) - str2time($item -> {startTime });
335369 if (!(defined $image )) {
0 commit comments