kara-moe-dl.pl (2527B)
- #!/usr/bin/env perl
- # Multimedia-DL: Youtube-DL inspired scraper
- # Copyright © 2021 Multimedia-DL Authors <https://hacktivis.me/git/multimedia-dl/>
- # SPDX-License-Identifier: AGPL-3.0-only
- use strict;
- use utf8;
- use HTML::TreeBuilder::XPath;
- use URI;
- use LWP::UserAgent;
- use LWP::Simple;
- use File::Basename;
- require HTTP::Request;
- use JSON::MaybeXS;
- sub get_suffix {
- my ($str) = @_;
- $str =~ /(\.[^\/]*)$/;
- return $1;
- }
- sub download {
- my ($url, $songname) = @_;
- my $fname = $songname . get_suffix($url);
- print STDERR "Downloading: $url -> $fname\n";
- if(!mirror($url, $fname)) {
- print STDERR "Failed downloading $url: $!\n";
- }
- return $fname;
- }
- my $json = JSON::MaybeXS->new(utf8 => 1);
- my $ua = LWP::UserAgent->new;
- # Picked this one for it's stability
- my $webkit_ua = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.0 Safari/605.1.15";
- $ua->agent($webkit_ua . "Multimedia-DL/1.0");
- if($#ARGV != 0) {
- print "usage: kara.moe <kara-id>\n";
- exit 1;
- }
- # Just in case an URL got passed
- my $kid = basename($ARGV[0], qr/\.[^.]*/);
- my $url = "https://kara.moe/api/karas/" . $kid;
- print STDERR "URL: $url\n";
- my $req = HTTP::Request->new(GET => $url);
- my $res = $ua->request($req) or die "$!";
- if($res->is_success) {
- my $content_type = $res->header("Content-Type");
- my $content_match = qr(^application/([^+]*\+)?json(; .*)?);
- if(!($content_type =~ $content_match)) {
- die "Got $content_type instead of $content_match\n";
- }
- my $blob = $json->decode($res->content) or die "Couldn't decode JSON object";
- print STDERR "Song name: ", $blob->{"songname"}, "\n";
- #if((scalar $blob->{"lyrics_infos"}) > 2)
- #{
- # foreach \my %lyrics (@($blob->{"lyrics_infos"}))
- # {
- # my $songname = $blob->{"songname"} . "-" . %lyrics->{"version"};
- # my $fname = download("https://kara.moe/downloads/lyrics/".%lyrics->{"filename"}, $songname);
- #
- # if($blob->{"songname"}->{"default"} == $json::true)
- # {
- # symlink($fname, $blob->{"songname"} . get_suffix(%lyrics->{"filename"})) or warn "Failed creating symlink for lyrics: $!";
- # }
- # }
- #}
- #else
- #{
- #}
- if ($blob->{"lyrics_infos"}[0]->{"filename"}) {
- download("https://kara.moe/downloads/lyrics/".$blob->{"lyrics_infos"}[0]->{"filename"}, $blob->{"songname"});
- }
- if ($blob->{"mediafile"}) {
- download("https://kara.moe/downloads/medias/".$blob->{"mediafile"}, $blob->{"songname"});
- }
- if ($blob->{"karafile"}) {
- download("https://kara.moe/downloads/karaokes/".$blob->{"karafile"}, $blob->{"songname"});
- }
- }