ID:96873
 
Keywords: development
I've revised an old script I wrote a while back for getting data from a hub page.

Let me put it into perspective for you, this is what two years of experience and practice does for you.

Old Script:
  • Only works for specific values.
  • Is actually broken and wont work under specific circumstances.
  • Only works for game hub pages.
  • Way more code then there needs to be.
  • Just sucks, really, it sucks.


New Script:
  • Works for all variables on a hub/members page, including newly added ones.
  • Should work for all pages that have available text modes.
  • Works on both members and game hub pages.
  • Aforementioned improvements require less code than the original script.
  • A way bigger improvement over the old.
  • A (hacky) method to make sure it doesn't attempt to parse pages that aren't text.


Just to give you a demonstration of how it works, he's the outputted array on a few pages:


And so on and so forth.

The only annoying part of the function is something I was struggling to figure out. I honestly couldn't find a way to check the Content-Type of the page with fopen() or file_get_contents() (if someone knows a way, fill me in!), so I've resorted to actually loading the page twice, one for it's headers, and if it's a text/plain page, continue on to get the content, otherwise return false.

So then, here's a PHP alternative to getting game info off the BYOND hub:
<?
echo "<pre>" . print_r(parse_hub("tiberath", "true", "long=true;num_posts=10"), TRUE) . "</pre>";

function parse_hub($hub, $member=FALSE, $additional = null) {
$url = "http://www.byond.com/" . ($member ? "members" : "hub") . "/" . $hub . ";format=text" . ($additional ? ";" . $additional : null);

$page = get_headers($url, TRUE);
if(strpos($page["Content-Type"], "text/plain") === false) return false;

$return_array = array(); // Array returned. Duh.
$page = file_get_contents($url);
$page = explode(chr(10), htmlentities($page, ENT_NOQUOTES));
$heading = "";
$subheading = "";

foreach($page as $p) {
if(!strpos($p, "=")) {
if(strpos($p, "/")) {
$p = explode("/", $p);
$heading = trim($p[0]);
$subheading = trim($p[1]);
} else {
$heading = trim($p);
unset($subheading);
}

} else {
$start = strpos($p, "=");
$a = substr($p, 0, $start);
$b = substr($p, $start + 2);

if(strpos($b, "list") === 0) {
$b = str_replace("\"", "", substr($b, 5, strlen($b) - 7));
$b = explode(",", $b);
if(!$b[0]) $b = null;
}

if($subheading) {
$return_array[$heading][$subheading][trim($a)] = ((strpos($b, "\"") === 0) ? substr($b, 1, strlen($b) - 3) : $b);
} else {
$return_array[$heading][trim($a)] = ((strpos($b, "\"") === 0) ? substr($b, 1, strlen($b) - 3) : $b);
}
}
}

return $return_array;
}
?>


Also note: Although the text-mode of the hub uses the same format as a plain-text BYOND Savefile. That function will fail miserably if it attempts to read one. I never designed it for use with a savefile, only for use with the BYOND Hub. If the hub is updated to include a directory inside a directory, then is when I'll have to make it completely compliant with a BYOND Savefile.

Also, also note: I'll be working on a function to read a BYOND Savefile (and thus put this one out of business), but I'm not entirely sure whether or not I'll release that one. Gotta keep some tricks for myself, you know.

Yeah, I was bored today.
Mah brain dumb.

Seriously though, not to be insulting in any way. What is the purpose of this creation?
Rasengan84 wrote:
Seriously though, not to be insulting in any way. What is the purpose of this creation?

I thought I was pretty clear with "Getting data from the hub".

How about this: Getting the text data from the text site of any BYOND Member or BYOND Game, and returning it into a useful array() in PHP for user's websites.
Tiberath wrote:
Rasengan84 wrote:
Seriously though, not to be insulting in any way. What is the purpose of this creation?

I thought I was pretty clear with "Getting data from the hub".

How about this: Getting the text data from the text site of any BYOND Member or BYOND Game, and returning it into a useful array() in PHP for user's websites.

Oh I see now. Thank you for dumbing it down for me.
I haven't played with PHP in a while. Pretty slick what you can do with it.
It had been a couple of weeks since I'd done something in it, and I figured re-releasing that would be a good way to stay sharp.
As a generosity, if you want the hub's long_desc to come out exactly as it is on the hub itself, I recommend running it through this function before displaying it:

    function parse_long_desc($t) {
$t = str_replace('\"', '"', $t);
$t = str_replace('\n', '', $t);
return html_entity_decode($t);
}


I don't know why the quotes are escaped in text mode, I honestly don't. =/