Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Photo Album HTML Adder

by DarkSniper (Initiate)
on Oct 08, 2002 at 09:44 UTC ( [id://203602]=sourcecode: print w/replies, xml ) Need Help??
Category: HTML Utility
Author/Contact Info
Description: I wrote this code a few months ago because i need to add photogalleries to my site. Instead of doing 15pages manual i let it do it by it self. improvements can be done,especially with update_menus() and the progress meters. It is a massive code i know but im kinda proud of it :). Comments?
#!/usr/bin/perl

use strict;

my $conf_file="/root/gallery.conf";
my $version="1.4 Rewritten";
my $count=0;
my $pics_root="/www/darksniper.net";
my $overwrite;

system("clear");

version();
file_tests();

sub main
{
        (my $current_image_set)=@_;
        print "We currently are at set [${current_image_set}]\n";
        print "Is this correct?\n";
        chomp(my $confirmation=<STDIN>);
        $_=$confirmation;
        print "\aEnter 'y' for yes and 'n' for no!\n" and chomp($_=<ST
+DIN>) until /[yn]/;
        $confirmation=$_;
        create_conf_file() if $confirmation eq "n";

        my $creating_set=$current_image_set+1;
        check_dupes($creating_set);
        print "OK,now some questions about the NEXT set..[$creating_se
+t]\n";
        print "How many images do you have in the set [$creating_set]?
+\n";
        chomp(my $number_of_images=<STDIN>);
        $_=$number_of_images;   # fix this
        print "\aNumbers only!\n" and chomp($_=<STDIN>) until /\d+/; #
+ and this
        print "\aMax 15 images per set!\n" and chomp($_=<STDIN>) until
+ $_ <= 15;
        $number_of_images=$_; # and this with tilde
        print "\n-Doing initial work-\n";
        print "Creating dir...";
        create_dirs($creating_set,$overwrite);
        print "DONE!\n";
        print "Creating ${creating_set}.html...";
        create_html($creating_set,$number_of_images);
        print "DONE!\n";
        print "Creating pics files...";
        create_pics_files($creating_set,$number_of_images);
        print "DONE!\n";
        print "Updating menus...";
        update_menus($current_image_set);
        print "DONE!\n";
        print "Changing axx rights...";
        change_rights();
        print "DONE!\n";
        print "Updating conf file...";
        update_conf($creating_set);
        print "DONE!\n";
        print "-Initial work OK-\n";
        sleep 1;
        print "Image set $creating_set...CREATED!!!\n";
        print "You may now upload the jpegs!\n";
        exit;
}

sub create_dirs
{
        (my $creating_set,$overwrite)=@_;
        rmdir  "$pics_root/pics/${creating_set}/" if $overwrite eq "ye
+s";
        unlink <$pics_root/pics/${creating_set}.html> if $overwrite eq
+ "yes";
        mkdir "$pics_root/pics/${creating_set}",0775;
}

sub check_dupes
{
        (my $creating_set)=@_;
        my $found;
        my $confirmation;

        print "\nChecking for dupes...";
        if (-e "$pics_root/pics/$creating_set.html" or -e "$pics_root/
+pics/$creating_set")
        {
                $found="true";
                print "FAILED!\n";
                print "[$creating_set] already exists!\n";
                print "Do you want to overwrite?\n";
                chomp($confirmation=<STDIN>);
                print "\aEnter 'y' for yes and 'n' for no!\n" and chom
+p($_=<STDIN>) until /[yn]$/;
                $confirmation=$_;
                create_conf_file() if $confirmation eq "n";
                $overwrite="yes" if $confirmation eq "y";
        }
        else
        {
                $found="false";
                print "OK!\n";
                $overwrite="no";
        }
        return $overwrite;
}

sub update_menus
{
        (my $current_image_set)=@_;
        my $set_before_current=$current_image_set - 1;
        open INDEX,"<$pics_root/pics/index.html" or die "FAILED!\n$!";
        open TEMP,">/tmp/temp.index_2" or print "Cannot open temp file
+!\n$!";
        while (<INDEX>)
        {
                print TEMP $_;
                if (/$set_before_current<\/a>/)
                {
                        print TEMP qq!<a href="s${current_image_set}.h
+tml">!;
                        print TEMP qq!$current_image_set</a>\n!;
                }
        }
        close TEMP;
        close INDEX;
        rename "$pics_root/pics/index.html","/tmp/delete.me.plz" or di
+e "Cannot rename index.html!
\n";
        rename "/tmp/temp.index_2" , "$pics_root/pics/index.html" or d
+ie "Cannot rename temp.index
_2 $!";
        unlink </tmp/delete.me.plz> or die "Can't unlink! $!";

        for (2..$current_image_set)
        {
                if ($_ < $current_image_set)
                {

                        open MENU,"<$pics_root/pics/s${_}.html" or pri
+nt "$!";
                        open TMP,">/tmp/s${_}.html" or print "$!";
                        while (<MENU>)
                        {
                                print TMP $_;
                                if (/$set_before_current<\/a>/)
                                {
                                        print TMP qq!<a href="s${curre
+nt_image_set}!;
                                        print TMP qq!">$current_image_
+set</a>\n!;
                                }
                                elsif (/$set_before_current<\/font><\/
+a>/)
                                {
                                        print TMP qq!<a href="s${curre
+nt_image_set}!;
                                        print TMP qq!">$current_image_
+set</a>\n!;
                                }
                close TMP;
                close MENU;
                rename "$pics_root/pics/s${_}.html","/tmp/buhaha${_}";
                rename "/tmp/s${_}.html","$pics_root/pics/s${_}.html";
                unlink </tmp/buhaha${_}>;
                        }
                }
        }
}

sub update_conf
{
        (my $creating_set)=@_;
        my @conf;

        open CONF,">$conf_file" or create_conf_file();
        @conf=<CONF>;
        close CONF;
        unshift @conf, "Image set=$creating_set\n";
        open CONF,">$conf_file" or create_conf_file();
        print CONF @conf;
        close CONF;
        return;
}

sub change_rights
{
        chmod 0755,"$pics_root/pics" or print "FAILED!\n" and exit;
        return;
}

sub update_menus
{
        (my $current_set, my $creating_set)=@_;
        open INDEX,"<$pics_root/pics/index.html" or die "index: $!";
        open TEMP,">/tmp/tmp_index" or die "tmp_index: $!";
        while (<INDEX>)
        {
                print TEMP $_;
                if (/$current_set<\/a>/)
                {
                        print TEMP qq#<a href="s${creating_set}.html>$
+creating_set</a>\n#
                }
        }
        close INDEX;
        close TEMP;
        rename "$pics_root/pics/index.html","/tmp/old_index" or die "$
+!";
        rename "/tmp/tmp_index","$pics_root/pics/index.html" or die "$
+!";
        unlink </tmp/old_index>;


}

sub create_pics_files
{
        $|=1;
        (my $creating_set,my $number_of_images)=@_;
        for (1..$number_of_images)
        {
        open IMAGE_FILE,">${pics_root}/pics/${creating_set}/s${creatin
+g_set}b${_}.html" or print "
FAILED!\n\n" and die "$!";
        select IMAGE_FILE;
        print qq#<html><head><title>Set $creating_set Pic $_</title></
+head>#;
        print qq#\n<body text="green" background="http://darks.mine.nu
+/bin.gif"#;
        print qq# bgproperties="fixed">#;
        print qq#\n<img src="s${creating_set}b${_}.jpg">\n<html>#;
        close IMAGE_FILE;
        select STDOUT;
        print ".";
        }
        $|=0;
}


sub create_html
{
        (my $creating_set,my $number_of_images)=@_;
        open SET_HTML,">$pics_root/pics/${creating_set}.html" or print
+ "FAILED!\n\n\n" and die "$!
";
        select SET_HTML;
        print qq#<html><head><title>Set ${creating_set}</title>\n#;
        print qq#<style type="text/css">\n#;
        print qq#<!--\n#;
        print qq#A:link { text-decoration: none; color: "green"; }\n#;
        print qq#A:visited { text-decoration: none; color: "green"; }\
+n#;
        print qq#A:active { text-decoration: none; color: "green"; }\n
+#;
        print qq#A:hover { text-decoration: none; color: "lightgreen";
+ }\n#;
        print qq#-->\n#;
        print qq#</style>\n#;
        print qq#<body background="http://darks.mine.nu/bin.gif">\n#;
        print qq#<center>\n#;
        print qq#<font face="Terminal" color="green">\n#;
        print qq#Gallery:\n#;
        print qq#<a href="index.html">1</a>\n#;

        for (2..$creating_set)
        {
                if ($_ == $creating_set)
                {
                        print qq#<a href="s${creating_set}.html">#;
                        print qq#<font color="red">$creating_set</font
+>#;
                        print qq#</a>\n#;
                }
                else
                {
                        print qq#<a href="s${_}.html">$_</a>\n#;
                }
        }

        print qq#</font>\n<table border=0>\n<tr>#;

        for (1..$number_of_images)
        {
                print qq#<td><a href="s${creating_set}/#;
                print qq#s${creating_set}b${_}.html">#;
                print qq#<img border=0 width=100 height=100 #;
                print qq#src="s${creating_set}/thumbs${creating_set}#;
                print qq#b${creating_set}.jpg"</a></td>\n#;
                $count++;
                if ($count == 3)
                {
                        print qq#</tr>\n#;
                        $count=0;
                }
        }
        print qq#</tr>\n</table>\n</center>\n</body>\n</html>#;
        close SET_HTML;
        select STDOUT;
}



sub create_conf_file
{
        my @conf;
        print "What image set are you CURRENTLY on?\n";
        chomp(my $image_set=<STDIN>);
        $_=$image_set;  # fix this
        print "\aNumbers only!\n" and chomp($_=<STDIN>) until /\d+/; #
+ and this
        $image_set=$_; # and this
        print "You inputed number $_\n"; # with tilde
        print "Is this correct?\n";
        chomp(my $confirmation=<STDIN>);
        $_=$confirmation;
        print "\aEnter 'y' for yes and 'n' for no!\n" and chomp($_=<ST
+DIN>) until /[yn]/;
        $confirmation=$_;
        create_conf_file() if $confirmation eq "n";
        open CONF,">$conf_file" or die "Cant open $conf_file: $!";
        print "You're currently at set $image_set\n";
        print CONF "Image set=",$image_set,"";
        close CONF;
        my $current_image_set=$image_set;
        main($current_image_set) if $confirmation eq "y";
}

sub check_conf_file
{
        if (-e $conf_file)
        {
                print "EXISTS\n";
                open CONF,"<$conf_file" or create_conf_file();
                print "-File tests OK-\n\n";
                my $conf=<CONF>;
                close CONF;
                my @temp_array=split /=/,$conf;
                my $current_image_set=$temp_array[1];
                main($current_image_set);
        }
        else
        {
                print "NOPE!\nBuilding one...\n";
                create_conf_file();
        }
}

sub file_tests
{
        print "\n-Doing initial file tests-\n";
        print "Check if the webdirectory exists...";
        if (-e "$pics_root")
        {
                print "EXISTS\n";
        }
        else
        {
                print "NOPE\n";
                die "Are you sure this is matrox?\n";
        }
        print "Check if index.html exists...";
        if (-e "$pics_root/pics/index.html")
        {
                print "EXISTS\n";
        }
        else
        {
                print "NOPE\n";
                die "Are you sure this is matrox?\n";
        }

print "Check if conf file exists...";
check_conf_file();
}

sub version
{
print qq!\t    ############################################\n!;
print qq!\t    # Gallery Maintainer $version - by DarkSniper #\n!;
print qq!\t    ############################################\n!;
}
Replies are listed 'Best First'.
Re: Photo Album HTML Adder
by DarkSniper (Initiate) on Oct 08, 2002 at 10:01 UTC
    humm the guys at perlmonks should fix their script to do the following in the code section: write
     and 
    at the start respectivly at the end of the code ${variable}; # is getting parsed print qq!text!; # is getting parsed as well... if you want the code for it i will add it to my site soon

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: sourcecode [id://203602]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (3)
As of 2024-04-19 05:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found