<commit_before><?php

namespace Mobile;

class Newsfeeds {
	// Plymouth.edu Feed List
	// http://www.plymouth.edu/main/rss.html

	// Settings
	private static $sort_settings = array(
		'limit_posts_per_source' => 8,
		'limit_posts_total' => 20,
		'old_post_days' => 365,
	);
	private static $news_settings = array(
		'twitter' => array(
			'feed_url' => 'http://api.twitter.com/1/statuses/user_timeline.json?screen_name=',
			'username' => 'plymouthstate',
		),
		'facebook' => array(
			'feed_url' => 'https://graph.facebook.com/',
			'username' => 'plymouthstate',
			'request' => 'posts',
			'access_token' => '172080957061|3<PASSWORD>ba<PASSWORD>cf3.0-<PASSWORD>|<KEY>',
		),
		'rss_feeds' => array(
			'feed_urls' => array(
				'http://www.plymouth.edu/includes/rss.php?v=2x&s=hn',
				'http://athletics.plymouth.edu/landing/headlines-featured?feed=rss_2.0',
			),
		),
	);

	/**
	 * Compare timestamps for sorting
	 * @param array $a An array with a 'timestamp' key
	 * @param array $b An array with a 'timestamp' key
	 */
	public static function compare_array_timestamp($a, $b) {
		return $a['timestamp'] < $b['timestamp'];
	} // End compare_array_timestamp

	/**
	 * Method to grab JSON data from a Twitter feed and return it as an array
	 */
	public static function twitter() {
		// Setup the parameters for limiting the returned data
		$url_params = '&count='.self::$sort_settings['limit_posts_per_source'];

		// Grab and parse the Twitter Feed
		// Set the url
		$twitter_url = self::$news_settings['twitter']['feed_url'].self::$news_settings['twitter']['username'].$url_params;

		// Get the JSON data with a CURL request to the url
		$feed_data = \PSU::curl($twitter_url, \PSU::FILE_GET_CONTENTS);

		// Return the JSON decoded as an array
		return json_decode($feed_data, true);
	} // End twitter

	/**
	 * Method to grab JSON data from a Facebook feed and return it as an array
	 */
	public static function facebook() {
		// Setup the parameters for limiting the returned data
		$url_params = '&limit='.self::$sort_settings['limit_posts_per_source'];

		// Grab and parse the Facebook Feed
		// Set the url
		$facebook_url = self::$news_settings['facebook']['feed_url'].self::$news_settings['facebook']['username'].'/'.self::$news_settings['facebook']['request'].'?access_token='.self::$news_settings['facebook']['access_token'].$url_params;

		// Get the JSON data with a CURL request to the url
		$feed_data = \PSU::curl($facebook_url, \PSU::FILE_GET_CONTENTS);

		// Return the JSON decoded as an array
		return json_decode($feed_data, true);
	} // End facebook

	/**
	 * Method to grab RSS data as an array of PSU Zend Feed objects
	 */
	public static function rss() {
		// Go through each RSS feed and parse it
		$feed_data = array();
		$feedCount = 0;
		foreach (self::$news_settings['rss_feeds']['feed_urls'] as $feed) {
			try {
				// Get the feed data
				$feed_data[$feedCount] = \PSU\Feed::import($feed);
			}
			catch (Zend_Feed_Exception $e) {
				//echo "Exception caught importing feed: {$e->getMessage()}\n";
				continue;
			}

			// Up the counter
			$feedCount++;<commit_msg>Added proper exception handling to the Newsfeed. The feed will now fail gracefully.<commit_after><?php

namespace Mobile;

class Newsfeeds {
	// Plymouth.edu Feed List
	// http://www.plymouth.edu/main/rss.html

	// Settings
	private static $sort_settings = array(
		'limit_posts_per_source' => 8,
		'limit_posts_total' => 20,
		'old_post_days' => 365,
	);
	private static $news_settings = array(
		'twitter' => array(
			'feed_url' => 'https://api.twitter.com/1/statuses/user_timeline.json?screen_name=',
			'username' => 'plymouthstate',
		),
		'facebook' => array(
			'feed_url' => 'https://graph.facebook.com/',
			'username' => 'plymouthstate',
			'request' => 'posts',
			'access_token' => '<KEY>',
		),
		'rss_feeds' => array(
			'feed_urls' => array(
				'http://www.plymouth.edu/includes/rss.php?v=2x&s=hn',
				'http://athletics.plymouth.edu/landing/headlines-featured?feed=rss_2.0',
			),
		),
	);

	/**
	 * Compare timestamps for sorting
	 * @param array $a An array with a 'timestamp' key
	 * @param array $b An array with a 'timestamp' key
	 */
	public static function compare_array_timestamp($a, $b) {
		return $a['timestamp'] < $b['timestamp'];
	} // End compare_array_timestamp

	/**
	 * Method to grab JSON data from a Twitter feed and return it as an array
	 */
	public static function twitter() {
		// Set up the feed_data array
		$feed_data = array();

		// Setup the parameters for limiting the returned data
		$url_params = '&count='.self::$sort_settings['limit_posts_per_source'];

		// Grab and parse the Twitter Feed
		// Set the url
		$twitter_url = self::$news_settings['twitter']['feed_url'].self::$news_settings['twitter']['username'].$url_params;

		// PSU::curl throws an exception if the document doesn't return a perfect 200 error
		try {
			// Get the JSON data with a CURL request to the url
			$feed_data = \PSU::curl($twitter_url, \PSU::FILE_GET_CONTENTS);

			// Return the JSON decoded as an array
			return json_decode($feed_data, true);
		}
		catch (\PSUToolsException $e) {
			// For now, lets just grab the exception and put it into the session
			$_SESSION['errors'][] = $e->getMessage();
		}

		return $feed_data;
	} // End twitter

	/**
	 * Method to grab JSON data from a Facebook feed and return it as an array
	 */
	public static function facebook() {
		// Set up the feed_data array
		$feed_data = array();

		// Setup the parameters for limiting the returned data
		$url_params = '&limit='.self::$sort_settings['limit_posts_per_source'];

		// Grab and parse the Facebook Feed
		// Set the url
		$facebook_url = self::$news_settings['facebook']['feed_url'].self::$news_settings['facebook']['username'].'/'.self::$news_settings['facebook']['request'].'?access_token='.self::$news_settings['facebook']['access_token'].$url_params;

		// PSU::curl throws an exception if the document doesn't return a perfect 200 error
		try {
			// Get the JSON data with a CURL request to the url
			$feed_data = \PSU::curl($facebook_url, \PSU::FILE_GET_CONTENTS);

			// Return the JSON decoded as an array
			return json_decode($feed_data, true);
		}
		catch (\PSUToolsException $e) {
			// For now, lets just grab the exception and put it into the session
			$_SESSION['errors'][] = $e->getMessage();
		}

		return $feed_data;
	} // End facebook

	/**
	 * Method to grab RSS data as an array of PSU Zend Feed objects
	 */
	public static function rss() {
		// Go through each RSS feed and parse it
		$feed_data = array();
		$feedCount = 0;
		foreach (self::$news_settings['rss_feeds']['feed_urls'] as $feed) {
			// \PSU\Feed::import can throw many exceptions due to the Zend underpinnings. We don't need to handle them individually, so let's just catch them all
			// Possible exceptions seen so far:
			// Zend_Feed_Exception
			// Zend_Http_Client_Exception
			// Zend_Http_Client_Adapter_Exception
			try {
				// Get the feed data
				$feed_data[$feedCount] = \PSU\Feed::import($feed);
			}
			catch (\Exception $e) {
				//echo "Exception caught importing feed: {$e->getMessage()}\n";

				// For now, lets just grab the exception and put it into the session
				$_SESSION['errors'][] = $e->getMessage();

				// Skip this iteration in the loop
				continue;
			}

			// Up the counter
			$feedCount++;
{'fraction_non_alphanumeric': 0.14755881952944377, 'fraction_numerical': 0.005847953216374269, 'mean_word_length': 3.75371687136393, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 39, 'https://': 3, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1051426', 'n_tokens_mistral': 2648, 'n_tokens_neox': 2333, 'n_words': 673}