New OOTS products from CafePress
New OOTS t-shirts, ornaments, mugs, bags, and more
Results 1 to 5 of 5
  1. - Top - End - #1
    Pixie in the Playground
     
    Goblin

    Join Date
    Jul 2014

    Default OOTS Update Checker PHP script

    Hey there OOTS Fans!

    This is one of my favorite web comics, and I've got a link to it (along with a few other comics I like) on my homepage. Thing is, I got sick of checking for a new comic every day - some I know are Mon, Wed, Fri, others are every weekday, another is only Sunday, but OOTS updates whenever Rich feels like it - definitely not a knock on Rich, since that definitely gives OOTS a certain flavor (for example, the excitement I felt when it was updated like every other day for a week a while back).

    If only there were a way to make my website automatically check for a new OOTS comic when I load my homepage, and put a nice green "new" next to the link if there were. Well, I had this idea about a year ago, and finally got around to implementing it a week or so ago, and with the new update today, I was finally able to verify that I fixed the bug I had in it.

    And then I thought I ought to share it with you fine folks!

    Apologies if this looks fishy at all - first post and all (coulda sworn I'd posted here before - but it makes sense, any time I wanted to comment on a new comic, there were already five people saying exactly what I'd wanted to say), and posting random code on a forum - but honest, I'm just kinda happy I finally got this working how I want it, and maybe others will find it useful or helpful.

    So, there's two files, latest_oots.php and oots_redirect.php. In order to use it, toss the two files on your server and include latest_oots.php wherever you want the link to appear. Three text files will be generated when you run the script (or, load the page you threw the include in) - latest_oots_num.txt (contains the latest comic number, and has a timestamp equal to when the latest comic was found by the script), latest_oots_check.txt (has a modified timestamp equal to when the script last checked), and latest_oots_redirect.txt (which has a modified timestamp equal to when you were last redirected to the newest comic). After running the script, you'll want to prime it by putting the latest comic number in latest_oots_num.txt. Then, tweak to your hearts content!

    This is pretty specific to my own use case, where I'm effectively the only user of my website - instead of touch files, it could have been written to use cookies for multiple users... but my whole reason for having a site is that it's the same across all the different machines I use, so cookies would defeat the purpose. Also - apologies if this post should have gone in another sub-forum. Finally, this code was written by me and is released for any and all purposes, modification, if you can make money off it, or if only to mock the author (edit: by which I mean me, not Rich), feel free.

    Here's latest_oots.php (including some silly and [slightly] outdated comments and pseudocode at the top):


    <?php

    //have a file, say, latest_oots_num.txt
    //it'll just have a number

    //script will check if the latest_oots_num.txt is at least a day old
    //if it is, read it in, increment, and see if we get a 404 error when getting that url
    //if we don't get a 404 error, write out the new value, and touch a oots_is_new.txt
    //if we do get a 404 error, just touch the file to update the timestamp so we only check once a day
    //if oots_is_new.txt exists, say new - rather, if oots_is_new.txt is newer than oots_is_old.txt, say new
    //when oots is clicked, instead of linking to the oots page, link to a redirect that will also touch oots_is_old.txt
    //even better - compare the timestamp of latest_oots_num.txt to a last_redirect file
    //okay, read file latest_oots_num.txt
    ////////////////////

    //okay, rewrite simpler
    /*

    latest_oots_num mtimestamp is the last time a new comic was found
    latest_oots_check mtimestamp is the last time we checked for a new comic
    latest_oots_redirect mtimestamp is the last time oots was read

    if latest_oots_num is later than latest_oots_redirect
    set the new flag
    else

    if latest_oots_check was more than 24 hours ago
    check if there's a new comic
    if there's a new comic
    update the new comic num
    set the new flag
    else
    touch the check file

    if the new flag is set
    print the link to the redirect to the lastest_oots_num link and say new
    else
    print the link to the last oots

    redirect page will input the oots num, make sure it's numeric, touch the redirect file, and print a redirect
    */

    $debug = false;

    $new_flag = false;
    $latest_oots_num = 0;
    $latest_oots_num_file = "latest_oots_num.txt";
    $latest_oots_check_file = "latest_oots_check.txt";
    $latest_oots_redirect_file = "latest_oots_redirect.txt";
    if (file_exists($latest_oots_num_file)) {
    $latest_oots_num = file_get_contents($latest_oots_num_file);
    $latest_oots_num_mtime = filemtime($latest_oots_num_file);
    } else {
    file_put_contents($latest_oots_num_file, "0");
    $latest_oots_num = 0;
    $latest_oots_mtime = time();
    }

    $next_oots_num = $latest_oots_num + 1;

    while (strlen($next_oots_num) < 4) {
    $next_oots_num = "0".$next_oots_num;
    }
    while (strlen($latest_oots_num) < 4) {
    $latest_oots_num = "0".$latest_oots_num;
    }

    if (file_exists($latest_oots_redirect_file)) {
    $latest_oots_redirect_time = filemtime($latest_oots_redirect_file);
    } else {
    $latest_oots_redirect_time = 0;
    }

    if ($latest_oots_num_mtime > $latest_oots_redirect_time) {
    if ($debug) {
    echo "latest_oots_num_mtime: " . $latest_oots_num_mtime . " is greater than ";
    echo "latest_oots_redirect_time: " . $latest_oots_redirect_time;
    echo "debug - oots new found since latest redirect<br />";
    }
    $new_flag = true;
    } else {
    if (file_exists($latest_oots_check_file)) {
    $latest_oots_check_mtime = filemtime($latest_oots_check_file);
    } else {
    $latest_oots_check_mtime = 0;
    }
    $current = time();

    if ($current - $latest_oots_check_mtime > 43200) {//12 hours
    if ($debug) {
    echo "it's been more than 12 hours since i've checked, so I'm checking<br />";
    }
    touch($latest_oots_check_file);
    $next_oots_file = "http://www.giantitp.com/comics/oots".$next_oots_num.".html";
    if ($debug) {
    echo "looking for $next_oots_file<br />\n";
    }
    $next_oots_file_headers = @get_headers($next_oots_file);
    if($next_oots_file_headers[0] == 'HTTP/1.1 404 Not Found') {
    if ($debug) {
    echo "checked for a new one, but no luck.<br />";
    }
    }
    else {
    file_put_contents($latest_oots_num_file, $next_oots_num);
    $latest_oots_num = $next_oots_num;
    $new_flag = true;
    if ($debug) {
    echo "debug - just found ya a new one<br />\n";
    echo "oots num set to " . $latest_oots_num . "<br />\n";
    }
    }
    } else {
    if ($debug) {
    echo "it's been less than 12 hours since i've checked<br />";
    }
    }
    }

    if ($new_flag) {
    ?>
    <a href="oots_redirect.php?oots=<?php echo $latest_oots_num; ?>" target="_blank">oots</a> <font color=green>new</font>
    <small><small><small><small><a href="http://nathanedwardwilliams.com/fun/orderofthestick.htm"
    target="_blank">translations</a></small></small></small></small><br />
    <?php
    } else {
    ?>
    <a href="http://www.giantitp.com/comics/oots<?php echo $latest_oots_num; ?>.html" target="_blank">oots</a>
    <small><small><small><small><a href="http://nathanedwardwilliams.com/fun/orderofthestick.htm"
    target="_blank">translations</a></small></small></small></small><br />
    <?php
    }
    ?>



    ----------------------------
    And here's oots_redirect.php:

    <?php

    $oots_num = $HTTP_GET_VARS["oots"];
    if (strlen($oots_num) > 4 || !is_numeric($oots_num)) {
    ?>invalid oots value<?php
    } else {
    while (strlen($oots_num) < 4) {
    $oots_num = "0".$oots_num;
    }
    ?><meta http-equiv="refresh" content="0; url=http://www.giantitp.com/comics/oots<?php echo $oots_num; ?>.html" /><?php
    touch("latest_oots_redirect.txt");
    }

    ?>
    Last edited by mattholimeau; 2014-07-22 at 12:35 PM.

  2. - Top - End - #2
    Orc in the Playground
     
    BlueKnightGuy

    Join Date
    Feb 2012
    Gender
    Male

    Lightbulb Re: OOTS Update Checker PHP script

    Or you could just click here for the RSS feed, and be automatically notified when the strip updates.

    http://www.giantitp.com/comics/oots.rss
    How to turn off these annoying .sigs:

    1. Edit your profile options.
    2. Scroll down to "Visible Post Elements".
    3. Uncheck "Show Signatures".
    4. Save changes.
    5. Enjoy a much less cluttered and noisy forum.

  3. - Top - End - #3
    Pixie in the Playground
     
    Goblin

    Join Date
    Jul 2014

    Default Re: OOTS Update Checker PHP script

    Touche, indeed. Guess I never caught onto the RSS craze.

  4. - Top - End - #4
    Bugbear in the Playground
     
    rodneyAnonymous's Avatar

    Join Date
    Feb 2013
    Location
    empty space

    Default Re: OOTS Update Checker PHP script

    I have this new invention; I call it a "wheel".
    I like semicolons; they make me feel smart.

  5. - Top - End - #5
    Barbarian in the Playground
     
    CaDzilla's Avatar

    Join Date
    Aug 2013

    Default Re: OOTS Update Checker PHP script

    I am now reminded of the time Homer Simpson crossed a rickety bridge only to see a suspension bridge parallel from his position.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •