Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/perl
- #---------------------------------------------------
- # IRC MAL Parser
- # For HexChat
- # uses the "unofficial" MAL API at http://mal-api.com
- #---------------------------------------------------
- # TODO: Some refinements
- # (like that hash-based floodprot)
- #---------------------------------------------------
- use strict;
- use warnings;
- use HTTP::Tiny;
- use Xchat qw( :all );
- register("IRC MAL Parser","1.0.5","Generic MyAnimeList searcher using the unofficial MyAnimeList API","");
- hook_print('Channel Message',\&trigger);
- my $floodprot = 0;
- my $floodprot_dur = 8000;
- Xchat::prnt('MAL Parser Loaded');
- sub trigger {
- my ($nick,$msg) = ($_[0][0],$_[0][1]);
- if ($floodprot != 1) {
- if ($msg =~ /^\.mal (.*?) \/(\d+)$/i) { &search ($nick, $1, "info", ($2-1)); }
- elsif ($msg =~ /^\.mal (.*)$/i) { &search ($nick, $1, "search", ""); }
- }
- }
- sub search {
- my ($nick, $searchterm, $flag, $num) = @_;
- # Xchat:prnt("\x0303*DEBUG\x0F\tSub args: " . join(', ', @_));
- my $searchtermwf = $searchterm;
- $searchtermwf =~ s/ /%20/g;
- my $response = HTTP::Tiny->new->get("http://mal-api.com/anime/search?q=$searchtermwf");
- my $ct = 0;
- # print $response->{content};
- my %data;
- my @months = ("","January","February","March","April","May","June","July","August","September","October","November","December");
- $response->{content} =~ s/^\[|\]$//g;
- my @anime = split(m/},{(?=\"id\":\d+)/,$response->{content});
- if (scalar(@anime) == 0) {
- # nothing found
- command ("say I can't find anything matching that keyword.");
- } else {
- foreach my $i (@anime) {
- $i =~ s/^\{|\}$//g;
- # print MALRESPONSE "** RAWLINE >> $i\n";
- ######################################################
- # OMG OMG OMG LOOK AT DAT REGEX #
- ######################################################
- if ($i =~ /
- "id":(?<id>.*?),
- "title":"(?<title>.*?)",
- "other_titles":.*?,
- "synopsis":"(?<synopsis>.*)",
- "type":"(?<type>.*?)",
- "rank":.*?,
- "popularity_rank":.*?,
- "image_url":".*",
- "episodes":(?<episodes>.*?),
- "status":.*?,
- "start_date":(?<start_date>.*?),
- "end_date":(?<end_date>.*?),
- "genres":.*?,
- "tags":.*?,
- "classification":"(?<classification>.*?)",
- "members_score":(?<members_score>.*?),
- "members_count":.*?,
- "favorited_count".*?,
- "manga_adaptations":.*?,
- "prequels":.*?,
- "sequels":.*?,
- "side_stories":.*?,
- "parent_story":.*?,
- "character_anime":.*?,
- "spin_offs":.*?,
- "summaries":.*?,
- "alternative_versions":.*?,
- "listed_anime_id":.*?,
- "watched_episodes":.*?,
- "score":.*?,
- "watched_status":.*?
- /x) {
- %data = %+;
- # Set dates
- if ($data{start_date} =~ /(\d\d\d\d)-(\d\d)-(\d\d).*/) { $data{start_date} = "$months[$2] $3, $1"; }
- if ($data{end_date} =~ /(\d\d\d\d)-(\d\d)-(\d\d).*/) { $data{end_date} = "$months[$2] $3, $1"; }
- if ($data{start_date} eq "null" and $data{end_date} eq "null") { $data{air_dates} = "\x02Not Aired Yet\x02"; }
- elsif ($data{end_date} eq "null") { $data{air_dates} = "\x02Started Airing\x02 $data{start_date}"; }
- elsif ($data{start_date} eq $data{end_date}) { $data{air_dates} = "\x02Aired\x02 $data{end_date}"; }
- else { $data{air_dates} = "\x02Aired\x02 $data{start_date} to $data{end_date}"; }
- # clean up the synopsis
- $data{synopsis} =~ s/\\(n|r)+/ /g;
- $data{synopsis} =~ s/<br\/?>//g;
- $data{synopsis} =~ s/\\"/"/g;
- $data{synopsis} =~ s/"/\"/g;
- $data{synopsis} =~ s/—/--/g;
- $data{synopsis} =~ s/ ...$/.../;
- # clean up episode numbers
- $data{episodes} =~ s/^0$|^null$/N\/A/g;
- # trim the synopsis
- if (length ($data{synopsis}) > 325) {
- $data{synopsis} =~ s/ ...$//;
- $data{synopsis} =~ s/(.{325}).*/$1/;
- $data{synopsis} = "$data{synopsis}...";
- }
- my $sep = "\x02\x0303::\x0F";
- my $line1 = "$sep \x02$data{title}\x02 $sep \x02Type\x02 $data{type} $sep \x02Members' Score\x02 $data{members_score}/10 $sep \x02Episodes\x02 $data{episodes} $sep $data{air_dates} $sep \x02Classification\x02 $data{classification} $sep";
- my $line2 = "$sep \x02Synopsis\x02 $data{synopsis} $sep \x02Link\x02 http://myanimelist.net/anime/$data{id} $sep";
- my $line3 = "$sep [" . ($ct + 1) . "/" . scalar(@anime) . "] \x02$data{title}\x02 $sep \x02Type\x02 $data{type} $sep \x02Members' Score\x02 $data{members_score}/10 $sep \x02Episodes\x02 $data{episodes} $sep \x02Link\x02 http://myanimelist.net/anime/$data{id} $sep";
- if ($flag eq "search") {
- # Xchat:prnt("\x0303*DEBUG\x0F\t\$ct is: $ct");
- if (scalar(@anime) == 1) {
- # 1 show found; print complete details
- command ("say $line1");
- command ("say $line2");
- $floodprot = 1;
- hook_timer( $floodprot_dur, sub { $floodprot = 0; return REMOVE; });
- } elsif (scalar(@anime) > 1 && scalar(@anime) <= 3) {
- # 2-3 shows found; print summaries for them all
- command ("say $line3");
- if ($ct == (scalar(@anime) - 1)) { command ("notice $nick ". scalar(@anime) . " results found; to view detailed info about a particular result, use \x02.mal $searchterm /<result_number>\x02. You can also visit the search page at http://myanimelist.net/anime.php?q=$searchtermwf"); }
- $floodprot = 1;
- hook_timer( $floodprot_dur, sub { $floodprot = 0; return REMOVE; });
- } else {
- # more than 3 shows found; print summaries of first 3
- if ($ct < 2) { command ("say $line3"); }
- if ($ct == 2) { command ("say $line3"); command ("notice $nick Showing first 3 of ". scalar(@anime) . " results found; to view detailed info about a particular result, use \x02.mal $searchterm /<result_number>\x02. You can also visit the search page at http://myanimelist.net/anime.php?q=$searchtermwf"); }
- $floodprot = 1;
- hook_timer( $floodprot_dur, sub { $floodprot = 0; return REMOVE; });
- }
- } elsif ($flag eq "info") {
- # Xchat:prnt("\x0303*DEBUG\x0F\t\$ct is: $ct");
- if ($num >= 0 and $num <= scalar(@anime)){
- if ($ct == $num) {
- # 1 show found; print complete details
- command ("say $line1");
- command ("say $line2");
- $floodprot = 1;
- hook_timer( $floodprot_dur, sub { $floodprot = 0; return REMOVE; });
- }
- } else {
- if ($num < 0 && $ct == 0) { command ("notice $nick \x02\x0304Error:\x02\x03 It starts with 1, heh heh."); }
- elsif ($num > scalar(@anime) && $ct == 0) { command ("notice $nick \x02\x0304Error:\x02\x03 Only " . scalar(@anime) . "results found."); }
- }
- }
- }
- $ct++;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement