{"id":1346,"date":"2014-03-06T22:49:49","date_gmt":"2014-03-06T22:49:49","guid":{"rendered":"http:\/\/johnjohnston.info\/106\/?p=1346"},"modified":"2014-03-07T07:51:25","modified_gmt":"2014-03-07T07:51:25","slug":"gif-scraping-and-a-ds106-gif-api","status":"publish","type":"post","link":"https:\/\/johnjohnston.info\/106\/gif-scraping-and-a-ds106-gif-api\/","title":{"rendered":"Gif Scraping and a DS106 Gif API"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\"  src=\"https:\/\/johnjohnston.info\/106\/wp-content\/uploads\/2013\/03\/wecodejam_econo.gif\" alt=\" \" \/>\n\t<\/p>\n<p>It started, as so many things do, with a <a href=\"https:\/\/twitter.com\/cogdog\/status\/440778537784852480\">tweet<\/a>(bark?) from <a href=\"http:\/\/cogdogblog.com\">CogDog<\/a> linking to <a href=\"http:\/\/lab.cogdogblog.com\/106giftv\/\">The Best of ds106 GIF TV<\/a>. This got me thinking of how to automate the gathering of gifs. I remembered that I had done some gif scraping a while back: <a href=\"http:\/\/johnjohnston.info\/tests\/dougsgifs.php\">Doug&#8217;s Gifs<\/a>.<\/p>\n<p>I wondered about scrapping tumblr in the same manner with a wee bit of php. This turns out to be quite simple: <a href=\"http:\/\/johnjohnston.info\/oddsandends\/randomgif.php\">Gifs, Gifs<\/a><\/p>\n<p><code><br \/>\n$url = \"http:\/\/jjgifs.tumblr.com\/rss\";<br \/>\n<br \/>$rss = file_get_contents($url);<br \/>\n$matches = array();<br \/>\npreg_match_all('!http:\/\/[^?#\\s]+\\.(gif)!', $rss,$matches);<br \/>\n<\/code><\/p>\n<p>I didn&#8217;t do the regExpression myself, I must have found it on the internet when I made the page of Doug&#8217;s gifs. Anyway at this point you have a list (php array) of urls to all of the gifs on the page. I&#8217;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.<\/p>\n<p>The page <a href=\"http:\/\/johnjohnston.info\/oddsandends\/randomgif.php\">Gifs, Gifs<\/a>, loops through a few ds106naughts tumblrs rather than just the one. It then displays a random gif, clicking on this will display another.<\/p>\n<p>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(&radic;). I am guessing that it might be better to have the list in a database?<\/p>\n<p>Now I have a text file with a list of ds106 gifs, it is pretty simple to have a<br \/>\n\t ds106GifAPI! This following image should be a random DS106 gif:<\/p>\n<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/johnjohnston.info\/oddsandends\/ds106gif\" alt=\"a random ds106 gif \" style=\"max-width:600px\"\/><\/p>\n<p>The code used:<\/p>\n<p><code><br \/>\n\t&lt;img src=&quot;http:\/\/johnjohnston.info\/oddsandends\/ds106gif&quot; alt=&quot;a random ds106 gif &quot; \/&gt;<\/code><\/p>\n<p>This should give a random gif every time.<\/p>\n<h2>THe DS106 Gif API<\/h2>\n<p>Imagine you are blogging (or making any webpage) and want an exciting gif to go with your post, but you don&#8217;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 <em>http:\/\/johnjohnston.info\/oddsandends\/ds106gif<\/em> as an image url and you will get a random gif every time the page loads.<\/p>\n<p><p>Given that my gif store is a list of gif urls the code is simple:<\/p>\n<pre><code>\r\n&lt;?php\r\n$cachefile = &quot;Path\/To\/The\/Cache\/File.txt&quot;;\r\n$gifs = file_get_contents($cachefile); \r\n$gifArray=explode(&quot;\\n&quot;,$gifs);\r\n$thisgif=$gifArray[rand(0,count($gifArray))];\r\nheader(&#x27;Location: &#x27;.$thisgif);\r\n?&gt;<\/code><\/pre>\n<p><a href=\"http:\/\/johnjohnston.info\/oddsandends\/ds106gif\/ds106gifapi.html\">DS106 Random Gif API<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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&#8217;s Gifs. I wondered about scrapping tumblr in the same manner &hellip; <a href=\"https:\/\/johnjohnston.info\/106\/gif-scraping-and-a-ds106-gif-api\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Gif Scraping and a DS106 Gif API&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[74],"tags":[153,77,43],"class_list":["post-1346","post","type-post","status-publish","format-standard","hentry","category-animatedgif","tag-fun","tag-javascript","tag-php"],"better_featured_image":null,"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p3RLlC-lI","_links":{"self":[{"href":"https:\/\/johnjohnston.info\/106\/wp-json\/wp\/v2\/posts\/1346","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/johnjohnston.info\/106\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/johnjohnston.info\/106\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/johnjohnston.info\/106\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/johnjohnston.info\/106\/wp-json\/wp\/v2\/comments?post=1346"}],"version-history":[{"count":6,"href":"https:\/\/johnjohnston.info\/106\/wp-json\/wp\/v2\/posts\/1346\/revisions"}],"predecessor-version":[{"id":1353,"href":"https:\/\/johnjohnston.info\/106\/wp-json\/wp\/v2\/posts\/1346\/revisions\/1353"}],"wp:attachment":[{"href":"https:\/\/johnjohnston.info\/106\/wp-json\/wp\/v2\/media?parent=1346"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/johnjohnston.info\/106\/wp-json\/wp\/v2\/categories?post=1346"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/johnjohnston.info\/106\/wp-json\/wp\/v2\/tags?post=1346"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}