Source of file PicasaRandomImage.php
Size: 9,690 Bytes - Last Modified: 2021-12-23T10:46:02+00:00
/var/www/docs.ssmods.com/process/src/code/model/PicasaRandomImage.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290 | <?php /** * adds pictures from your google picasa account to a data object for random retrieval. *@author nicolaas[at]sunnysideup.co.nz * example link: https://picasaweb.google.com/data/feed/api/user/xxx * example link: https://picasaweb.google.com/data/feed/api/user/nfrancken/albumid/5742357499437955281xxxx/ * large image: https://lh4.googleusercontent.com/-zs8sog5SG0U/T7DzuF3Lw9I/AAAAAAAAP4E/rPZiGlVL_eY/s2500/IMG_1715.JPG * (Note 2500 as the width!) * * **/ class PicasaRandomImage extends DataObject { private static $db = array( "URL" => "Text", "DoNotUse" => "Boolean", "Selected" => "Boolean" ); /** * google username e.g. firstname.lastname (if your email address is firstname.lastname@google.com) * @var string */ private static $google_username = ''; /** * set to 30 to take one in thirty albums * set to 1 to take all * @var Int */ private static $number_of_folders = 22; /** * set to 30 to take one in thirty pictures * set to 1 to add all * @var Int */ private static $number_of_images_per_folder = 7; public static function get_random_image($width) { $objects = PicasaRandomImage::get()->filter( array("DoNotUse" => 0) ) ->sort("RAND()"); if ($objects && $obj = $objects->First()) { $obj->URL = str_replace('/s72/', '/s'.$width.'/', $obj->URL); return $obj; } } public function requireDefaultRecords() { parent::requireDefaultRecords(); if (isset($_GET["updatepicassapics"])) { $albums = $this->getAlbums(PicasaRandomImage::$google_username); if (is_array($albums) && count($albums)) { $selectedAlbums = array_rand($albums, (count($albums <= self::get_number_of_folders()) ? self::get_number_of_folders() : count($albums))); if (!is_array($selectedAlbums)) { $selectedAlbums = array($selectedAlbums); } foreach ($selectedAlbums as $albumKey) { $albumTitle = $albums[$albumKey]; //google wants only the letters and numbers in the url $albumTitle = preg_replace("[^A-Za-z0-9]", "", $albumTitle); //get the list of pictures from the album $pictures = $this->showAlbumContent(PicasaRandomImage::$google_username, $albumTitle); if (is_array($pictures) && count($pictures)) { $selectedPictures = array_rand($pictures, (count($pictures <= self::get_number_of_images_per_folder()) ? self::get_number_of_images_per_folder() : count($pictures))); //get a random picture from the album if (!is_array($selectedPictures)) { $selectedPictures = array($selectedPictures); } foreach ($selectedPictures as $pictureKey) { $picture = $pictures[$pictureKey]; $url = $picture["src"]; if ($obj = PicasaRandomImage::get()->filter(array("URL" => $url))->first()) { //do nothing } else { $obj = new PicasaRandomImage(); $obj->URL = $url; $obj->write(); DB::alteration_message("adding picasa random image: ".$obj->URL."<img src=\"$url\" alt=\"\">", "created"); } } } } } } } //GET THE REMOTE FILE INTO A VARIABLE protected function curlit($url) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $return=curl_exec($ch); curl_close($ch); return $return; } //THIS IS BASED ON http://blogoscoped.com/archive/2007-03-22-n84.html protected function getAlbums($userId) { $tmp = array(); $url = 'http://picasaweb.google.com/data/feed/api/user/' . urlencode($userId) . '?kind=album'; $xml = $this->curlit($url); $xml = str_replace("xmlns='http://www.w3.org/2005/Atom'", '', $xml); $dom = new domdocument; if ($xml) { $dom->loadXml($xml); $xpath = new domxpath($dom); $nodes = $xpath->query('//entry'); foreach ($nodes as $node) { $rights = $xpath->query('rights', $node)->item(0)->textContent; if ($rights == "public") { $tmp[] = $xpath->query('title', $node)->item(0)->textContent; } } } return $tmp; } //THIS IS BASED ON http://blogoscoped.com/archive/2007-03-22-n84.html protected function showAlbumContent($userId, $albumName) { $tmp = array(); $url = 'http://picasaweb.google.com/data/feed/api/user/' . urlencode($userId) . '/album/'.$albumName."/"; $xml = $this->curlit($url); $xml = str_replace("xmlns='http://www.w3.org/2005/Atom'", '', $xml); if ($xml == "No album found.") { DB::alteration_message("$albumName NOT FOUND", "deleted"); return $tmp; } $dom = new domdocument; if ($xml) { $dom->loadXml($xml); $xpath = new domxpath($dom); $nodes = $xpath->query('//entry'); foreach ($nodes as $node) { $tmp[]['src'] = $xpath->query('.//media:thumbnail/@url', $node)->item(0)->textContent; } } return $tmp; } } class PicasaRandomImage_Controller extends ContentController { private static $allowed_actions = array( "one" => "ADMIN", "review" => "ADMIN", "donotuse" => "ADMIN", "select" => "ADMIN", "mylist" => "ADMIN" ); public function one($request) { $width = $request->Param("ID"); $image = PicasaRandomImage::get_random_image($width); if ($image) { return $image->URL; } } public function review($request) { echo "<html><head></head><body></body> <h2>Review Pictures</h2> <p>Click on pixies that you do not want to use.</p> <ul>"; $width = $request->Param("ID"); if (!$limit) { $width = 400; } $limit = $request->Param("OtherID"); if (!$limit) { $limit = 0; } $objects = PicasaRandomImage::get()->sort("ID", "ASC")->limit(100, $limit); if ($objects->count()) { foreach ($objects as $obj) { $obj->URL = str_replace('/s72/', '/s'.$width.'/', $obj->URL); $style = ""; if ($obj->Selected) { $style = "style=\"background-color: green;\""; } if ($obj->DoNotUse) { $style = "style=\"opacity: 0.3\""; } echo " <li $style> <a href=\"/randompicassaimage/donotuse/".$obj->ID."/\" class=\"remove\" style=\"float: right;\">remove</a> <a href=\"/randompicassaimage/select/".$obj->ID."/\" class=\"select\">select</a> <img src=\"".$obj->URL."\" alt=\"\" /> <hr style=\"clear: both\" /> </li>"; } } $limit = $limit + 100; echo " </ul><a href=\"/randompicassaimage/review/400/$limit/\">next 100</a>"; echo " <script src=\"http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js\"></script> <script type=\"text/javascript\"> jQuery(\"a.remove\").click( function(event){ event.preventDefault(); var url = jQuery(this).attr(\"href\"); var holder = jQuery(this).parents(\"li\"); jQuery.get( url, function() { holder.fadeOut(); } ); } ); jQuery(\"a.select\").click( function(event){ event.preventDefault(); var url = jQuery(this).attr(\"href\"); var holder = jQuery(this).parents(\"li\"); jQuery.get( url, function() { holder.css(\"backgroundColor\", \"green\"); } ); } ); </script> </body></html>"; } public function mylist($request) { echo "\$array = array("; $width = $request->Param("ID"); if (!$width) { $width = 2400; } $objects = PicasaRandomImage::get()->filter(array("DoNotUse" => 0, "Selected" => 1)); if ($objects->count()) { foreach ($objects as $obj) { if ($obj->URL) { $obj->URL = str_replace('/s72/', '/s'.$width.'/', $obj->URL); echo "\r\n"; echo "\t'".$obj->URL."'"; if (!$obj->Last()) { echo ","; } } } } echo "\r\n);"; } public function donotuse($request) { $id = intval($request->Param("ID")); if ($obj = PicasaRandomImage::get()->byID($id)) { $obj->DoNotUse = 1; $obj->write(); return "deleted"; } } public function select($request) { $id = intval($request->Param("ID")); if ($obj = PicasaRandomImage::get()->byID($id)) { $obj->DoNotUse = 0; $obj->Selected = 1; $obj->write(); return "selected"; } } } |