Forum Moderators: coopster & phranque

Message Too Old, No Replies

Help with Perl random image script

         

lindajames

12:26 pm on Mar 14, 2003 (gmt 0)

10+ Year Member



I am in need of help and was wondering if anyone could help me on something im trying to do. Basically i downloaded a code from the net that randomizes images. Heres the code:

============================================
$basedir = "http://www.mydomain.co.uk/";

@files = qw(1.jpg 2.jpg 3.jpg 4.jpg);

srand(time ^ $$);
$num = rand(@files);

print "Location: $basedir$files[$num]\n\n";
============================================

The script works perfect, but i wanted to customize it to use alternative settings if first setting not found. In other words when the script looks at $basedir and @files for a file, if the file cannot be found or has been renamed etc, the script should use an alternative setting i.e. $basedir2 and @files2 so basically $basedir2 and @files2 will be exactly same as the other but the only thing that will be different is the value of $basedir2. (if you know what i mean?) just like the way Primary and Secondary DNS works.

Note: it should only resort to the alternative setting if the image is not found not any other reason.

I will be very greatful if someone can help me out on this

I appreciate your help

Thanks
Linda

andreasfriedrich

12:38 pm on Mar 14, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This script should do the trick:


$basedir = "http://www.mydomain.co.uk/";
$basedir2 = "http://mirror.mydomain.co.uk/";
#
@files = %qw%(1.jpg 2.jpg 3.jpg 4.jpg);
#
srand [perldoc.com](time [perldoc.com] ^ $$);
$num = rand [perldoc.com](@files);
#
$img = join [perldoc.com] '',
(-f "$ENV{DOCUMENT_ROOT}/$files[$num]"? $basedir : $basedir2),
$files[$num];
#
print [perldoc.com] "Location: $img\n\n";

The -f operator returns true if the argument is a file.

Andreas

lindajames

5:03 pm on Mar 14, 2003 (gmt 0)

10+ Year Member



I tried that and i get errors, ive pasted the error descriptions below for you to see.

please help me out.

Thanks
Linda

Bareword found where operator expected at d:\htdocs\image.cgi line 4, near "1.jpg"
(Missing operator before jpg?)
syntax error at d:\htdocs\image.cgi line 4, near "1.jpg "
Number found where operator expected at d:\htdocs\image.cgi line 4, near "jpg 2."
(Do you need to predeclare jpg?)
Bareword found where operator expected at d:\htdocs\image.cgi line 4, near "2.jpg"
(Missing operator before jpg?)
Number found where operator expected at d:\htdocs\image.cgi line 4, near "jpg 3."
(Do you need to predeclare jpg?)
Bareword found where operator expected at d:\htdocs\image.cgi line 4, near "3.jpg"
(Missing operat

andreasfriedrich

5:49 pm on Mar 14, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The line defining your images needs to read like this:

@files = qw(1.jpg 2.jpg 3.jpg 4.jpg);

The qw enclosed in percent signs was just me and my auto quoting system trying to turn qw into a link to the Perl [perl.com] manual entry for qw.

Andreas

lindajames

6:37 pm on Mar 14, 2003 (gmt 0)

10+ Year Member



I did that its working now, but the script seems to go to the mirror site instead of the main site, and yes the main images do exist

whats wrong now?

andreasfriedrich

6:50 pm on Mar 14, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



>>go to the mirror site instead of the main site

The Perl [perl.com] cannot find the images. Are they stored in the DocumentRoot [httpd.apache.org] folder? If not you need to specify their path.

Andreas

lindajames

9:20 pm on Mar 14, 2003 (gmt 0)

10+ Year Member



The thing is $basedir is on a total different server (a Unix one) and $basedir2 is on another server (windowsNT one) the script is hosted on the $basedir2 site and im assuming thats why its going to $basedir2 by default and cannot find the images at $basedir

Really Appreciate you help

Cheers

lindajames

11:28 am on Mar 18, 2003 (gmt 0)

10+ Year Member



I have this random image script below, that basically has a mirror site feature. so if the file on $basedir is not found it looks for the same files on $basedir2

The only problem i am having is that, the actual script is hosted on $basedir2 server and its WindowsNT and $basedir is on a total different web URL and a Unix Server. And since the script is located in $basedir2 it tries to located $basedir on that same server for somereason, and it cant find $basedir and therefore automatically goes to $basedir2.

Can anyone help me customize it so that it checks $basedir on a different server first and only (ONLY) if it is not found then the last resort should be to switch to $basedir2

$basedir = "http://www.mydomain.co.uk/";
$basedir2 = "http://mirror.mydomain.co.uk/";
#
@files = qw(1.jpg 2.jpg 3.jpg 4.jpg);
#
srand (time ^ $$);
$num = rand (@files);
#
$img = join '',
(-f "$ENV{DOCUMENT_ROOT}/$files[$num]"? $basedir : $basedir2),
$files[$num];
#
print "Location: $img\n\n";

I appreciate anyones help

Cheers
Linda

dive into perl

7:23 pm on Mar 18, 2003 (gmt 0)

10+ Year Member



Hi Linda,

For a Perl script to determine if a file exits on a different server you are going to need to make a HTTP request to that server and see if you get a valid response.

You can do this by using the LWP::UserAgent.pm [search.cpan.org] module along with HTTP::Status.

The snippet of code shows how a script running on servera checks for a file on serverb and depending on the result will print the correct location.

Use this example as a starting point to adapt your existing script.


use LWP::UserAgent;
use HTTP::Status;

my $ua = new LWP::UserAgent;

# define a User Agent and Timeout #
$ua->agent('My Image Cheker');
$ua->timeout(20);

# make the request to server b
my $req = new HTTP::Request( GET => 'http://www.serverb.tld/1.jpg');

# check if server b responds
my $response = $ua->request($req);

if ($response->is_success) {

# image file exists on serverb
print "Location: [serverb.tld...]

} else {

# image file does not exist on server b
# so print the copy from server a

print "Location: [servera.tld...]
}

On a more basic note, can't your @files array have the full URL listed :-


@files = qw(
[servera.tld...]
[serverb.tld...]
[serverb.tld...]
[servera.tld...]
);

This would save the overhead of making the request to another server.

- dip
use strict or die();

lindajames

8:54 pm on Mar 18, 2003 (gmt 0)

10+ Year Member



yes my @files array can have the full URL listed but what would the script look like if i wanted it like that?

thanx for your help