Page 1 of 5

Some sample guild export code

Posted: Thu Jan 27, 2005 12:24 pm
by Rollie
Here is some sample code to get people a little jump start in utilizing the guild export feature. Feel free to use as you like. You should be able to utilize this fairly easily. Just make sure to update the local directory where files should be stored/read and update your guildid and viola, it should work!

Code: Select all

<?php
    //
    //  Rostertest.php
    //
    //  Sample guild export downloader and display
    //  You may use this script as you wish.  This is only some sample code
    //  provided to help get a jumpstart!
    //
    //  Created&#58; 1/27/2005   
    //
    //  Author&#58;  Cooper Sellers aka Rollie - Bloodscalp
    //

    $local_directory = "/";     //  this is the directory where your local files
                                //  will be written to and read from.  Make sure
                                //  you have WRITE priveledges on this directory

    $guild_id        = 33962;   //  get this number from the link posted on the 
                                //  guilddisplay.php page
                                


    // 
    //  Remember to check the status file so that you are not pulling data
    //  more than once per day
    //
    $localstatusfile = $local_directory . "status.txt";
    $infile = fopen &#40;$localstatusfile, "r"&#41;; 
    $current_timestamp = 0;
    if &#40;!$infile&#41; 
    &#123; 
        echo "<p>No status file available, assuming this is the first run<br>"; 
    &#125;
    else
    &#123;
        //  read our status file time
        $buffer = fgets&#40;$infile, 4096&#41;;

        $current_timestamp = trim&#40; $buffer &#41;;

        echo 'Local status file reads &#58; ' . strftime&#40;"%m/%d/%y %H&#58;%M&#58;%S",$current_timestamp&#41; . '<br>';
    &#125;
    fclose&#40; $infile &#41;;         //  close our local status file
    
    $filename = "http&#58;//www.warcraftrealms.com/exports/status.txt";
    $infile = fopen &#40;$filename, "r"&#41;;   // open remote status file
    if &#40;!$infile&#41; 
    &#123; 
        echo "<p>Unable to open status file.<br>"; 
        exit; 
    &#125;

    $remote_timestamp = 0;
    if&#40;!feof &#40;$infile&#41;&#41;   // only 1 read should be needed for the status file
    &#123;
        $buffer = fgets&#40;$infile, 4096&#41;;
        $remote_timestamp = trim&#40; $buffer &#41;;

        echo 'Remote status file reads &#58; ' . strftime&#40;"%m/%d/%y %H&#58;%M&#58;%S",$remote_timestamp&#41; . '<br>';

    &#125;
    fclose&#40; $infile &#41;;  //  close the remote status file

    if&#40; $remote_timestamp - $current_timestamp > 86400 &#41; //  1 day = 60*60*24
    &#123;
        //
        //  We can do a full get
        //

        //  write our new status file
        $outfilename = $local_directory . "status.txt";
        $outfile = fopen&#40;$outfilename, "w"&#41;;
        if&#40; !$outfile &#41;
        &#123;
            echo "<p>Unable to open save file => " . $outfilename . "<br>";
            exit;
        &#125;

        fputs&#40;$outfile, $buffer&#41;;
        fclose&#40;$outfile&#41;;

        //
        //  Now get our guild roster file
        //
        $filename = 'http&#58;//www.warcraftrealms.com/exports/guildexport.php?guildid=' . $guild_id;
        $infile = fopen &#40;$filename, "r"&#41;; 
        if &#40;!$infile&#41; 
        &#123; 
            echo "<p>Unable to open remote file.<br>\n"; 
            exit; 
        &#125;

        $outfilename = $local_directory . "guildroster.csv";
        $outfile = fopen&#40;$outfilename, "w"&#41;;
        if&#40; !$outfile &#41;
        &#123;
            echo "<p>Unable to open save file => " . $outfilename . "<br>\n";
            exit;
        &#125;

        while &#40;!feof &#40;$infile&#41;&#41; 
        &#123;
            $buffer = fgets&#40;$infile, 4096&#41;;
            fputs&#40;$outfile, $buffer&#41;;
        &#125;

        fclose&#40;$outfile&#41;;
        fclose&#40;$infile&#41;; 
    &#125;



    //
    //  Now let's just output our roster as it's given
    //
    $filename = $local_directory . "guildroster.csv";
    $infile = fopen &#40;$filename, "r"&#41;; 
    if &#40;!$infile&#41; 
    &#123; 
        echo "<p>Unable to open local roster file.<br>"; 
        exit; 
    &#125;

    //  do one read to get the header
    $buffer = fgets&#40;$infile, 4096&#41;;

    //  read the entries
    echo '<table><tr><th>Name</th><th>Race</th><th>Class</th><th>Level</th><th>Last Seen</th><th>Rank</th></tr>';
    while &#40;!feof &#40;$infile&#41;&#41; 
    &#123;
        $buffer = fgets&#40;$infile, 4096&#41;;
        list&#40; $name, $race, $class, $level, $last_seen, $rank &#41; = explode&#40;",",$buffer&#41;; 

        echo '<tr><td>' . $name . '</td><td>' . $race . '</td><td>' . $class . '</td><td>' . $level . '</td><td>' . $last_seen . '</td><td>' . $rank . '</td></tr>';
    &#125;
    echo '</table>';

    //  don't forget our credit link =&#41;

    echo "Guild data provided by <a href='http&#58;//www.warcraftrealms.com/'>WarcraftRealms.com</a>.";
?>
Feel free to post questions or suggestions! And please post anything you did to make it work better for you! It might just help others too!

Posted: Thu Jan 27, 2005 12:27 pm
by Rollie
You can see this script in action here:

http://www.warcraftrealms.com/rostertest.php

This is for the guild Absynthe on Dragonblight:

http://www.warcraftrealms.com/guilddisp ... ildid=6231

Posted: Thu Jan 27, 2005 4:45 pm
by Greytusks
I greatly appreciate the jumpstart it gives me. I have been out of touch with programming for a while now. With my basics in PHP I should be able to utilize this exemple in setting it up for my guild site.

I will make sure to credit warcreaftrealms for the help!

Posted: Thu Jan 27, 2005 5:15 pm
by Rollie
When you get it up and running, drop a link so I can check out your handywork!

Posted: Thu Jan 27, 2005 5:42 pm
by Greytusks
Don't have time to setup a better looking presentation for tonight, but it works great!

http://ironwolves.vempyre.net/archives/ ... ist_fr.php

I had problems getting the $local_directory variable to point to the right folder with the proper autorisations so I just used "./" to make it easier for me.

I included the census.php (which contains your code) into my members.php page so I can easily isolate the code when/if I have to change it.

When I get more time on my hands I will add css information to the code so it displays the information the way I want it to. (which makes me remember I still have to split my various web interface elements into separate modules for an easier time in creating new pages in the future without having to redo all pages each time I change a link in my menus!)

Big thanks again, just looking at your code made me realize I had forgotten so much about php ... I will have to relearn that I guess, in time.

Adding WarcraftRealms link to my main page. Well worth it.

Edit : removed as many spelling errors as I could find on short notice.

Posted: Thu Jan 27, 2005 6:38 pm
by Rollie
Glad to see someone is getting use out of the exports! Lots of folks are downloading, but I haven't seen anyone putting it to any use yet, hehe!

Very cool!

Posted: Sat Jan 29, 2005 5:55 pm
by Shade
I start working on a better template for it utilizing images etc :)

Posted: Tue Feb 01, 2005 12:42 pm
by Greytusks
I plan to modify (when I finally stop playing WoW a bit and fix my site instead, hehe) your base code to replace the echo to the page by code that puts the data into a local guild database, which would let ppl view the data a bit like on WarcraftRealms by letting them sort the data by the various fields.

Shade : by images you mean like on the realm where races and classes have their avatar showing?

Related question : race shows as unknown for all members which havn't been on lately (or just havn't been on while a census took place), what causes this?

Posted: Tue Feb 01, 2005 12:59 pm
by Rollie
Race comes up as unknown if the player has only been seen as an offline guild member. The guild info does not tell you the race, only the class, so I have no way of knowing what race that character is until they are seen by an actualy census.

Posted: Tue Feb 01, 2005 3:26 pm
by Greytusks
Somebody asked the same question already, but I can't seem to find the thread (I know, there isnt that many threads, hehe).

Was there a way to purge members who have been kicked out from the guild list taken with census+ from the WarcraftRealms data?

Or will those appear on the roster until they finally login ( maybe never for deleted characters ) and a census is taken while they are online?

If that's the case, I will certainly want to update my site so it imports the guild data into a database, and include a "black list" of names in the script so specified "black list" of names wont get imported into the database (for known characters who are no longuer played and no longuer part of the guild but still shown as members on WarcraftRealms)

Makes me think, I could easily do the same without the database fonction .. by comaparing each entry form the csv file to the black list and if the name appears on the black list it wont be echoed by the script to print on the web page ..

mmm yes .. would do while I get more time to code the database part ...

will try to put that here as soon as I'm done with it. Bugs me to see non-members on my roster.

I guess there is an easy way to use the while fonction in an internal loop inside the main loop that gets each member name from the csv and compare it to each name on the black list and if its any of those it wont be printed to the page ?

I definitively need to redo some reading on those php programming sites :)

Edit : I wish I hadnt put the the garbage my book of notes about php programming ... :(

Posted: Tue Feb 01, 2005 4:27 pm
by Greytusks

Code: Select all

<?php
    //
    //  Rostertest.php
    //
    //  Sample guild export downloader and display
    //  You may use this script as you wish.  This is only some sample code
    //  provided to help get a jumpstart!
    //
    //  Created&#58; february 1st 2005   
    //
    //  Author&#58;  Cooper Sellers aka Rollie - Bloodscalp
    //  Modifications&#58; Martin Beauvais aka Greytusks - Aggramar
    //
    //  Trying to add a blacklist function to remove unwanted members from the list
    //

    $local_directory = "./";     //  this is the directory where your local files
                                //  will be written to and read from.  Make sure
                                //  you have WRITE priveledges on this directory

    $guild_id        = 33962;   //  get this number from the link posted on the
                                //  guilddisplay.php page
                               


    //
    //  Remember to check the status file so that you are not pulling data
    //  more than once per day
    //
    $localstatusfile = $local_directory . "status.txt";
    $infile = fopen &#40;$localstatusfile, "r"&#41;;
    $current_timestamp = 0;
    if &#40;!$infile&#41;
    &#123;
        echo "<p>No status file available, assuming this is the first run<br>";
    &#125;
    else
    &#123;
        //  read our status file time
        $buffer = fgets&#40;$infile, 4096&#41;;

        $current_timestamp = trim&#40; $buffer &#41;;

        echo 'Local status file reads &#58; ' . strftime&#40;"%m/%d/%y %H&#58;%M&#58;%S",$current_timestamp&#41; . '<br>';
    &#125;
    fclose&#40; $infile &#41;;         //  close our local status file
   
    $filename = "http&#58;//www.warcraftrealms.com/exports/status.txt";
    $infile = fopen &#40;$filename, "r"&#41;;   // open remote status file
    if &#40;!$infile&#41;
    &#123;
        echo "<p>Unable to open status file.<br>";
        exit;
    &#125;

    $remote_timestamp = 0;
    if&#40;!feof &#40;$infile&#41;&#41;   // only 1 read should be needed for the status file
    &#123;
        $buffer = fgets&#40;$infile, 4096&#41;;
        $remote_timestamp = trim&#40; $buffer &#41;;

        echo 'Remote status file reads &#58; ' . strftime&#40;"%m/%d/%y %H&#58;%M&#58;%S",$remote_timestamp&#41; . '<br>';

    &#125;
    fclose&#40; $infile &#41;;  //  close the remote status file

    if&#40; $remote_timestamp - $current_timestamp > 86400 &#41; //  1 day = 60*60*24
    &#123;
        //
        //  We can do a full get
        //

        //  write our new status file
        $outfilename = $local_directory . "status.txt";
        $outfile = fopen&#40;$outfilename, "w"&#41;;
        if&#40; !$outfile &#41;
        &#123;
            echo "<p>Unable to open save file => " . $outfilename . "<br>";
            exit;
        &#125;

        fputs&#40;$outfile, $buffer&#41;;
        fclose&#40;$outfile&#41;;

        //
        //  Now get our guild roster file
        //
        $filename = 'http&#58;//www.warcraftrealms.com/exports/guildexport.php?guildid=' . $guild_id;
        $infile = fopen &#40;$filename, "r"&#41;;
        if &#40;!$infile&#41;
        &#123;
            echo "<p>Unable to open remote file.<br>\n";
            exit;
        &#125;

        $outfilename = $local_directory . "guildroster.csv";
        $outfile = fopen&#40;$outfilename, "w"&#41;;
        if&#40; !$outfile &#41;
        &#123;
            echo "<p>Unable to open save file => " . $outfilename . "<br>\n";
            exit;
        &#125;

        while &#40;!feof &#40;$infile&#41;&#41;
        &#123;
            $buffer = fgets&#40;$infile, 4096&#41;;
            fputs&#40;$outfile, $buffer&#41;;
        &#125;

        fclose&#40;$outfile&#41;;
        fclose&#40;$infile&#41;;
    &#125;



    //
    //  Now let's just output our roster as it's given
    //
    $filename = $local_directory . "guildroster.csv";
    $infile = fopen &#40;$filename, "r"&#41;;

    $blacklist = $local_directory . "blacklist.txt";
    $blackinfile = fopen &#40;$blacklist, "r"&#41;;

    if &#40;!$infile&#41;
    &#123;
        echo "<p>Unable to open local roster file.<br>";
        exit;
    &#125;

    if &#40;!$blackinfile&#41;
    &#123;
        echo "<p>Unable to open black list file.<br>";
        exit;
    &#125;

    //  do one read to get the header
    $buffer = fgets&#40;$infile, 4096&#41;;
    $blackbuffer = fgets&#40;$blackinfile, 4096&#41;;

    //  read the entries
    echo '<table><tr><th>Name</th><th>Race</th><th>Class</th><th>Level</th><th>Last Seen</th><th>Rank</th></tr>';
    while &#40;!feof &#40;$infile&#41;&#41;
    &#123;
        $buffer = fgets&#40;$infile, 4096&#41;;
        list&#40; $name, $race, $class, $level, $last_seen, $rank &#41; = explode&#40;",",$buffer&#41;;

	while &#40;!feof &#40;$blackinfile&#41;&#41; // trying to check each name and print it if not on blacklist and print blacklist if its on for dubugging purposes
	&#123;
	     $blackbuffer = fgets&#40;$blackinfile, 4096&#41;;
             list&#40; $blackname &#41; = explode&#40;",",$blackbuffer&#41;;
	     
	     if &#40; $name != $blackname &#41;
	     &#123;
		echo '<tr><td>' . $name . '</td><td>' . $race . '</td><td>' . $class . '</td><td>' . $level . '</td><td>' . $last_seen . '</td><td>' . $rank . '</td></tr>';
	     &#125;
	     elseif &#40; $name == $blackname &#41;
	     &#123;
		echo '<tr><td><b><i>blacklist</i></b><td></tr>';
	     &#125;
	&#125;


    &#125;
    echo '</table>';

    //  don't forget our credit link =&#41;

    echo "Guild data provided by <a href='http&#58;//www.warcraftrealms.com/'>WarcraftRealms.com</a>.<br>Hugue Thanks to Rollie for starting this script!";
?>
Ok you can see its result : http://ironwolves.vempyre.net/members/census2.php

And compare it to Rollie's posted script : http://ironwolves.vempyre.net/members/census.php

MY skills are rusty, why does it stop after the first name? A problem with the nested while functions?

Posted: Tue Feb 01, 2005 4:41 pm
by Rollie
From first glance, it would appear that the problem is that when you read the first character from the infile, you then read through the entire blacklist file to the end. Now

Code: Select all

while &#40;!feof &#40;$blackinfile&#41;&#41; 
will always return false so you will not output more than the first group.

The best approach to do what you are trying is to first open your blacklist file and read in all the characters to a list, then read the roster file and check to see if each name is in the list of blacklisted characters.

Are the blacklisted names characters that are no longer in the guild but they still show up on the site as being in the guild?

Posted: Tue Feb 01, 2005 4:51 pm
by Greytusks
Rollie wrote:Are the blacklisted names characters that are no longer in the guild but they still show up on the site as being in the guild?
exact

And good suggestion, thanks

edit :

Code: Select all

    //  do one read to get the header
    $buffer = fgets&#40;$infile, 4096&#41;;
    $blackbuffer = fgets&#40;$blackinfile, 4096&#41;;

    // trying to put unwanted names in a list
    while &#40;!feof &#40;$blackinfile&#41;&#41;
    &#123;
         $blackbuffer = fgets&#40;$blackinfile, 4096&#41;;
         list&#40; $blackname &#41; = explode&#40;",",$blackbuffer&#41;;
    &#125;

    //  read the entries from the csv files
    echo '<table><tr><th>Name</th><th>Race</th><th>Class</th><th>Level</th><th>Last Seen</th><th>Rank</th></tr>';
    while &#40;!feof &#40;$infile&#41;&#41;
    &#123;
        $buffer = fgets&#40;$infile, 4096&#41;;
        list&#40; $name, $race, $class, $level, $last_seen, $rank &#41; = explode&#40;",",$buffer&#41;;

    // comparing to the unwanted names
        if &#40; $name != $blackname &#41;
        &#123;
        echo '<tr><td>' . $name . '</td><td>' . $race . '</td><td>' . $class . '</td><td>' . $level . '</td><td>' . $last_seen . '</td><td>' . $rank . '</td></tr>';
        &#125;
        elseif &#40; $name == $blackname &#41;
        &#123;
        echo '<tr><td><b><i>blacklist</i></b><td></tr>';
        &#125;
    &#125;
    echo '</table>';
This almost works, exept only the last name of the blacklist is ever considered for the comparisons ... (does it show I barely remember how these functions work? hehe)

if you check the above census2.php link, you will now see one name is listed as blacklist. The blacklist.txt contained 4 names, and the only one that got blacklisted was the last. Thanks for your help on this, helps me refresh my skills !

Posted: Tue Feb 01, 2005 5:06 pm
by Rollie
Ah, okay, hopefully this will be resolved soon. I had something in place to fix this but it ended up causing other problems. I'll get back to it and see what I can do.

Posted: Sat Feb 05, 2005 6:33 am
by Khael
Harro Rollie,

Great site you got here. I am really happy with the convenience it lends me and other guild webmasters ;)

My code basicly looks like this:

Code: Select all

$csv = file_get_contents&#40;"http&#58;//www.warcraftrealms.com/exports/guildexport.php?guildid=52794"&#41;;
$csv = explode&#40;"\n",$csv&#41;;
So now I have an array with each char in my guild, and I use that for whatever I want to do. It is going to be on our webpage - looking something like this:

http://www.guildofsun.com/Web/Site/Games

Posted: Sat Feb 05, 2005 10:45 am
by Rollie
You need to download the file locally and parse it on your end instead of pulling the file each time the page is viewed.

From the exports page:

#5 You must code your web site or application to only download the CSV file when there is a new one available. There is no advantage to be gained from downloading every time you need to access the data and it will keep our bandwidth usage figures down. You can use the status files (see below) to find out if a new version of the file is available.
There is also a status file available, that allows you to get the date when the files were last generated (in UNIX timestamp format):

http://www.warcraftrealms.com/exports/status.txt
Essentially, I would much rather you to only pull a version once a day instead of pulling a version each time the page is accessed.

Your site looks great though!

Posted: Sat Feb 05, 2005 1:10 pm
by Guest
Im running a cron script that fetches this every hour, is that ok?

Its not that much bandwidth once per hour =P

Posted: Sat Feb 05, 2005 1:12 pm
by Khael
Oops that was me. I think you update per hour, and as far as I can see, you only save chars that are in game when you export.

So if I run this only once per day, I would miss out on alot of char updates. If you insist, I will check the date in that date file you produce of course, but this will only save minimal bandwidth, and only if you do not update every hour.

Posted: Sat Feb 05, 2005 6:35 pm
by Rollie
The database information is updated once per hour.

You wouldn't be losing out on any information if you only pulled the file once per day. You get a full listing every time. It would only matter if you had a dynamically changing guild roster (by the hour). I would bet that your guild roster doesn't change that often though.

If you are running a cron job, then it would be a cinch to set it once per day instead of once per hour. Thanks =)

Granted, guild exports are not that big, but it is still unnecessarily spent bandwidth and bandwidth isn't cheap =)

Posted: Sat Feb 05, 2005 6:46 pm
by Khael
Ok Rollie, I will take your word for it and change the cron job to run daily instead of course :D

A guildmate told me you are using CensusPlus addon for WoW that does a /who S U N request before export and therefore doesn't list any offline members. Is this entirely wrong?

The reason I am saying this is because the list I have currently at http://www.guildofsun.com/Web/Site/Games has not got all our chars in it, only the ones that have played recently.