WatchLater.pm

filter: Statementstmt 44.9% 31 / 69 Branchbran 0.0% 0 / 20 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 50.0% 10 / 20 Podpod 100% 1 / 1 totaltotal 36.5% 42 / 115 CC 14 SCAR 22.4
CC
14
Cov
34%
main
30.0
_add
12.0
_get_random_video
12.0
CRAP 9.4
« WatchLater/Browser.pm ↑ Summary WatchLater/YouTube.pm »
1 package App::WatchLater;
2  
3 1 ▶use 5.016;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use DBI;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Getopt::Long qw(:config auto_help gnu_getopt);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Pod::Usage;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 1 ▶use App::WatchLater::YouTube;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use App::WatchLater::Browser;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 =head1 NAME
17  
18 App::WatchLater - Manage your YouTube Watch Later videos
19  
20 =head1 VERSION
21  
22 Version 0.03
23  
24 =cut
25  
26 1 our $VERSION = '0.03';
27  
28  
29 =head1 SYNOPSIS
30  
31 exit App::WatchLater::main();
32  
33 =head1 DESCRIPTION
34  
35 Manages a Watch Later queue of YouTube videos, in case you're one of the kinds
36 of people whose Watch Later lists get too out of hand. Google has deprecated the
37 ability to access the B<WL> playlist via the YouTube Data API, which means we
38 have to go to a bit more effort.
39  
40 An API key is required to access the YouTube Data API. Alternatively, requests
41 may be authorized by providing an OAuth2 access token.
42  
43 =head1 SUBROUTINES/METHODS
44  
45 =cut
46  
47 0 ▶sub _ensure_schema {
Subroutine _ensure_schema: not called CC=2 SCAR=17.9
48 0 my $dbh = shift;
49 0 ▶ $dbh->do(<<'SQL') or die $dbh->errstr;
Branch: 1 branch
BranchTrueFalseTotal
$dbh->do("CREATE TABLE IF NOT EXISTS videos(\n video_id TEXT PRIMARY KEY,\n video_title TEXT,\n channel_id TEXT,\n channel_title TEXT,\n watched INTEGER NOT NULL DEFAULT 0\n);\n") or die $dbh->errstr 0 0 0
50 CREATE TABLE IF NOT EXISTS videos(
51 video_id TEXT PRIMARY KEY,
52 video_title TEXT,
53 channel_id TEXT,
54 channel_title TEXT,
55 watched INTEGER NOT NULL DEFAULT 0
56 );
57 SQL
58 }
59  
60 0 ▶sub _add {
Subroutine _add: not called CC=3 SCAR=24.8
61 0 my ($dbh, $api, $opts, @video_ids) = @_;
62  
63 0 ▶ my $on_conflict = $opts->{force} ? 'REPLACE' : 'IGNORE';
Branch: 1 branch
BranchTrueFalseTotal
$opts->{'force'} ? : 0 0 0
64  
65 0 my $sth = $dbh->prepare_cached(<<SQL);
66 INSERT OR $on_conflict INTO videos
67 (video_id, video_title, channel_id, channel_title, watched)
68 VALUES (?, ?, ?, ?, 0);
69 SQL
70  
71 0 for my $vid (@video_ids) {
72 0 ▶ try {
Subroutine __ANON__: not called CC=1 SCAR=6.9
73 0 my $snippet = $api->get_video($vid);
74 $sth->execute($vid, $snippet->{title},
75 0 $snippet->{channelId}, $snippet->{channelTitle});
76 0 ▶ } catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
77 # warn the user and continue
78 0 print STDERR;
79 0 };
80 }
81 }
82  
83 0 ▶sub _get_random_video {
Subroutine _get_random_video: not called CC=3 SCAR=24.8
84 0 my ($dbh) = @_;
85 0 my $sth = $dbh->prepare_cached(<<'SQL');
86 SELECT video_id, video_title, channel_id, channel_title FROM videos
87 WHERE NOT watched
88 ORDER BY RANDOM()
89 LIMIT 1;
90 SQL
91 0 ▶ $sth->execute or die $sth->errstr;
Branch: 1 branch
BranchTrueFalseTotal
$sth->execute or die $sth->errstr 0 0 0
92 0 ▶ my $row = $sth->fetchrow_hashref or croak 'no videos';
Branch: 1 branch
BranchTrueFalseTotal
my $row = $sth->fetchrow_hashref or croak("no videos") 0 0 0
93 0 $row->{video_id};
94 }
95  
96 0 ▶sub _mark_watched {
Subroutine _mark_watched: not called CC=2 SCAR=17.9
97 0 my ($dbh, $vid) = @_;
98 0 my $sth = $dbh->prepare(<<'SQL');
99 UPDATE videos SET watched=1
100 WHERE video_id = ?;
101 SQL
102 0 ▶ $sth->execute($vid) or die $sth->errstr;
Branch: 1 branch
BranchTrueFalseTotal
$sth->execute($vid) or die $sth->errstr 0 0 0
103 }
104  
105 0 ▶sub _watch {
Subroutine _watch: not called CC=3 SCAR=24.8
106 0 my ($dbh, $api, $opts, @video_ids) = @_;
107  
108 0 ▶ if (!@video_ids) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@video_ids) 0 0 0
109 0 push @video_ids, _get_random_video($dbh);
110 }
111  
112 0 for my $vid (@video_ids) {
113 0 ▶ try {
Subroutine __ANON__: not called CC=2 SCAR=17.9
114 0 ▶ open_url("https://youtu.be/$vid") if $opts->{open};
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"open"} 0 0 0
115 0 _mark_watched($dbh, $vid);
116 0 ▶ } catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
117 # warn the user and continue
118 0 print STDERR;
119 0 };
120 }
121 }
122  
123 =head2 main
124  
125 main();
126  
127 C<main()> runs the watch-later command line interface. It reads arguments
128 directly from C<@ARGV> using L<Getopt::Long>.
129  
130 =cut
131  
132 # TODO a better module interface to main()
133 0 ▶sub main {
Subroutine main: not called CC=5 SCAR=34.0
Pod documented
134 0 my %opts = (
135 'db-path' => "$ENV{HOME}/.watch-later.db",
136 force => 0,
137 open => 1,
138 );
139  
140 0 ▶ GetOptions(
Branch: 1 branch
BranchTrueFalseTotal
&GetOptions(\%opts, "db-path|d=s", "add|a", "watch|w", "force|f!", "open|o!") or pod2usage(2) 0 0 0
141 \%opts,
142 'db-path|d=s',
143 'add|a',
144 'watch|w',
145 'force|f!',
146 'open|o!',
147 ) or pod2usage(2);
148  
149 0 ▶ croak "Add and Watch modes both specified" if $opts{add} && $opts{watch};
Branch: 1 branch
BranchTrueFalseTotal
if $opts{"add"} and $opts{"watch"} 0 0 0
Condition0%0/3
$opts{"add"} and $opts{"watch"}
!ll&&!rl&&r
000
MC/DC0%0/2
$opts{"add"} and $opts{"watch"}
$opts{"add"} $opts{"watch"}
Truth table0%0/3
A and B
A= $opts{"add"}B= $opts{"watch"}
ABresult
0X0
100
111
150  
151 0 my @video_ids = map { find_video_id($_) } @ARGV;
152  
153 0 my $dbpath = $opts{'db-path'};
154 0 my $dbh = DBI->connect("dbi:SQLite:dbname=$dbpath");
155 0 _ensure_schema($dbh);
156  
157 my $api = App::WatchLater::YouTube->new(
158 api_key => $ENV{YT_API_KEY},
159 access_token => $ENV{YT_ACCESS_TOKEN},
160 0 );
161  
162 0 ▶ if ($opts{watch}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{'watch'}) { } 0 0 0
163 0 _watch($dbh, $api, \%opts, @video_ids);
164 } else {
165 0 _add($dbh, $api, \%opts, @video_ids);
166 }
167 }
168  
169 =head1 AUTHOR
170  
171 Aaron L. Zeng, C<< <me at bcc32.com> >>
172  
173 =head1 BUGS
174  
175 Please report any bugs or feature requests to C<bug-app-watchlater at rt.cpan.org>, or through
176 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=App-WatchLater>. I will be notified, and then you'll
177 automatically be notified of progress on your bug as I make changes.
178  
179  
180  
181  
182 =head1 SUPPORT
183  
184 You can find documentation for this module with the perldoc command.
185  
186 perldoc App::WatchLater
187  
188  
189 You can also look for information at:
190  
191 =over 4
192  
193 =item * RT: CPAN's request tracker (report bugs here)
194  
195 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=App-WatchLater>
196  
197 =item * AnnoCPAN: Annotated CPAN documentation
198  
199 L<http://annocpan.org/dist/App-WatchLater>
200  
201 =item * CPAN Ratings
202  
203 L<http://cpanratings.perl.org/d/App-WatchLater>
204  
205 =item * Search CPAN
206  
207 L<http://search.cpan.org/dist/App-WatchLater/>
208  
209 =back
210  
211  
212 =head1 ACKNOWLEDGEMENTS
213  
214  
215 =head1 LICENSE AND COPYRIGHT
216  
217 Copyright 2017 Aaron L. Zeng.
218  
219 This program is distributed under the MIT (X11) License:
220 L<http://www.opensource.org/licenses/mit-license.php>
221  
222 Permission is hereby granted, free of charge, to any person
223 obtaining a copy of this software and associated documentation
224 files (the "Software"), to deal in the Software without
225 restriction, including without limitation the rights to use,
226 copy, modify, merge, publish, distribute, sublicense, and/or sell
227 copies of the Software, and to permit persons to whom the
228 Software is furnished to do so, subject to the following
229 conditions:
230  
231 The above copyright notice and this permission notice shall be
232 included in all copies or substantial portions of the Software.
233  
234 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
235 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
236 OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
237 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
238 HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
239 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
240 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
241 OTHER DEALINGS IN THE SOFTWARE.
242  
243  
244 =cut
245  
246 1 1; # End of App::WatchLater
« WatchLater/Browser.pm ↑ Summary WatchLater/YouTube.pm »