Skip to content
This repository has been archived by the owner on Nov 3, 2021. It is now read-only.

Commit

Permalink
- Implemented sort_by and date parameters for /search. (https:/…
Browse files Browse the repository at this point in the history
…/github.com/iv-org/invidious/wiki/API#get-apiv1search)

In the CLI version, we have:

	--order=s	# valid values: relevance rating upload_date view_count
	--date=s	# valid values: hour today week month year

In the GTK3 version, this is implemented in the "Order by" and "Published within" comboboxes.

Also implemented the `--hd`, `--captions` and `--region=s` options.
  • Loading branch information
trizen committed Sep 18, 2020
1 parent 157e118 commit 00f98d9
Show file tree
Hide file tree
Showing 8 changed files with 417 additions and 483 deletions.
361 changes: 169 additions & 192 deletions bin/gtk-straw-viewer

Large diffs are not rendered by default.

288 changes: 135 additions & 153 deletions bin/straw-viewer

Large diffs are not rendered by default.

48 changes: 23 additions & 25 deletions lib/WWW/StrawViewer.pm
Original file line number Diff line number Diff line change
Expand Up @@ -46,34 +46,33 @@ our $VERSION = '0.0.9';
my %valid_options = (

# Main options
v => {valid => q[], default => 3},
page => {valid => qr/^(?!0+\z)\d+\z/, default => 1},
http_proxy => {valid => qr/./, default => undef},
hl => {valid => qr/^\w+(?:[\-_]\w+)?\z/, default => undef},
maxResults => {valid => [1 .. 50], default => 10},
topicId => {valid => qr/./, default => undef},
order => {valid => [qw(relevance date rating viewCount title videoCount)], default => undef},
publishedAfter => {valid => qr/^\d+/, default => undef},
publishedBefore => {valid => qr/^\d+/, default => undef},
channelId => {valid => qr/^[-\w]{2,}\z/, default => undef},
channelType => {valid => [qw(any show)], default => undef},
v => {valid => q[], default => 3},
page => {valid => qr/^(?!0+\z)\d+\z/, default => 1},
http_proxy => {valid => qr/./, default => undef},
hl => {valid => qr/^\w+(?:[\-_]\w+)?\z/, default => undef},
maxResults => {valid => [1 .. 50], default => 10},
order => {valid => [qw(relevance rating upload_date view_count)], default => undef},
date => {valid => [qw(hour today week month year)], default => undef},

channelId => {valid => qr/^[-\w]{2,}\z/, default => undef},
channelType => {valid => [qw(any show)], default => undef},

# Video only options
videoCaption => {valid => [qw(any closedCaption none)], default => undef},
videoDefinition => {valid => [qw(any high standard)], default => undef},
videoCategoryId => {valid => qr/^\d+\z/, default => undef},
videoDimension => {valid => [qw(any 2d 3d)], default => undef},
videoDuration => {valid => [qw(any short medium long)], default => undef},
videoEmbeddable => {valid => [qw(any true)], default => undef},
videoLicense => {valid => [qw(any creativeCommon youtube)], default => undef},
videoSyndicated => {valid => [qw(any true)], default => undef},
eventType => {valid => [qw(completed live upcoming)], default => undef},
chart => {valid => [qw(mostPopular)], default => 'mostPopular'},

regionCode => {valid => qr/^[A-Z]{2}\z/i, default => undef},
videoCaption => {valid => [qw(1 true)], default => undef},
videoDefinition => {valid => [qw(high standard)], default => undef},
videoCategoryId => {valid => qr/^\d+\z/, default => undef},
videoDimension => {valid => [qw(2d 3d)], default => undef},
videoDuration => {valid => [qw(short long)], default => undef},
videoEmbeddable => {valid => [qw(true)], default => undef},
videoLicense => {valid => [qw(creative_commons)], default => undef},
videoSyndicated => {valid => [qw(true)], default => undef},
eventType => {valid => [qw(completed live upcoming)], default => undef},
chart => {valid => [qw(mostPopular)], default => 'mostPopular'},

region => {valid => qr/^[A-Z]{2}\z/i, default => undef},
relevanceLanguage => {valid => qr/^[a-z]+(?:\-\w+)?\z/i, default => undef},
safeSearch => {valid => [qw(none moderate strict)], default => undef},
videoType => {valid => [qw(any episode movie)], default => undef},
videoType => {valid => [qw(episode movie)], default => undef},

comments_order => {valid => [qw(top new)], default => 'top'},
subscriptions_order => {valid => [qw(alphabetical relevance unread)], default => undef},
Expand Down Expand Up @@ -669,7 +668,6 @@ sub default_arguments {
#part => 'snippet',
#prettyPrint => 'false',
#maxResults => $self->get_maxResults,
#regionCode => $self->get_regionCode,
%args,
);

Expand Down
4 changes: 2 additions & 2 deletions lib/WWW/StrawViewer/GuideCategories.pm
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ sub _make_guideCategories_url {
my ($self, %opts) = @_;

if (not exists $opts{id}) {
$opts{regionCode} //= $self->get_regionCode;
$opts{region} //= $self->get_region;
}

$self->_make_feed_url('guideCategories', hl => $self->get_hl, %opts);
Expand All @@ -47,7 +47,7 @@ Return info for a list of comma-separated category IDs.
name => 'guide_categories_info',
},
{
key => 'regionCode',
key => 'region',
name => 'guide_categories',
},
) {
Expand Down
149 changes: 78 additions & 71 deletions lib/WWW/StrawViewer/Search.pm
Original file line number Diff line number Diff line change
Expand Up @@ -21,43 +21,46 @@ WWW::StrawViewer::Search - Search functions for Straw API v3
sub _make_search_url {
my ($self, %opts) = @_;

my @features;

if (defined(my $vd = $self->get_videoDefinition)) {
if ($vd eq 'high') {
push @features, 'hd';
}
}

if (defined(my $vc = $self->get_videoCaption)) {
if ($vc eq 'true' or $vc eq '1') {
push @features, 'subtitles';
}
}

if (defined(my $vd = $self->get_videoDimension)) {
if ($vd eq '3d') {
push @features, '3d';
}
}

if (defined(my $license = $self->get_videoLicense)) {
if ($license eq 'creative_commons') {
push @features, 'creative_commons';
}
}

return $self->_make_feed_url(
'search',

topicId => $self->get_topicId,
regionCode => $self->get_regionCode,

maxResults => $self->get_maxResults,
order => $self->get_order,
publishedAfter => $self->get_publishedAfter,
publishedBefore => $self->get_publishedBefore,
regionCode => $self->get_regionCode,
relevanceLanguage => $self->get_relevanceLanguage,
safeSearch => $self->get_safeSearch,
channelId => $self->get_channelId,
channelType => $self->get_channelType,
pageToken => $self->page_token,

(
$opts{type} eq 'video'
? (
videoCaption => $self->get_videoCaption,
videoCategoryId => $self->get_videoCategoryId,
videoDefinition => $self->get_videoDefinition,
videoDimension => $self->get_videoDimension,
videoDuration => $self->get_videoDuration,
videoEmbeddable => $self->get_videoEmbeddable,
videoLicense => $self->get_videoLicense,
videoSyndicated => $self->get_videoSyndicated,
videoType => $self->get_videoType,
eventType => $self->get_eventType,
)
: ()
),
region => $self->get_region,
sort_by => $self->get_order,
date => $self->get_date,
channelId => $self->get_channelId,
pageToken => $self->page_token,
duration => $self->get_videoDuration,

(@features ? (features => join(',', @features)) : ()),

%opts,
);

}

=head2 search_for($types,$keywords;\%args)
Expand Down Expand Up @@ -146,59 +149,61 @@ be set to a YouTube video ID.
sub related_to_videoID {
my ($self, $videoID) = @_;

my %info = $self->_get_video_info($videoID);
my %info = $self->_get_video_info($videoID);
my $watch_next_response = $self->parse_json_string($info{watch_next_response});
my $related = eval { $watch_next_response->{contents}{twoColumnWatchNextResults}{secondaryResults}{secondaryResults}{results} } // return { results => []};
my $related =
eval { $watch_next_response->{contents}{twoColumnWatchNextResults}{secondaryResults}{secondaryResults}{results} }
// return {results => []};

#use Data::Dump qw(pp);
#pp $related;

my @results;

foreach my $entry(@$related) {
foreach my $entry (@$related) {

my $info = $entry->{compactVideoRenderer} // next;
my $title = $info->{title}{simpleText} // next;
my $info = $entry->{compactVideoRenderer} // next;
my $title = $info->{title}{simpleText} // next;

my $viewCount = 0;

if ($info->{viewCountText}{simpleText} =~ /^([\d,]+) views/) {
$viewCount = ($1 =~ tr/,//dr);
}
elsif ($info->{viewCountText}{simpleText} =~ /Recommended for you/i) {
next; # filter out recommended videos from related videos
next; # filter out recommended videos from related videos
}

my $lengthSeconds = 0;

if ($info->{lengthText}{simpleText} =~ /([\d:]+)/) {
my $time = $1;
my $time = $1;
my @fields = split(/:/, $time);

my $seconds = pop(@fields) // 0;
my $minutes = pop(@fields) // 0;
my $hours = pop(@fields) // 0;
my $hours = pop(@fields) // 0;

$lengthSeconds = 3600 * $hours + 60*$minutes + $seconds;
$lengthSeconds = 3600 * $hours + 60 * $minutes + $seconds;
}

my $published = 0;
if (exists $info->{publishedTimeText} and $info->{publishedTimeText}{simpleText} =~ /(\d+)\s+(\w+)\s+ago/) {

my $quantity = $1;
my $period = $2;
my $period = $2;

$period =~ s/s\z//; # make it singural
$period =~ s/s\z//; # make it singural

my %table = (
year => 31556952, # seconds in a year
month => 2629743.83, # seconds in a month
week => 604800, # seconds in a week
day => 86400, # seconds in a day
hour => 3600, # seconds in a hour
minute => 60, # seconds in a minute
second => 1, # seconds in a second
);
year => 31556952, # seconds in a year
month => 2629743.83, # seconds in a month
week => 604800, # seconds in a week
day => 86400, # seconds in a day
hour => 3600, # seconds in a hour
minute => 60, # seconds in a minute
second => 1, # seconds in a second
);

if (exists $table{$period}) {
$published = int(time - $quantity * $table{$period});
Expand All @@ -209,40 +214,42 @@ sub related_to_videoID {
}

push @results, {
type => "video",
title => $title,
videoId => $info->{videoId},
author => $info->{longBylineText}{runs}[0]{text},
type => "video",
title => $title,
videoId => $info->{videoId},
author => $info->{longBylineText}{runs}[0]{text},
authorId => $info->{longBylineText}{runs}[0]{navigationEndpoint}{browseEndpoint}{browseId},

#authorUrl => $info->{longBylineText}{runs}[0]{navigationEndpoint}{browseEndpoint}{browseId},

description => $info->{accessibility}{accessibilityData}{label},
description => $info->{accessibility}{accessibilityData}{label},
descriptionHtml => undef,
viewCount => $viewCount,
published => $published,
publishedText => $info->{publishedTimeText}{simpleText},
lengthSeconds => $lengthSeconds,
liveNow => ($lengthSeconds == 0), # maybe it's live if lengthSeconds == 0?
paid => 0,
premium => 0,
viewCount => $viewCount,
published => $published,
publishedText => $info->{publishedTimeText}{simpleText},
lengthSeconds => $lengthSeconds,
liveNow => ($lengthSeconds == 0), # maybe it's live if lengthSeconds == 0?
paid => 0,
premium => 0,

videoThumbnails => [
map {
scalar {
quality => 'medium',
url => $_->{url},
width => $_->{width},
height => $_->{height},
}
quality => 'medium',
url => $_->{url},
width => $_->{width},
height => $_->{height},
}
} @{$info->{thumbnail}{thumbnails}}
],
};
}

return scalar {
url => undef,
results => \@results,
};
return
scalar {
url => undef,
results => \@results,
};
}

=head1 AUTHOR
Expand Down
4 changes: 2 additions & 2 deletions lib/WWW/StrawViewer/VideoCategories.pm
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ sub video_categories {

require File::Spec;

my $region = $self->get_regionCode() // 'US';
my $url = $self->_make_videoCategories_url(regionCode => $region);
my $region = $self->get_region() // 'US';
my $url = $self->_make_videoCategories_url(region => $region);
my $file = File::Spec->catfile($self->get_config_dir, "categories-$region-" . $self->get_hl() . ".json");

my $json;
Expand Down
5 changes: 0 additions & 5 deletions lib/WWW/StrawViewer/Videos.pm
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,7 @@ sub trending_videos_from_category {
my ($self, $cat_id) = @_;

my $results = do {
local $self->{publishedAfter} = do {
state $yv_utils = WWW::StrawViewer::Utils->new;
$yv_utils->period_to_date(1, 'w');
} if !defined($self->get_publishedAfter);
local $self->{videoCategoryId} = $cat_id;
local $self->{regionCode} = "US" if !defined($self->get_regionCode);
$self->search_videos("");
};

Expand Down
Loading

0 comments on commit 00f98d9

Please sign in to comment.