LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (http://www.linuxquestions.org/questions/linux-software-2/)
-   -   How to wget a random file from a website, then use FEH to view it. (http://www.linuxquestions.org/questions/linux-software-2/how-to-wget-a-random-file-from-a-website-then-use-feh-to-view-it-492888/)

xmrkite 10-16-2006 12:51 PM

How to wget a random file from a website, then use FEH to view it.
 
Hello,

What I want to do is have it so that I run wget and FEH (an image viewer) and each time they are run, a different, random image file is download with wget and run in feh.

Now here is the tricky part...the images are online and are cool Garfield comics. They are named according to the date. For example: http://images.ucomics.com/comics/ga/2006/ga061016.gif

As you can see, the year is the folder that the image lies in, and the filename is "ga" then two digit year, month, date then .gif

How would i create a script to randomly download just one image, then use feh to show that image?

Some of the years would start with 19 and some with 20, and i need to make sure i don't use dates like sep 31 or feb 29 (unless it's a leap year).

Any help would be great, even a point in the right direction is appreciated.

-Thanks

unSpawn 10-16-2006 04:22 PM

Here's an example of an download-and-display script: http://www.linuxquestions.org/questi...d.php?t=463192
So all you have to do is randomise the URI.

Code:

#!/bin/sh
doThis() { RAND=${RANDOM:0:4}; }
doThat() { doThis; [ $RAND -le 2800 ] && FILE=`date +'/%Y/qa/%y%m%d.gif' --date="$RAND days ago"` || doThat ; }
doThat &&FILE=" http://images.ucomics.com/comics/ga${FILE}"
/some/app "$FILE"
exit 0

Now you can use the $FILE variable with the fetcher to get the file and display it on your desktop or script it to save it with wget to a tempfile and display with your viewer.

matthewg42 10-17-2006 07:09 AM

Here's a way to do it with perl:

Code:

#!/usr/bin/perl -w

use Date::Parse;
use Date::Format;

my $earliest = str2time("2006:01:01");
my $get_time = $earliest + int( rand() * (time() - $earliest ));
my $url = time2str("http://images.ucomics.com/comics/ga/%Y/ga%y%m%d.gif", $get_time);
system("feh", $url);

Notes:
  1. You can set the earliest date by modifying the 2006:01:01 to whatever is the earliest date for which a URL exists (YYYY MM DD format).
  2. feh is called on the URL directly. No need for wget first.
  3. Don't forget to chmod a+x the file before trying to run it.
  4. All your scripts are belong to us.

xmrkite 10-17-2006 01:26 PM

Hey guys, thanks for the help.

I was able to get it going nicely.


All times are GMT -5. The time now is 12:17 PM.