public function generateInsight(Instance $instance, User $user, $last_week_of_posts, $number_days) { parent::generateInsight($instance, $user, $last_week_of_posts, $number_days); $this->logger->logInfo("Begin generating insight", __METHOD__ . ',' . __LINE__); $year = date('Y'); $regenerate = false; //testing //$regenerate = true; $should_generate_insight = self::shouldGenerateEndOfYearAnnualInsight($this->slug, $instance, $insight_date = "{$year}-{$this->run_date}", $regenerate, $day_of_year = $this->run_date, null, $excluded_networks = array('twitter')); if ($should_generate_insight) { $this->logger->logInfo("Should generate", __METHOD__ . ',' . __LINE__); $fav_dao = DAOFactory::getDAO('FavoritePostDAO'); $fans = $fav_dao->getUsersWhoFavoritedMostOfYourPosts($instance->network_user_id, $instance->network, TimeHelper::getDaysSinceJanFirst()); if (!isset($fans) || sizeof($fans) == 0) { $this->logger->logInfo("No fans captured", __METHOD__ . ',' . __LINE__); return; } $post_dao = DAOFactory::getDAO('PostDAO'); $earliest_pub_date = $post_dao->getEarliestCapturedPostPubDate($instance); $qualified_year = "."; if (date('Y', strtotime($earliest_pub_date)) == date('Y')) { if (date('n', strtotime($earliest_pub_date)) > 1) { //not January //Earliest post was this year; figure out what month we have data since this year $since = date('F', strtotime($earliest_pub_date)); $qualified_year = " (at least since " . $since . ")."; } } $insight = new Insight(); $insight->instance_id = $instance->id; $insight->slug = $this->slug; $insight->date = "{$year}-{$this->run_date}"; $network = $instance->network; $copy = array('facebook' => array('normal' => array('headline' => "%username's biggest Facebook fans of %year", 'body' => "It feels great to have friends who support you. " . "%user_list liked %username's status updates the most this year%qualified_year")), 'instagram' => array('normal' => array('headline' => "%username's biggest Instagram fans of %year", 'body' => "It means a lot to have friends who love your stuff. " . "%user_list liked %username's Instagram photos and videos the most this year%qualified_year"))); $type = 'normal'; $headline = $this->getVariableCopy(array($copy[$network][$type]['headline']), array('year' => $year)); $fan_list = array(); foreach ($fans as $fan) { $fan_list[] = $fan->full_name; } $fan_list = $this->makeList($fan_list); $insight_text = $this->getVariableCopy(array($copy[$network][$type]['body']), array('user_list' => $fan_list, 'qualified_year' => $qualified_year)); $insight->headline = $headline; $insight->text = $insight_text; $insight->setPeople($fans); $insight->filename = basename(__FILE__, ".php"); $insight->emphasis = Insight::EMPHASIS_HIGH; $this->insight_dao->insertInsight($insight); } $this->logger->logInfo("Done generating insight", __METHOD__ . ',' . __LINE__); }
public function generateInsight(Instance $instance, User $user, $last_week_of_posts, $number_days) { parent::generateInsight($instance, $user, $last_week_of_posts, $number_days); $this->logger->logInfo("Begin generating insight", __METHOD__ . ',' . __LINE__); $year = date('Y'); $regenerate = false; //testing //$regenerate = true; $should_generate_insight = self::shouldGenerateEndOfYearAnnualInsight($this->slug, $instance, $insight_date = "{$year}-{$this->run_date}", $regenerate, $day_of_year = $this->run_date); if ($should_generate_insight) { $this->logger->logInfo("Should generate", __METHOD__ . ',' . __LINE__); $post_dao = DAOFactory::getDAO('PostDAO'); $days = TimeHelper::getDaysSinceJanFirst(); $bestie = $post_dao->getBestie($instance, $days); if (isset($bestie)) { $type = 'normal'; } else { $type = 'none'; } $earliest_pub_date = $post_dao->getEarliestCapturedPostPubDate($instance); $qualified_year = ""; if (date('Y', strtotime($earliest_pub_date)) == date('Y')) { if (date('n', strtotime($earliest_pub_date)) > 1) { //not January //Earliest post was this year; figure out what month we have data since this year $since = date('F', strtotime($earliest_pub_date)); $qualified_year = " (at least since " . $since . ")"; } } $insight = new Insight(); $insight->instance_id = $instance->id; $insight->slug = $this->slug; $insight->date = "{$year}-{$this->run_date}"; $network = $instance->network; $copy = array('twitter' => array('normal' => array('headline' => "%username's Twitter bestie of %year", 'body' => "Nobody likes tweeting into the void. %username and @%bestie made Twitter a " . "void-free place to tweet this year. %username tweeted at @%bestie " . "<strong>%u_to_b times</strong> in 2015, and @%bestie replied " . "<strong>%b_to_u times</strong>%qualified_year. OMG you two!"), 'none' => array('headline' => "%username's Twitter bestie of %year", 'body' => "%username didn't reply to any one person more than 3 times " . "this year%qualified_year. That means no one can claim the title of %username's Twitter " . "bestie. Playing hard-to-get, huh?")), 'facebook' => array('normal' => array('headline' => "%username's Facebook bestie of %year", 'body' => "Everyone loves getting comments from their friends. In 2015, %bestie commented " . "on %username's status updates <strong>%b_to_u times</strong>, more than " . "anyone else%qualified_year. Best friends forever!"), 'none' => array('headline' => "%username's Facebook bestie of %year", 'body' => "%username's friends must consider %username's words definitive - no one replied " . "more than three times to %username's status updates all year%qualified_year.")), 'instagram' => array('normal' => array('headline' => "%username's Instagram bestie of %year", 'body' => "Everyone loves getting comments from their friends. In 2015, %bestie commented " . "on %username's Instagram photos and videos <strong>%b_to_u times</strong>, more than " . "anyone else%qualified_year. Best friends forever!"), 'none' => array('headline' => "%username's Instagram bestie of %year", 'body' => "%username's photos and videos left %username's friends speechless - no one replied " . "more than three times to %username's Instagram posts all year%qualified_year."))); $headline = $this->getVariableCopy(array($copy[$network][$type]['headline']), array('year' => $year)); $insight_text = $this->getVariableCopy(array($copy[$network][$type]['body']), array('bestie' => isset($bestie['user_name']) ? $bestie['user_name'] : "", 'u_to_b' => isset($bestie['total_replies_to']) ? $bestie['total_replies_to'] : "", 'b_to_u' => isset($bestie['total_replies_from']) ? $bestie['total_replies_from'] : "", 'qualified_year' => $qualified_year)); $insight->headline = $headline; $insight->text = $insight_text; $insight->filename = basename(__FILE__, ".php"); $insight->emphasis = Insight::EMPHASIS_HIGH; if (isset($bestie['avatar'])) { $insight->header_image = $bestie['avatar']; } $this->insight_dao->insertInsight($insight); } $this->logger->logInfo("Done generating insight", __METHOD__ . ',' . __LINE__); }
/** * Get three most retweeted posts this year * @param Instance $instance * @param str $order Defaults to 'retweets' * @return array Three most retweeted posts in descending order */ public function topThreeThisYear(Instance $instance, $order = 'retweet_count_api') { $post_dao = DAOFactory::getDAO('PostDAO'); $days = TimeHelper::getDaysSinceJanFirst(); $posts = $post_dao->getAllPostsByUsernameOrderedBy($instance->network_username, $network = $instance->network, $count = 3, $order_by = $order, $in_last_x_days = $days, $iterator = false, $is_public = false); //Filter out posts with 0 retweets $posts_with_retweets = array(); foreach ($posts as $post) { if ($post->retweet_count_api > 0) { $posts_with_retweets[] = $post; } } return $posts_with_retweets; }
/** * Get the most talkative day(s) on network * @param Instance $instance * @return array Most talkative day or days */ public function getMostTalkativeDays(Instance $instance) { $post_dao = DAOFactory::getDAO('PostDAO'); $days = TimeHelper::getDaysSinceJanFirst(); $most_talkative_days = $post_dao->getMostTalkativeDays($instance->network_username, $instance->network, $in_last_x_days = $days); $most_posts = $most_talkative_days[0]['post_count']; foreach ($most_talkative_days as $key => $day) { if ($day['post_count'] < $most_posts) { unset($most_talkative_days[$key]); } } return $most_talkative_days; }
/** * Get three most replied to posts this year * @param Instance $instance * @param str $order Defaults to 'reply_count_cache' * @return array Three most replied to posts in descending order */ public function topThreeThisYear(Instance $instance, $order = 'reply_count_cache') { $post_dao = DAOFactory::getDAO('PostDAO'); $days = TimeHelper::getDaysSinceJanFirst(); $posts = $post_dao->getAllPostsByUsernameOrderedBy($instance->network_username, $network = $instance->network, $count = 3, $order_by = $order, $in_last_x_days = $days, $iterator = false, $is_public = false); return $posts; }
/** * Get year of posts as an iterator * @param Instance $instance * @return PostIterator $posts */ public function getYearOfPosts(Instance $instance) { $post_dao = DAOFactory::getDAO('PostDAO'); $days = TimeHelper::getDaysSinceJanFirst(); $posts = $post_dao->getAllPostsByUsernameOrderedBy($instance->network_username, $network = $instance->network, $count = 0, $order_by = 'pub_date', $in_last_x_days = $days, $iterator = true, $is_public = false); return $posts; }
public function testOfGetDaysSinceJanFirst() { $test_date = new DateTime(); $test_date->setDate(2014, 2, 21); $days = TimeHelper::getDaysSinceJanFirst($test_date->getTimestamp()); $this->assertEqual($days, 51); $test_date = new DateTime(); $test_date->setDate(2014, 1, 30); $days = TimeHelper::getDaysSinceJanFirst($test_date->getTimestamp()); $this->assertEqual($days, 29); //Non leap year $test_date = new DateTime(); $test_date->setDate(2014, 3, 1); $days = TimeHelper::getDaysSinceJanFirst($test_date->getTimestamp()); $this->assertEqual($days, 59); //Leap year $test_date = new DateTime(); $test_date->setDate(2016, 3, 1); $days = TimeHelper::getDaysSinceJanFirst($test_date->getTimestamp()); $this->assertEqual($days, 60); }
public function testGetMostTalkativeDays() { $year = date('Y'); // A bunch of posts on 2014-02-07 for ($i = 0; $i < 3; $i++) { $builders[] = FixtureBuilder::build('posts', array('post_text' => 'This is very shared', 'pub_date' => "{$year}-02-07", 'author_username' => 'a_user', 'network' => 'twitter', 'post_id' => 'ablah' . $i)); } // One post on a diff day $builders[] = FixtureBuilder::build('posts', array('post_text' => 'This is pretty well shared', 'pub_date' => "{$year}-08-07", 'author_username' => 'a_user', 'network' => 'twitter', 'post_id' => 'ablh')); // Larger group of posts, but in the wrong year for ($i = 0; $i < 4; $i++) { $builders[] = FixtureBuilder::build('posts', array('post_text' => 'This is least shared', 'pub_date' => '2013-03-09', 'author_username' => 'a_user', 'network' => 'twitter', 'post_id' => 'emvi' . $i)); } // large cluster of posts by wrong user for ($i = 0; $i < 5; $i++) { $builders[] = FixtureBuilder::build('posts', array('post_text' => 'These Sochi games are kind of a cluster already.', 'pub_date' => "{$year}-03-09", 'author_username' => 'a_different_user', 'network' => 'twitter', 'post_id' => 'eeic' . $i)); } $post_dao = DAOFactory::getDAO('PostDAO'); $days = TimeHelper::getDaysSinceJanFirst(); $most_talkative_days = $post_dao->getMostTalkativeDays($author_username = '******', $network = 'twitter', $in_last_x_days = $days); // // test that query returns 2 results (only within query span) $this->assertEqual(2, sizeof($most_talkative_days)); // $this->debug(Utils::varDumpToString($most_talkative_days[0])); // test that query returns the correct day and # of posts $this->assertEqual(3, $most_talkative_days[0]['post_count']); $this->assertEqual("{$year}-02-07", $most_talkative_days[0]['pub_date']); }