It started, as so many things do, with a tweet(bark?) from CogDog linking to The Best of ds106 GIF TV. This got me thinking of how to automate the gathering of gifs. I remembered that I had done some gif scraping a while back: Doug’s Gifs.
I wondered about scrapping tumblr in the same manner with a wee bit of php. This turns out to be quite simple: Gifs, Gifs
$url = "http://jjgifs.tumblr.com/rss";
$rss = file_get_contents($url);
$matches = array();
preg_match_all('!http://[^?#\s]+\.(gif)!', $rss,$matches);
I didn’t do the regExpression myself, I must have found it on the internet when I made the page of Doug’s gifs. Anyway at this point you have a list (php array) of urls to all of the gifs on the page. I’ve not worked out the best way to handle this, but currently I am writing it to a cache file and only updating it every now and again. The list is put into a JavaScript Array.
The page Gifs, Gifs, loops through a few ds106naughts tumblrs rather than just the one. It then displays a random gif, clicking on this will display another.
This is pretty much proof of concept rather than a well though out solution, but it works. There are a few interesting things that could be done structurally. Alan has already suggested hiving off the generation to a separate php file and a cron job. We could then have a plain html/javascript page for display. He also suggest a next and previous button(√). I am guessing that it might be better to have the list in a database?
Now I have a text file with a list of ds106 gifs, it is pretty simple to have a
ds106GifAPI! This following image should be a random DS106 gif:
The code used:
<img src="http://johnjohnston.info/oddsandends/ds106gif" alt="a random ds106 gif " />
This should give a random gif every time.
THe DS106 Gif API
Imagine you are blogging (or making any webpage) and want an exciting gif to go with your post, but you don’t just want any old gif, you want a random DS106 gif. Safe for work but not for your mind. Now you casn, simply use http://johnjohnston.info/oddsandends/ds106gif as an image url and you will get a random gif every time the page loads.
Given that my gif store is a list of gif urls the code is simple:
<?php
$cachefile = "Path/To/The/Cache/File.txt";
$gifs = file_get_contents($cachefile);
$gifArray=explode("\n",$gifs);
$thisgif=$gifArray[rand(0,count($gifArray))];
header('Location: '.$thisgif);
?>
I very much like you for making this- as much for the concept as the thing itself. I plan to make use of it in the near future.
Thanks Tom,
Now got this working with a database, updated by a scraper scrip and a sort of api that provides gif urls and lists of urls in json format. As ever no error checking.