Advertisement
TakatsukiYayoi

Just Another MAL Parser

Jul 21st, 2013
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 7.91 KB | None | 0 0
  1. #!/usr/bin/perl
  2. #---------------------------------------------------
  3. # IRC MAL Parser
  4. # For HexChat
  5. # uses the "unofficial" MAL API at http://mal-api.com
  6. #---------------------------------------------------
  7. # TODO: Some refinements
  8. #  (like that hash-based floodprot)
  9. #---------------------------------------------------
  10.  
  11. use strict;
  12. use warnings;
  13.  
  14. use HTTP::Tiny;
  15.  
  16. use Xchat qw( :all );
  17.  
  18. register("IRC MAL Parser","1.0.5","Generic MyAnimeList searcher using the unofficial MyAnimeList API","");
  19.  
  20. hook_print('Channel Message',\&trigger);
  21. my $floodprot = 0;
  22. my $floodprot_dur = 8000;
  23.  
  24. Xchat::prnt('MAL Parser Loaded');
  25.  
  26. sub trigger {
  27.  my ($nick,$msg) = ($_[0][0],$_[0][1]);
  28.   if ($floodprot != 1) {
  29.    if     ($msg =~ /^\.mal (.*?) \/(\d+)$/i) { &search ($nick, $1, "info", ($2-1)); }
  30.    elsif  ($msg =~ /^\.mal (.*)$/i) { &search ($nick, $1, "search", ""); }
  31.   }
  32.  }
  33.  
  34. sub search {
  35. my ($nick, $searchterm, $flag, $num) = @_;
  36. # Xchat:prnt("\x0303*DEBUG\x0F\tSub args: " . join(', ', @_));
  37. my $searchtermwf = $searchterm;
  38. $searchtermwf =~ s/ /%20/g;
  39.  
  40. my $response = HTTP::Tiny->new->get("http://mal-api.com/anime/search?q=$searchtermwf");
  41.  
  42. my $ct = 0;
  43.  
  44. # print $response->{content};
  45.  my %data;
  46.  my @months = ("","January","February","March","April","May","June","July","August","September","October","November","December");
  47.  $response->{content} =~ s/^\[|\]$//g;
  48.  my @anime = split(m/},{(?=\"id\":\d+)/,$response->{content});
  49.   if (scalar(@anime) == 0) {
  50.    # nothing found
  51.    command ("say I can't find anything matching that keyword.");
  52.   } else {
  53.   foreach my $i (@anime) {
  54.      $i =~ s/^\{|\}$//g;
  55.      # print MALRESPONSE "** RAWLINE >> $i\n";
  56.      ######################################################
  57.      #            OMG OMG OMG LOOK AT DAT REGEX           #
  58.      ######################################################
  59.      if ($i =~ /
  60.                "id":(?<id>.*?),
  61.                "title":"(?<title>.*?)",
  62.                "other_titles":.*?,
  63.                "synopsis":"(?<synopsis>.*)",
  64.                "type":"(?<type>.*?)",
  65.                "rank":.*?,
  66.                "popularity_rank":.*?,
  67.                "image_url":".*",
  68.                "episodes":(?<episodes>.*?),
  69.                "status":.*?,
  70.                "start_date":(?<start_date>.*?),
  71.                "end_date":(?<end_date>.*?),
  72.                "genres":.*?,
  73.                "tags":.*?,
  74.                "classification":"(?<classification>.*?)",
  75.                "members_score":(?<members_score>.*?),
  76.                "members_count":.*?,
  77.                "favorited_count".*?,
  78.                "manga_adaptations":.*?,
  79.                "prequels":.*?,
  80.                "sequels":.*?,
  81.                "side_stories":.*?,
  82.                "parent_story":.*?,
  83.                "character_anime":.*?,
  84.                "spin_offs":.*?,
  85.                "summaries":.*?,
  86.                "alternative_versions":.*?,
  87.                "listed_anime_id":.*?,
  88.                "watched_episodes":.*?,
  89.                "score":.*?,
  90.                "watched_status":.*?
  91.                /x) {
  92.                
  93.                 %data = %+;
  94.                
  95.                 # Set dates
  96.                 if ($data{start_date} =~ /(\d\d\d\d)-(\d\d)-(\d\d).*/) { $data{start_date} = "$months[$2] $3, $1"; }
  97.                 if ($data{end_date} =~ /(\d\d\d\d)-(\d\d)-(\d\d).*/) { $data{end_date} = "$months[$2] $3, $1"; }
  98.                 if    ($data{start_date} eq "null" and $data{end_date} eq "null") { $data{air_dates} = "\x02Not Aired Yet\x02"; }
  99.                 elsif ($data{end_date} eq "null") { $data{air_dates} = "\x02Started Airing\x02 $data{start_date}"; }
  100.                 elsif ($data{start_date} eq $data{end_date}) { $data{air_dates} = "\x02Aired\x02 $data{end_date}"; }
  101.                 else  { $data{air_dates} = "\x02Aired\x02 $data{start_date} to $data{end_date}"; }
  102.                                
  103.                 # clean up the synopsis
  104.                 $data{synopsis} =~ s/\\(n|r)+/ /g;
  105.                 $data{synopsis} =~ s/<br\/?>//g;
  106.                 $data{synopsis} =~ s/\\"/"/g;
  107.                 $data{synopsis} =~ s/&quot;/\"/g;
  108.                 $data{synopsis} =~ s/&mdash;/--/g;
  109.                 $data{synopsis} =~ s/ ...$/.../;
  110.                
  111.                 # clean up episode numbers
  112.                 $data{episodes} =~ s/^0$|^null$/N\/A/g;
  113.                
  114.                 # trim the synopsis
  115.                 if (length ($data{synopsis}) > 325) {
  116.                    $data{synopsis} =~ s/ ...$//;
  117.                    $data{synopsis} =~ s/(.{325}).*/$1/;
  118.                    $data{synopsis} = "$data{synopsis}...";
  119.                  }
  120.                
  121.                 my $sep = "\x02\x0303::\x0F";
  122.                 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";
  123.                 my $line2 = "$sep \x02Synopsis\x02 $data{synopsis} $sep \x02Link\x02 http://myanimelist.net/anime/$data{id} $sep";
  124.                 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";
  125.                
  126.                 if ($flag eq "search") {
  127.                  # Xchat:prnt("\x0303*DEBUG\x0F\t\$ct is: $ct");
  128.                  if (scalar(@anime) == 1) {
  129.                    # 1 show found; print complete details
  130.                      command ("say $line1");
  131.                      command ("say $line2");
  132.                      $floodprot = 1;
  133.                      hook_timer( $floodprot_dur, sub { $floodprot = 0; return REMOVE; });
  134.                    } elsif (scalar(@anime) > 1 && scalar(@anime) <= 3) {
  135.                       # 2-3 shows found; print summaries for them all
  136.                       command ("say $line3");
  137.                       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"); }
  138.                       $floodprot = 1;
  139.                       hook_timer( $floodprot_dur, sub { $floodprot = 0; return REMOVE; });
  140.                      } else {
  141.                        # more than 3 shows found; print summaries of first 3
  142.                        if ($ct < 2) { command ("say $line3"); }
  143.                        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"); }
  144.                        $floodprot = 1;
  145.                        hook_timer( $floodprot_dur, sub { $floodprot = 0; return REMOVE; });
  146.                       }
  147.                   } elsif ($flag eq "info") {
  148.                     # Xchat:prnt("\x0303*DEBUG\x0F\t\$ct is: $ct");
  149.                     if ($num >= 0 and $num <= scalar(@anime)){
  150.                      if ($ct == $num) {
  151.                       # 1 show found; print complete details
  152.                       command ("say $line1");
  153.                       command ("say $line2");
  154.                       $floodprot = 1;
  155.                       hook_timer( $floodprot_dur, sub { $floodprot = 0; return REMOVE; });
  156.                      }
  157.                     } else {
  158.                       if    ($num < 0 && $ct == 0) { command ("notice $nick \x02\x0304Error:\x02\x03 It starts with 1, heh heh."); }
  159.                       elsif ($num > scalar(@anime) && $ct == 0) { command ("notice $nick \x02\x0304Error:\x02\x03 Only " . scalar(@anime) . "results found."); }
  160.                      }
  161.                   }
  162.      }
  163.     $ct++;
  164.    }
  165.   }
  166. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement