Image renamer and randomizer

Netopia

Diamond Member
Oct 9, 1999
4,793
4
81
Hey Gang....

I was thinking the other day that it would be cool to be able to have a collection of graphics to use as avatars at various BBSes that allow you to link to external graphics. I figured that I'd be able to quickly find a script out there that would rename/recycle filenames to be whatever I wanted, but I've found nothing at all!

Actually, that's not true. I've found a LOT of things that will allow you to randomize a graphic in a web page, but that isn't what I'm talking about.

Scenario:

Let's say that I link to an off site graphic called avatar.jpg

Let's also say that I have a directory on a Linux box with 100 graphics.

What I envision is a program that will, on a regular basis (once every 10, 20 30 seconds... 1, 2, 5, 10 minutes...whatever) rename a file to "avatar.jpg". At the next cycle it will rename that file something else... I don't care what else, just something... and then it will randomly choose another file in the directory and rename it to be "avatar.jpg".

This way, the remote server is always linked to "avatar.jpg", but the actual graphic that "avatar.jpg" represents would be constantly changing.

Perhaps there is a better and more obvious way to do this that I'm missing. Is so, I'd be very appreciative to be enlightened.

If not... what would be the best way to go about programming some script in Linux to do it (as a cron job?) ? I'm not very well versed at programming more than the most basic of scripts, but I'm willing to learn and get my feel wet with this, but I'm not even sure of how to go about it.

Any thoughts?

Was I clear in what I'm trying to describe?

Thanks in advance for any input!

Joe
 

CTho9305

Elite Member
Jul 26, 2000
9,214
1
81
Not at all highly technical, and pretty easy.

Just put the files in a directory, name them avatar1.jpg, avatar2.jpg, etc, and have people access them through a script (perl, php, whatever) that serves a different image back based on time.
 

LeoDioxide

Member
Mar 2, 2006
43
0
0
Different image based on time, meaning you have it load avatar.jpg where i is a randomly generated number. Store that number somewhere to prevent the same image popping up over and over, and voila! However, that approach only works on the client side.

If you wanted server side, you'd have to write a script (I don't do shell scripts because I'm lazy). Hopefully, you would numerically name each file if you didn't care what they were. You could then use a random number generator to pick the file, rename it to avatar.jpg, store the filename, and wait. After 5 minutes, rename the file back to 003.jpg and pick again. Not difficult, read up on your scripting.
 

Netopia

Diamond Member
Oct 9, 1999
4,793
4
81
Originally posted by: CTho9305
Not at all highly technical, and pretty easy.

Just put the files in a directory, name them avatar1.jpg, avatar2.jpg, etc, and have people access them through a script (perl, php, whatever) that serves a different image back based on time.

That doesn't serve my purpose though. I don't know of a BBS that will allow you to point to a remote script as your avatar, only remote graphics. I need the graphic actually changed ON THE SERVER and to always be the same name.

Joe
 

Netopia

Diamond Member
Oct 9, 1999
4,793
4
81
Ok... did some thinking about this over night and a couple things occurred to me.

1) I'll have to come up with some if-then type testing for checking names of files in the directory and renaming them, since I want the script to automate the entire process. If others wanted to use the same script, I'd like them to be able to simply copy more images into the directory and have it automatically start using them.

2) I should have the renaming logic append -xxxx (4 numbers) to the end of the file name (before the .jpg) and use that for choosing the file to be displayed as corresponds to a random number.

3) I don't need to constantly rename anything... I was thinking too hard. Simply taking the next random file and copying it to avatar.jpg (over writing the previous avatar.jpg) is the way to go. MUCH simpler and cleaner.

Actually, I'm now getting sort of excited about learning enough about what is available at the command line (or perhaps perl) and working out the logic of this. I AM however, still interested in other people's ideas and input, since experience and knowledge could keep me from making some bonehead beginner's error!

Joe

 

CTho9305

Elite Member
Jul 26, 2000
9,214
1
81
Originally posted by: Netopia
Originally posted by: CTho9305
Not at all highly technical, and pretty easy.

Just put the files in a directory, name them avatar1.jpg, avatar2.jpg, etc, and have people access them through a script (perl, php, whatever) that serves a different image back based on time.

That doesn't serve my purpose though. I don't know of a BBS that will allow you to point to a remote script as your avatar, only remote graphics. I need the graphic actually changed ON THE SERVER and to always be the same name.

Joe

The BBS doesn't have to know. You can name your perl/php script .jpg if the BBS really bothers to check. Since your script will actually be serving image content types, even a thorough check by the BBS (i.e. the admin visiting the URL you submit) will make it appear to be an image rather than a script.
 

Netopia

Diamond Member
Oct 9, 1999
4,793
4
81
Originally posted by: Netopia
Ok... did some thinking about this over night and a couple things occurred to me.

1) I'll have to come up with some if-then type testing for checking names of files in the directory and renaming them, since I want the script to automate the entire process. If others wanted to use the same script, I'd like them to be able to simply copy more images into the directory and have it automatically start using them.

2) I should have the renaming logic append -xxxx (4 numbers) to the end of the file name (before the .jpg) and use that for choosing the file to be displayed as corresponds to a random number.

3) I don't need to constantly rename anything... I was thinking too hard. Simply taking the next random file and copying it to avatar.jpg (over writing the previous avatar.jpg) is the way to go. MUCH simpler and cleaner.

Actually, I'm now getting sort of excited about learning enough about what is available at the command line (or perhaps perl) and working out the logic of this. I AM however, still interested in other people's ideas and input, since experience and knowledge could keep me from making some bonehead beginner's error!

Joe


Wow... that's interesting. I'll have to give that a try.

Joe
 

AnthraX101

Senior member
Oct 7, 2001
771
0
0
Add this to your .htaccess file:

AddType application/x-httpd-php .gif

Now you are able to name a PHP file avatar.gif.

AnthraX101
 

theanorak

Junior Member
Jul 31, 2006
5
0
0
Depending on how frequently you want to change avatars, I wouldn't go that far.

If you want to change avatars every half-a-day or whatever, then I'd write a script which followed this path:

Collect a list of filenames in the "avatars" folder which correspond to the filetype(s) you use for your avatars - you'd probably want to stick with one filetype for everything.

Select a random filename from the list and compare it to the previous selected avatar.
You can store the filename of the previously-selected file in an empty file called previous_avatar_name.gif.previous (or something0, then you wouldn't even have to write text to a file, but just call touch to create an empty file.

Delete existing/Create symbolic link from "avatar.gif" to the file you just selected.

Delete the current ".previous" file and create a new one with the name of the graphic you just linked to avatar.gif

Then use cron or whatever to schedule the script to run periodically. Shouldn't need any maintenance.
 

theanorak

Junior Member
Jul 31, 2006
5
0
0
No problem. My guess is this could be fairly easily accomplished with a shell script (although I've done pretty much zero shell scripting myself), and it's probably not a particularly complex script, so with luck and some headscratching, you'd be fine with an "intro to shell scripting" book/website/tutorial and a bit of hunt-the-command!
 

nd

Golden Member
Oct 9, 1999
1,690
0
0
Here's a trivial solution in Python.

#!/usr/bin/env python

import os, sys, random

if len(sys.argv) < 3:
print "usage: %s <directory> <target filename>" % sys.argv[0]
sys.exit(-1)

dir = sys.argv[1]
target = sys.argv[2]

# Get list of files in the directory
filelist = os.listdir(dir)

# Exclude certain files (script itself and the target filename)
exclude_files = [os.path.basename(sys.argv[0]), target]
for xname in exclude_files:
if filelist.count(xname) > 0:
filelist.remove(xname)

# Choose a random file
randnum = random.randint(0, len(filelist)-1)
randfile = filelist[randnum]
# Create a symbolic link from target to random file
try:
# Remove the existing one first
os.unlink(target)
except:
pass

try:
os.symlink(randfile, target)
print "Created symlink from %s -> %s" % (randfile, target)
except:
print "Error while creating symlink from %s -> %s" % (randfile, target)

The forums probably screwed up the indentation on that. I also pasted it here:

http://rafb.net/paste/results/jygkw448.html

All the script does is build a list of files in a given directory, excludes a couple, and selects one. It creates a symbolic link to the random file with a given target name.

Example usage:

$ ls
aksjd avatar.jpg fkasjdf iwej ljc random-select.py
alksjdf fkajsd ioewr kjas lksjdf
$ ./random-select.py
usage: ./random-select.py <directory> <target filename>
$ ./random-select.py . avatar.jpg
Created symlink from fkasjdf -> avatar.jpg
$ ./random-select.py . avatar.jpg
Created symlink from ljc -> avatar.jpg
$ ./random-select.py . avatar.jpg
Created symlink from ljc -> avatar.jpg
 

Netopia

Diamond Member
Oct 9, 1999
4,793
4
81
THAT is what I wanted! (or very close to it). I'm just a neophyte in scripting, but am about a third of the way through a rather rotund book I picked up. I'll look at your script more closely and see what I can learn.

One thought... is there a way, rather than having it remove certain files, have it only use certain files? For instance, only use *.jpg, *.gif, *.png but NOT use avatar.* ?

Thanks for the example and the encouragement!

Joe
 

nd

Golden Member
Oct 9, 1999
1,690
0
0
Originally posted by: Netopia
THAT is what I wanted! (or very close to it). I'm just a neophyte in scripting, but am about a third of the way through a rather rotund book I picked up. I'll look at your script more closely and see what I can learn.

One thought... is there a way, rather than having it remove certain files, have it only use certain files? For instance, only use *.jpg, *.gif, *.png but NOT use avatar.* ?

Thanks for the example and the encouragement!
Well, the script already excludes the target filename (which would be avatar.jpg, for example). If you wanted more control to exclude/include other things, I would change it so it takes a list of individual filenames rather than a directory name as an argument.

Then you could just call the Python script from a shell script using something like this:

#!/bin/sh
FILES=`find *.jpg *.gif *.png | grep -vE "avatar\..*"`
./random-select.py avatar.jpg $FILES

And that would do what you mentioned (include *.jpg *.gif *.png, exclude avatar.*), by passing the desired list of files to the python script. If it were me, I'd probably just make a global EXCLUDE_FILTER variable in the script and exclude those if it matches the filter.

For fun, here's a pure bash version, which is much uglier and harder to follow:

#!/bin/bash

# Determine how many files there are
NUM_FILES=`find *.jpg *.png *.gif | grep -vE "avatar\..*" | grep -nE . |
tail -n 1 | awk -F: '{print $1}'`

# Get a random number between 1 and the number of files
RAND_INT=`echo $((RANDOM%$NUM_FILES+1))`

# Get a random filename
RAND_FILE=`find *.jpg *.png *.gif | grep -vE "avatar\..*" | grep -nE . |
grep -E "^$RAND_INT:" | awk -F: '{print $2}'`

rm -f avatar.jpg
ln -s $RAND_FILE avatar.jpg

echo "Created link from $RAND_FILE to avatar.jpg"

 
sale-70-410-exam    | Exam-200-125-pdf    | we-sale-70-410-exam    | hot-sale-70-410-exam    | Latest-exam-700-603-Dumps    | Dumps-98-363-exams-date    | Certs-200-125-date    | Dumps-300-075-exams-date    | hot-sale-book-C8010-726-book    | Hot-Sale-200-310-Exam    | Exam-Description-200-310-dumps?    | hot-sale-book-200-125-book    | Latest-Updated-300-209-Exam    | Dumps-210-260-exams-date    | Download-200-125-Exam-PDF    | Exam-Description-300-101-dumps    | Certs-300-101-date    | Hot-Sale-300-075-Exam    | Latest-exam-200-125-Dumps    | Exam-Description-200-125-dumps    | Latest-Updated-300-075-Exam    | hot-sale-book-210-260-book    | Dumps-200-901-exams-date    | Certs-200-901-date    | Latest-exam-1Z0-062-Dumps    | Hot-Sale-1Z0-062-Exam    | Certs-CSSLP-date    | 100%-Pass-70-383-Exams    | Latest-JN0-360-real-exam-questions    | 100%-Pass-4A0-100-Real-Exam-Questions    | Dumps-300-135-exams-date    | Passed-200-105-Tech-Exams    | Latest-Updated-200-310-Exam    | Download-300-070-Exam-PDF    | Hot-Sale-JN0-360-Exam    | 100%-Pass-JN0-360-Exams    | 100%-Pass-JN0-360-Real-Exam-Questions    | Dumps-JN0-360-exams-date    | Exam-Description-1Z0-876-dumps    | Latest-exam-1Z0-876-Dumps    | Dumps-HPE0-Y53-exams-date    | 2017-Latest-HPE0-Y53-Exam    | 100%-Pass-HPE0-Y53-Real-Exam-Questions    | Pass-4A0-100-Exam    | Latest-4A0-100-Questions    | Dumps-98-365-exams-date    | 2017-Latest-98-365-Exam    | 100%-Pass-VCS-254-Exams    | 2017-Latest-VCS-273-Exam    | Dumps-200-355-exams-date    | 2017-Latest-300-320-Exam    | Pass-300-101-Exam    | 100%-Pass-300-115-Exams    |
http://www.portvapes.co.uk/    | http://www.portvapes.co.uk/    |