logo

multimedia-dl

Unnamed repository; edit this file 'description' to name the repository. git clone https://anongit.hacktivis.me/git/multimedia-dl.git/

kara-moe-dl.pl (2527B)


  1. #!/usr/bin/env perl
  2. # Multimedia-DL: Youtube-DL inspired scraper
  3. # Copyright © 2021 Multimedia-DL Authors <https://hacktivis.me/git/multimedia-dl/>
  4. # SPDX-License-Identifier: AGPL-3.0-only
  5. use strict;
  6. use utf8;
  7. use HTML::TreeBuilder::XPath;
  8. use URI;
  9. use LWP::UserAgent;
  10. use LWP::Simple;
  11. use File::Basename;
  12. require HTTP::Request;
  13. use JSON::MaybeXS;
  14. sub get_suffix {
  15. my ($str) = @_;
  16. $str =~ /(\.[^\/]*)$/;
  17. return $1;
  18. }
  19. sub download {
  20. my ($url, $songname) = @_;
  21. my $fname = $songname . get_suffix($url);
  22. print STDERR "Downloading: $url -> $fname\n";
  23. if(!mirror($url, $fname)) {
  24. print STDERR "Failed downloading $url: $!\n";
  25. }
  26. return $fname;
  27. }
  28. my $json = JSON::MaybeXS->new(utf8 => 1);
  29. my $ua = LWP::UserAgent->new;
  30. # Picked this one for it's stability
  31. my $webkit_ua = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.0 Safari/605.1.15";
  32. $ua->agent($webkit_ua . "Multimedia-DL/1.0");
  33. if($#ARGV != 0) {
  34. print "usage: kara.moe <kara-id>\n";
  35. exit 1;
  36. }
  37. # Just in case an URL got passed
  38. my $kid = basename($ARGV[0], qr/\.[^.]*/);
  39. my $url = "https://kara.moe/api/karas/" . $kid;
  40. print STDERR "URL: $url\n";
  41. my $req = HTTP::Request->new(GET => $url);
  42. my $res = $ua->request($req) or die "$!";
  43. if($res->is_success) {
  44. my $content_type = $res->header("Content-Type");
  45. my $content_match = qr(^application/([^+]*\+)?json(; .*)?);
  46. if(!($content_type =~ $content_match)) {
  47. die "Got $content_type instead of $content_match\n";
  48. }
  49. my $blob = $json->decode($res->content) or die "Couldn't decode JSON object";
  50. print STDERR "Song name: ", $blob->{"songname"}, "\n";
  51. #if((scalar $blob->{"lyrics_infos"}) > 2)
  52. #{
  53. # foreach \my %lyrics (@($blob->{"lyrics_infos"}))
  54. # {
  55. # my $songname = $blob->{"songname"} . "-" . %lyrics->{"version"};
  56. # my $fname = download("https://kara.moe/downloads/lyrics/".%lyrics->{"filename"}, $songname);
  57. #
  58. # if($blob->{"songname"}->{"default"} == $json::true)
  59. # {
  60. # symlink($fname, $blob->{"songname"} . get_suffix(%lyrics->{"filename"})) or warn "Failed creating symlink for lyrics: $!";
  61. # }
  62. # }
  63. #}
  64. #else
  65. #{
  66. #}
  67. if ($blob->{"lyrics_infos"}[0]->{"filename"}) {
  68. download("https://kara.moe/downloads/lyrics/".$blob->{"lyrics_infos"}[0]->{"filename"}, $blob->{"songname"});
  69. }
  70. if ($blob->{"mediafile"}) {
  71. download("https://kara.moe/downloads/medias/".$blob->{"mediafile"}, $blob->{"songname"});
  72. }
  73. if ($blob->{"karafile"}) {
  74. download("https://kara.moe/downloads/karaokes/".$blob->{"karafile"}, $blob->{"songname"});
  75. }
  76. }