Exemplo n.º 1
0
 public static function _runTumblrNews(\Phalcon\DiInterface $di)
 {
     $news_items = array();
     $config = $di->get('config');
     // Pull featured images.
     $timestamp_threshold = strtotime('-6 weeks');
     $api_params = array('api_key' => $config->apis->tumblr->key, 'limit' => 10);
     $api_url = 'http://api.tumblr.com/v2/blog/news.ponyvillelive.com/posts/photo?' . http_build_query($api_params);
     $results_raw = @file_get_contents($api_url);
     if ($results_raw) {
         $results = json_decode($results_raw, true);
         $posts = $results['response']['posts'];
         foreach ((array) $posts as $post) {
             if ($post['timestamp'] < $timestamp_threshold) {
                 continue;
             }
             $image = null;
             $post_style = 'vertical';
             $image_is_valid = false;
             foreach ((array) $post['photos'] as $photo) {
                 $image = $photo['original_size'];
                 if ($image['width'] == 600 && $image['height'] == 300) {
                     $image_is_valid = true;
                     $post_style = 'vertical';
                     break;
                 } elseif ($image['width'] == 1150 && $image['height'] == 200) {
                     $image_is_valid = true;
                     $post_style = 'horizontal';
                     break;
                 }
             }
             if (!$image_is_valid) {
                 continue;
             }
             // Copy the image to the local static directory (for SSL and other caching support).
             $image_url = $image['url'];
             $image_url_basename = basename($image_url);
             $local_path_base = 'rotators/' . $image_url_basename;
             $local_url = $local_path_base;
             $local_path = DF_INCLUDE_TEMP . DIRECTORY_SEPARATOR . str_replace('/', DIRECTORY_SEPARATOR, $local_path_base);
             $s3_path = Service\AmazonS3::path($local_url);
             if (!file_exists($s3_path)) {
                 @mkdir(dirname($local_path));
                 @copy($image_url, $local_path);
                 // Optimize image for fast display.
                 Image::resizeImage($local_path, $local_path, $image['width'], $image['height']);
                 Service\AmazonS3::upload($local_path, $local_path_base);
             }
             $tags = array_map('strtolower', (array) $post['tags']);
             if (in_array('archive', $tags)) {
                 continue;
             }
             $description = strip_tags($post['caption']);
             if (strpos($description, ':') === FALSE) {
                 break;
             }
             list($title, $description) = explode(':', $description, 2);
             $description = Utilities::truncateText($description, self::DESCRIPTION_LENGTH);
             $news_items[] = array('id' => 'tumblr_' . $post['id'], 'title' => trim($title), 'source' => 'tumblr', 'body' => trim($description), 'image_url' => \PVL\Url::upload($local_url), 'web_url' => $post['post_url'], 'layout' => $post_style, 'tags' => (array) $post['tags'], 'sort_timestamp' => $post['timestamp'], 'display_timestamp' => $post['timestamp']);
         }
     }
     return $news_items;
 }