# # Copyright (c) 2008-2009 by FlashCode # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see . # # Viedemerde API for WeeChat. # # History: # 2008-12-09, FlashCode : # version 0.1-dev: development version # 2008-12-08, FlashCode : # script creation use strict; use XML::Parser; my $version = "0.1-dev"; my $buffer = ""; my $buffer_title = "Vie de merde - vdm.pl $version"; my $current_vdm = ""; my $current_text = ""; my $current_index = 0; my %vdm = (); my $vdmlist = ""; sub xmlStartHandler { if ($_[1] eq "vdm") { $current_vdm = $_[3] if ($_[2] eq "id"); } else { $current_text = "" if ($_[1] eq "texte"); } } sub xmlEndHandler { if ($_[1] eq "texte") { $vdm{$current_index}{"id"} = $current_vdm; $vdm{$current_index}{"text"} = $current_text; $current_index++; $vdmlist .= ${current_vdm}.","; } } sub xmlCharHandler { $current_text .= $_[1]; } sub display_vdm { weechat::print($buffer, "-------\t---------------\n"); my $i = 0; foreach my $key (sort { $b <=> $a } keys %vdm) { weechat::print($buffer, "\n") if ($i > 0); weechat::print($buffer, weechat::color("white")."#".weechat::color("cyan")."$vdm{$key}{id}\t$vdm{$key}{text}"); $i++; } weechat::print($buffer, "-------\t---------------\n"); } sub get_vdm { if ($buffer ne "") { my $str_count = ($_[1] > 1) ? "($_[1])" : ""; weechat::buffer_set($buffer, "title", $buffer_title." | Views: last, random, top, flop, #, category | Displayed: $_[0] $str_count"); my $parser = new XML::Parser(); $parser->setHandlers('Start' => \&xmlStartHandler, 'End' => \&xmlEndHandler, 'Char' => \&xmlCharHandler); $current_vdm = ""; $current_text = ""; $current_index = 0; my $oldvdmlist = $vdmlist; %vdm = (); $vdmlist = ""; for (my $i = 0; $i < $_[1]; $i++) { my $output = `wget -q --timeout=5 --user-agent "Mozilla" --output-document=- "http://api.viedemerde.fr/1.2/view/$_[0]?key=readonly"`; $parser->parse($output); } if ($vdmlist ne $oldvdmlist) { weechat::buffer_clear($buffer); display_vdm(); } } } sub buffer_input { my ($key, $howmany) = split(/ /,$_[1]); if ((!defined $howmany) || ($howmany eq "") || ($key ne "random")) { $howmany = 1; } get_vdm($key, $howmany); return weechat::WEECHAT_RC_OK; } sub buffer_close { $buffer = ""; return weechat::WEECHAT_RC_OK; } sub buffer_new { $buffer = weechat::buffer_new("vdm", "buffer_input", "buffer_close"); if ($buffer ne "") { weechat::buffer_set($buffer, "title", $buffer_title); } } sub vdm { if ($buffer eq "") { buffer_new(); get_vdm("last", 1); weechat::buffer_set($buffer, "display", "1"); } return weechat::WEECHAT_RC_OK; } weechat::register("vdm", "FlashCode ", $version, "GPL3", "Viedemerde API for WeeChat", "", ""); weechat::hook_command("vdm", "Open vdm buffer", "", "", "", "vdm");