Jump to content

Perl script for Komics


Recommended Posts

I know nothing of perl and so when something is a little different than described in the FAQs I'm pretty much lost. I want to grab the cartoon from this site (http://www.rotterdamsdagblad.nl/dirkjan/) using Komics (http://www.kde-apps.org/content/show.php?content=9078) and for that I need to add a little perl script. So I copied one of the default ones, read the instructions (http://www.orson.it/~domine/komics/index.html), and got stuck. The instructions mention an $atr_name = '' but the cartoonsite uses some sort of javascript

<script LANGUAGE="JavaScript">

<!-- Begin

today = new Date();

day = today.getDay();

arday = new Array("zaterdag.jpg", "maandag.jpg", "dinsdag.jpg",

"woensdag.jpg", "donderdag.jpg", "vrijdag.jpg", "zaterdag.jpg");

 

document.write("<img src='" + arday[day] + "'>");

//  End -->

</script>

 

So how to edit the itlle perl script for that??

Link to comment
Share on other sites

The URL to the comic is much easier than on the Komics example.

it is "http://www.rotterdamsdagblad.nl/dirkjan/" followed by the day of the week (in dutch) followed by .jpg, as in:

http://www.rotterdamsdagblad.nl/dirkjan/dinsdag.jpg

 

 

Look at the scripts that come with Komics to see if there is one that generates the day of the week (even if it's in English) as a starting point.

 

if, not, if what the script really needs to do is generate the URL to the pic, here's something:

#/bin/perl
%translateWeek = (
   "Mon" => "Lundi",
   "Tue" => "Mardi",
   "Wed" => "Mercredi",
   "Thu" => "Jeudi",
   "Fri" => "Vendredi",
   "Sat" => "Samedi",
   "Sun" => "Dimanche",
);
$now_time = localtime;
$now_time =~/^(\w+)\s/;
print 'http://www.rotterdamsdagblad.nl/dirkjan/', $translateWeek{$1}, ".jpg\n";

 

 

The hash at the start of the program matches the abbreviated name of the week with the full name, I'm running on a US installation but just for fun I matched them to Frecnh. Translate to Dutch as needed (I think the abbreviated form should use dutch names of the week but not sure). I'm sure you can figure it out.

 

 

Also, it's not clear from the Komics page if the script is limiter to a perl script ot any other kind of scripting language that you might be more familiar with, from sh to awk.

 

Disclaimer: I don't use Komics, so the script above is based solely on what my understanding of the komics perl script needs to do based on the page above.

Edited by papaschtroumpf
Link to comment
Share on other sites

If you wish to use bash instead of perl, this is how it may work based on the javascript you posted:

#! /bin/bash

url="http://www.rotterdamsdagblad.nl/dirkjan/"

ar_day=(zaterdag maandag dinsdag woensdag donderdag vrijdag zaterdag)

wget -c "${url}${ar_day[$(date +'%w')]}.jpg"

If instead of downloading the comic strip you want to output it as an html image link, change the wget line with this one

printf "<img src=\"${url}${ar_day[$(date +'%w')]}.jpg\" alt=\"comic\" />\n"

Edited by aru
Link to comment
Share on other sites

Thanks for the help :). I get the correct link now with the perl script and with the bash script I can download the jpg. Now I need to get it in Komics.

 

This is an example pl script that cam with comics:

 

#!/usr/bin/perl

use strict;
use warnings;

our $url = 'http://www.comics.com/creators/andycapp/index.html';
our $prefix = 'http://www.comics.com';
our $attr_value = 'Today\'s Comic';
our $attr_name = 'alt';
our $regex = '';

$0=~m|^(.*/).*?$|;
require $1.'komics.pm';

 

This is the komics.pl

use LWP::UserAgent;
require HTML::TokeParser;

my $ua = LWP::UserAgent->new;

if(    defined($ENV{'KOMICS_HTTP_PROXY'})
   || defined($ENV{'KOMICS_HTTPS_PROXY'})
   || defined($ENV{'KOMICS_FTP_PROXY'})   )
{
   $ua->proxy(
               http  => $ENV{'KOMICS_HTTP_PROXY'},
               https => $ENV{'KOMICS_HTTPS_PROXY'},
               ftp   => $ENV{'KOMICS_FTP_PROXY'}
             );
}

my $req = HTTP::Request->new('GET',$url);

my $res = $ua->request($req);

exit unless $res->is_success;

my $parser = HTML::TokeParser->new( \$res->content );

while (my $token = $parser->get_token)
{
   my $ttype = shift @{ $token };

   if($ttype eq "S")
   {
       my ($tagname,$attr) = @{ $token };

       if($tagname eq 'img')
       {
           my $attribute = $attr->{$attr_name};

           if(    (!isempty($regex) && $attr->{'src'} =~ /$regex/i)
               || (isempty($regex) && !isempty($attribute) && $attribute eq $attr_value)  )
           {
               print $prefix.$attr->{'src'}."\n";
               exit;
           }
       }
   }
}

sub isempty
{
   return (!defined($_[0]) || $_[0] eq '');
}

 

And this is my attempt (using papaschtoumpfs script):

#!/bin/perl

use strict;
use warnings;

our %translateWeek = (
  "Mon" => "maandag",
  "Tue" => "dinsdag",
  "Wed" => "woensdag",
  "Thu" => "donderdag",
  "Fri" => "vrijdag",
  "Sat" => "zaterdag",
  "Sun" => "zondag",
);
our $now_time = localtime;
$now_time =~/^(\w+)\s/;
our $regex = 'http://www.rotterdamsdagblad.nl/dirkjan/', $translateWeek{$1}, ".jpg\n";

$0=~m|^(.*/).*?$|;
require $2.'komics.pm';

 

When I run it I get

perl dirkjan.pl

Useless use of hash element in void context at dirkjan.pl line 17.

Useless use of a constant in void context at dirkjan.pl line 17.

Use of uninitialized value in concatenation (.) or string at dirkjan.pl line 20.

Link to comment
Share on other sites

Did you try using my script with no other modification than the names of the day?

 

According to the Komics page "you can indicate in the URL field a script name that prints on STDOUT the URL of the day. The script must be contained in the ~/.kde/share/apps/komics/ directory, and must have executable permissions."

My script does just that, it prints the URL on STDOUT. maybe you can remove the \n at the end of the URL if it causes trouble, but I would hope Komics knows how to deal with it.

 

This is just a guess since I don't use Komics though.

Edited by papaschtroumpf
Link to comment
Share on other sites

Yes I did but it didn't work. However I have been doing some experimenting. I noticed that your script runs fine when I give the command perl dirkjan.pl but when I give ./dirkjan.pl I got an error. So I changed #/bin/perl to #!/usr/bin/perl and now it runs :).

 

Unfortunately now I have an other error:

Helaas, de afbeelding

/tmp/kde-XXXXX/appletproxyWX0Iia.jpg

kan niet worden geladen.

Mogelijk wordt het afbeeldingformaat niet ondersteund, of is uw imlib-bibliotheek onjuist geïnstalleerd.

 

Which translates something like 'the image ........jpg could not be loaded. Perhaps the imageformat is not supported or your imlib library is installed incorrectly.

 

When I look in the /tmp folder I see only gifs but I can watch jpg with kuickshow (the image viewer I'm using without problems. I'll go and check the fileassociations again.

 

Thanks alot for the help.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

×
×
  • Create New...