summaryrefslogtreecommitdiffstats
path: root/frikanalen/bin/frikanalen-filler-playlist
blob: 6a14c4c2f75cf38bc118171c22317e9476ed4bdb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#!/usr/bin/perl
#
# Extract public videos, remove those requiring tono fee and long
# religious videos.  Used to create a evening playout playlist.

use strict;
use warnings;
use vars qw(%opts);
use Data::Dumper;
use JSON;
use LWP::Simple;

my $starturl = "http://beta.frikanalen.tv/ws/videos/";
my @videos = get_relevant_videos($starturl);
print sort join("\n", @videos), "\n";
exit(0);

sub get_relevant_videos {
    my $url = shift;
    my $jsonstr = get($url);
    my $json = decode_json( $jsonstr );
    my @videos;
    unless ($json->{'results'}) {
        return;
    }
    foreach my $video (@{$json->{'results'}}) {
        my $durationsec = parse_duration($video->{duration});
        # Skip religious stuff that is longer than 10 minutes
        next if (grep(m%Religion/livssyn%, @{$video->{'categories'}})
                 && $durationsec >= 10 * 60);
        # Skip stuff triggering TONO fee
        next if ($video->{'has_tono_records'});

#        print Dumper($video);
        my $entry = join(";",
                         $video->{id},
                         $durationsec,
                         $video->{name},
                         $video->{organization} || '',
                         join(":", @{$video->{'categories'}}));
        push(@videos, $entry);
    }
    if (defined $json->{'next'}) {
        push(@videos, get_relevant_videos($json->{'next'}));
    }
    return @videos;
}

# Convert "04:05.12" to 4 * 60 + 5.12
sub parse_duration {
    my $durationstr = shift;
    my @parts = split(/:/, $durationstr);
    my $duration = 0;
    while (my $part = shift @parts) {
        $duration *= 60;
        $duration += int($part);
    }
#    print "$durationstr = $duration\n";
    return $duration;
}