<commit_before><?php
namespace PVL;

use Entity\NetworkNews;
use Entity\Podcast;
use Entity\Schedule;
use Entity\Station;

class NewsManager
{
    const DESCRIPTION_LENGTH = 300;

    public static function syncNetwork()
    {
        $di = \Phalcon\Di::getDefault();
        $em = $di->get('em');
        $config = $di->get('config');

        $news_items = array();

        // 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) // New vertical style.
                    {
                        $image_is_valid = true;
                        $post_style = 'vertical';
                        break;
                    }
                    elseif ($image['width'] == 1150 && $image['height'] == 200) // Older horizontal style.
                    {
                        $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)) {
                    @copy($image_url, $local_path);

                    // Optimize image for fast display.
                    \DF\Image::resizeImage($local_path, $local_path, $image['width'], $image['height']);

                    Service\AmazonS3::upload($local_path, $s3_path);
                }

                $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 = \DF\Utilities::truncateText($description, self::DESCRIPTION_LENGTH);

                $news_items[] = array(
                    'id' => 'tumblr_' . $post['id'],
                    'title' => trim($title),
                    'source' => 'tumblr',
                    'body' => trim($description),
                    'image_url' => $local_url,
                    'web_url' => $post['post_url'],
                    'layout' => $post_style,
                    'tags' => (array)$post['tags'],
                    'sort_timestamp' => $post['timestamp'],
                    'display_timestamp' => $post['timestamp'],
                );
            }
        }

        // Pull podcast episodes.
        $podcasts_raw = $em->createQuery('SELECT p, pe FROM Entity\Podcast p LEFT JOIN p.episodes pe<commit_msg>Fix issues with S3 handling of news photos.<commit_after><?php
namespace PVL;

use DF\Image;
use DF\Utilities;

use Entity\NetworkNews;
use Entity\Podcast;
use Entity\Schedule;
use Entity\Station;

class NewsManager
{
    const DESCRIPTION_LENGTH = 300;

    public static function syncNetwork()
    {
        $di = \Phalcon\Di::getDefault();
        $em = $di->get('em');
        $config = $di->get('config');

        $news_items = array();

        // 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) // New vertical style.
                    {
                        $image_is_valid = true;
                        $post_style = 'vertical';
                        break;
                    }
                    elseif ($image['width'] == 1150 && $image['height'] == 200) // Older horizontal style.
                    {
                        $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' => $local_url,
                    'web_url' => $post['post_url'],
                    'layout' => $post_style,
                    'tags' => (array)$post['tags'],
                    'sort_timestamp' => $post['timestamp'],
                    'display_timestamp' => $post['timestamp'],
                );
            }
        }

        // Pull podcast episodes.
        $podcasts_raw = $em->createQuery('SELECT p, pe FROM Entity\Podcast p LEFT JOIN p.episodes pe
{'fraction_non_alphanumeric': 0.1345423354981007, 'fraction_numerical': 0.006371768165665972, 'mean_word_length': 1.4710868907054193, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7125997', 'n_tokens_mistral': 2319, 'n_tokens_neox': 2090, 'n_words': 419}