3 #==============================================================================
5 # File ID: 533a9d38-23d9-11e9-ac41-4f45262dc9b5
10 # ©opyleft 2019– Øyvind A. Holm <sunny@sunbase.org>
11 # License: GNU General Public License version 2 or later, see end of file for
13 #==============================================================================
32 $progname =~ s/^.*\/(.*?)$/$1/;
33 our $VERSION = '0.2.2';
35 Getopt
::Long
::Configure
('bundling');
38 'create-url|c' => \
$Opt{'create-url'},
39 'help|h' => \
$Opt{'help'},
40 'quiet|q+' => \
$Opt{'quiet'},
41 'verbose|v+' => \
$Opt{'verbose'},
42 'version' => \
$Opt{'version'},
44 ) || die("$progname: Option error. Use -h for help.\n");
46 $Opt{'verbose'} -= $Opt{'quiet'};
47 $Opt{'help'} && usage
(0);
48 if ($Opt{'version'}) {
59 defined($url) || myexit
(1, "No URL specified");
61 $id = get_id
($Opt{'create-url'}, $url);
68 my ($type, $id, $user) = @_;
70 if ($type eq "youtube") {
71 return "https://www.youtube.com/watch?v=$id";
72 } elsif ($type eq "twitter") {
73 return "https://twitter.com/$user/status/$id";
75 myexit
(1, "Internal error: create_url(): $type: Unknown type");
80 my ($create_url, $url) = @_;
83 msg
(2, "url = \"$url\"");
84 $url =~ s!^https?://!!;
88 if ($url =~ /^youtube\.com\/watch
\?.*\bv
=(.*)/) {
90 msg
(2, "Found youtube.com/watch?v=*");
92 valid_youtube_id
($id) || myexit
(1, "Invalid URL");
93 $create_url && ($id = create_url
("youtube", $id));
94 } elsif ($url =~ /^youtu\.be\/(.*)/) {
96 msg
(2, "Found youtu.be/*");
98 valid_youtube_id
($id) || myexit
(1, "Invalid URL");
99 $create_url && ($id = create_url
("youtube", $id));
100 } elsif ($url =~ /^twitter.com\/([^\
/]+)\/status\
/(\d+)/) {
102 ($user, $id) = ($1, $2);
103 msg
(2, "Found twitter.com/*/status/*");
104 valid_twitter_id
($id) || myexit
(1, "Invalid Twitter ID");
105 $create_url && ($id = create_url
("twitter", $id, $user));
106 } elsif ($url =~ /^google\.com\/url
.*\
&url
=(.+)/) {
108 msg
(2, "Found google.com/url*url=*");
110 $id = get_id
(0, url2txt
($id));
111 length($id) || myexit
(1, "Invalid Google URL");
112 $Opt{'create-url'} && ($id = create_url
("youtube", $id));
113 } elsif (length($url) == 11) {
114 msg
(2, "Found plain id");
115 valid_youtube_id
($url) || myexit
(1, "Invalid Youtube ID");
116 $id = $create_url ? create_url
("youtube", $url) : $url;
118 myexit
(1, "Unknown URL format");
127 $txt =~ s/%([0-9a-f]{2})/chr(hex($1))/gei;
131 sub valid_twitter_id
{
134 ($id =~ /\d/) || return 0;
135 ($id =~ /\D/) && return 0;
140 sub valid_youtube_id
{
142 my $ytchars = join('',
145 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' .
147 'abcdefghijklmnopqrstuvwxyz'
150 if (length($id) != 11) {
155 $strid =~ s/[$ytchars]//g;
156 if (length($strid)) {
164 # Print program version
165 print("$progname $VERSION\n");
170 # Send the help message to stdout
173 if ($Opt{'verbose'}) {
179 Usage: $progname [options] URL
184 Generate generic URL instead of ID and remove additional URL
189 Be more quiet. Can be repeated to increase silence.
191 Increase level of verbosity. Can be repeated.
193 Print version information.
200 # Print a status message to stderr based on verbosity level
201 my ($verbose_level, $Txt) = @_;
203 if ($Opt{'verbose'} >= $verbose_level) {
204 print(STDERR
"$progname: $Txt\n");
210 my ($exitval, $msg) = @_;
212 print(STDERR
"$progname: $msg\n");
218 # This program is free software; you can redistribute it and/or modify it under
219 # the terms of the GNU General Public License as published by the Free Software
220 # Foundation; either version 2 of the License, or (at your option) any later
223 # This program is distributed in the hope that it will be useful, but WITHOUT
224 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
225 # FOR A PARTICULAR PURPOSE.
226 # See the GNU General Public License for more details.
228 # You should have received a copy of the GNU General Public License along with
230 # If not, see L<http://www.gnu.org/licenses/>.
232 # vim: set ts=8 sw=8 sts=8 noet fo+=w tw=79 fenc=UTF-8 :