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__); if ($instance->network == 'twitter') { $day_of_week = 4; } else { $day_of_week = 1; } $should_generate_insight = self::shouldGenerateWeeklyInsight('long_lost_contacts', $instance, $insight_date = 'today', $regenerate_existing_insight = false, $day_of_week = $day_of_week); if ($should_generate_insight) { $follow_dao = DAOFactory::getDAO('FollowDAO'); $contacts = $follow_dao->getFolloweesRepliedToThisWeekLastYear($instance->network_user_id, $instance->network); $long_lost_contacts = array(); $insight_text = ''; if (count($contacts)) { $post_dao = DAOFactory::getDAO('PostDAO'); foreach ($contacts as $contact) { if ($post_dao->getDaysAgoSinceUserRepliedToRecipient($instance->network_user_id, $contact->user_id, $instance->network) >= 365) { $long_lost_contacts[] = $contact; } } } if (count($long_lost_contacts)) { $headline = $this->username . " hasn't replied to " . (count($long_lost_contacts) > 1 ? "<strong>" . count($long_lost_contacts) . " people</strong> " : $long_lost_contacts[0]->full_name . " ") . "in over a year"; $insight_text = "It's good to reflect after some time has passed."; //Instantiate the Insight object $my_insight = new Insight(); //REQUIRED: Set the insight's required attributes $my_insight->slug = 'long_lost_contacts'; //slug to label this insight's content $my_insight->instance_id = $instance->id; $my_insight->date = $this->insight_date; //date is often this or $simplified_post_date $my_insight->headline = $headline; // or just set a string like 'Ohai'; $my_insight->text = $insight_text; // or just set a strong like "Greetings humans"; if (count($long_lost_contacts) == 1) { $my_insight->header_image = $long_lost_contacts[0]->avatar; } $my_insight->filename = basename(__FILE__, ".php"); $my_insight->emphasis = Insight::EMPHASIS_MED; $my_insight->setPeople($long_lost_contacts); $this->insight_dao->insertInsight($my_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__); $post_dao = DAOFactory::getDAO('PostDAO'); $insight_text = ''; foreach ($last_week_of_posts as $post) { $big_reshares = $post_dao->getRetweetsByAuthorsOverFollowerCount($post->post_id, $instance->network, $user->follower_count); if (isset($big_reshares) && sizeof($big_reshares) > 0) { if (!isset($config)) { $config = Config::getInstance(); } if (sizeof($big_reshares) > 1) { $headline = "People with lots of followers " . $this->terms->getVerb('shared') . " " . "{$this->username}"; } else { $follower_count_multiple = intval($big_reshares[0]->follower_count / $user->follower_count); if ($follower_count_multiple > 1) { $headline = "Someone with <strong>" . $follower_count_multiple . "x</strong> more followers " . $this->terms->getVerb('shared') . " " . $this->username; } else { $headline = $big_reshares[0]->full_name . " " . $this->terms->getVerb('shared') . " " . $this->username; } } $added_people = 0; foreach ($big_reshares as $big_resharer) { $added_people += $big_resharer->follower_count - $user->follower_count; } $insight_text = number_format($added_people) . " more people saw " . $this->username . "'s " . $this->terms->getNoun('post') . "."; $simplified_post_date = date('Y-m-d', strtotime($post->pub_date)); //Instantiate the Insight object $my_insight = new Insight(); $my_insight->slug = "big_reshare_" . $post->id; //slug to label this insight's content $my_insight->instance_id = $instance->id; $my_insight->date = $simplified_post_date; //date of the data this insight applies to $my_insight->headline = $headline; // or just set a string like 'Ohai'; $my_insight->text = $insight_text; // or just set a strong like "Greetings humans"; $my_insight->header_image = $header_image; $my_insight->filename = basename(__FILE__, ".php"); $my_insight->emphasis = Insight::EMPHASIS_MED; $my_insight->setPeople($big_reshares); $my_insight->setPosts(array($post)); $this->insight_dao->insertInsight($my_insight); } } $this->logger->logInfo("Done generating insight", __METHOD__ . ',' . __LINE__); }
public function testInsightRelatedDataSetters() { $i = new Insight(); $i->setPhotos("this is my list of photos"); $this->assertEqual($i->related_data["photos"], "this is my list of photos"); $i->setPosts("my posts"); $this->assertEqual($i->related_data["posts"], "my posts"); $i->setLineChart("line chart data goes here"); $this->assertEqual($i->related_data["line_chart"], "line chart data goes here"); $i->setBarChart("bar chart data goes here"); $this->assertEqual($i->related_data["bar_chart"], "bar chart data goes here"); $i->setPeople("list 'o users"); $this->assertEqual($i->related_data["people"], "list 'o users"); $i->setLinks("listoflinks"); $this->assertEqual($i->related_data["links"], "listoflinks"); $i->setMilestoneNumber("milestone_number"); $this->assertEqual($i->related_data["milestone_number"], "milestone_number"); }
public function testInsightRelatedDataSetters() { $i = new Insight(); // @TODO Assign and assert that the data is in the array structure it should be $i->setPhotos("this is my list of photos"); $this->assertEqual($i->related_data["photos"], "this is my list of photos"); $i->setPosts("my posts"); $this->assertEqual($i->related_data["posts"], "my posts"); $i->setLineChart("line chart data goes here"); $this->assertEqual($i->related_data["line_chart"], "line chart data goes here"); $i->setBarChart("bar chart data goes here"); $this->assertEqual($i->related_data["bar_chart"], "bar chart data goes here"); $i->setPeople("list 'o users"); $this->assertEqual($i->related_data["people"], "list 'o users"); $i->setLinks("listoflinks"); $this->assertEqual($i->related_data["links"], "listoflinks"); $i->setMilestones("milestones"); $this->assertEqual($i->related_data["milestones"], "milestones"); $i->setButton("button"); $this->assertEqual($i->related_data["button"], "button"); }
public function generateInsight(Instance $instance, User $user, $last_week_of_posts, $number_days) { parent::generateInsight($instance, $user, $last_week_of_posts, $number_days); $thanksgiving_day = date('m/j', strtotime("3 weeks thursday", mktime(0, 0, 0, 11, 1, date('Y')))); $regenerate = false; //test // $thanksgiving_day = date('11/20'); // $regenerate = true; if (!$this->shouldGenerateAnnualInsight($this->slug, $instance, 'today', $regenerate, $thanksgiving_day, null, array('instagram'))) { $this->logger->logInfo("Skipped generating insight on " . $instance->network, __METHOD__ . ',' . __LINE__); return; } $this->logger->logInfo("Begin generating insight", __METHOD__ . ',' . __LINE__); $user_dao = DAOFactory::getDAO('UserDAO'); $post_dao = DAOFactory::getDAO('PostDAO'); $post_iterator = $post_dao->getThisYearOfPostsIterator($instance->network_user_id, $instance->network); $thankees = array(); $thankees_totals = array(); foreach ($post_iterator as $post) { $in_reply_id = $post->in_reply_to_user_id; if ($in_reply_id == 0 || $in_reply_id == $instance->network_user_id) { // No self thanks, untargeted thanks, or rethanking continue; } $text = strtolower($post->post_text); $has_thanks = preg_match('/(\\W|^)(thanks|thank you)(\\W|$)/', $text); if ($has_thanks) { if (preg_match('/(\\W|^)no (thanks|thank you)/', $text) || preg_match('/thank(s| you),? but/', $text)) { $has_thanks = false; } } if (!$has_thanks) { continue; } if (isset($thankees_totals[$in_reply_id])) { $thankees_totals[$in_reply_id] = $thankees_totals[$in_reply_id] + 1; } else { $thankees_totals[$in_reply_id] = 0; } } arsort($thankees_totals, SORT_NUMERIC); foreach ($thankees_totals as $user_id => $count) { $user = $user_dao->getDetails($user_id, $instance->network); if ($user) { $thankees[] = $user; } } if (count($thankees) > 1) { $this->logger->logInfo("More than one thankee", __METHOD__ . ',' . __LINE__); $insight = new Insight(); $insight->slug = $this->slug; $insight->instance_id = $instance->id; $insight->date = $this->insight_date; if ($instance->network == 'facebook') { $insight->headline = $this->username . ' had friends to be thankful for in ' . date('Y'); $insight->text = "These are the friends " . $this->username . " was thankful for this year."; $insight->setHeroImage(array('url' => 'https://www.thinkup.com/assets/images/insights/2014-11/thanksgiving-2.jpg', 'alt_text' => $insight->headline, 'credit' => 'Photo: John-Morgan', 'img_link' => 'https://www.flickr.com/photos/aidanmorgan/4135626581/')); } else { $insight->headline = 'Who ' . $this->username . " was thankful for in " . date('Y'); if (count($thankees) > 20) { $insight->text = "These are just some of the <strong>" . count($thankees) . " people</strong> " . $this->username . " thanked this year."; } else { $insight->text = "These are all the people " . $this->username . " thanked this year."; } $insight->setHeroImage(array('url' => 'https://www.thinkup.com/assets/images/insights/2014-11/thanksgiving-1.jpg', 'alt_text' => $insight->headline, 'credit' => 'Photo: Steve Voght', 'img_link' => 'https://www.flickr.com/photos/voght/2441818832/')); } $insight->filename = basename(__FILE__, ".php"); $insight->emphasis = Insight::EMPHASIS_HIGH; $this->logger->logInfo("About to setPeople, total is " . count($thankees), __METHOD__ . ',' . __LINE__); $thankees_sliced = array_slice($thankees, 0, 20); $insight->setPeople($thankees_sliced); $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__); $filename = basename(__FILE__, ".php"); $insight_text = ''; //Find out if insight already exists $should_generate_insight = self::shouldGenerateInsight('top_amplifier', $instance, date('Y-m-d')); if ($should_generate_insight) { //insight does not exist //Get yesterday's retweets $yesterdays_retweets = array(); $simplified_date_yesterday = date('Y-m-d', strtotime('-1 day')); foreach ($last_week_of_posts as $post) { if ($post->in_retweet_of_post_id != null && $post->in_rt_of_user_id != null) { $simplified_post_date = date('Y-m-d', strtotime($post->pub_date)); if ($simplified_date_yesterday == $simplified_post_date) { $yesterdays_retweets[] = $post; } } } $largest_added_audience = 0; $insight_retweeted_user = null; $insight_retweet = null; //Get top amplifier from yesterday foreach ($yesterdays_retweets as $post) { if (!isset($user_dao)) { $user_dao = DAOFactory::getDAO('UserDAO'); } $retweeted_user = $user_dao->getDetails($post->in_rt_of_user_id, $post->network); //if user exists and has fewer followers than instance user compare to current top if (isset($retweeted_user) && $retweeted_user->follower_count < $user->follower_count) { $added_audience = $user->follower_count - $retweeted_user->follower_count; if ($added_audience > $largest_added_audience) { $largest_added_audience = $added_audience; $insight_retweeted_user = $retweeted_user; $insight_retweet = $post; } } } //If there's a top amplifier from yesterday, insert the insight if ($largest_added_audience > 0 && isset($insight_retweeted_user) && isset($insight_retweet)) { $multiplier = floor($user->follower_count / $insight_retweeted_user->follower_count); if ($multiplier > 1 && TimeHelper::getTime() / 10 % 2 == 1) { $largest_added_audience = number_format($multiplier) . 'x'; } else { $largest_added_audience = number_format($largest_added_audience); } $retweeted_username = $insight_retweeted_user->username; if ($instance->network == 'twitter') { $retweeted_username = '******' . $retweeted_username; } $headline = $this->getVariableCopy(array($insight_retweeted_user->full_name . " can thank %username for %added more people seeing this %post", "%added more people saw %repostedee's %post thanks to %username", '%username boosted ' . $insight_retweeted_user->full_name . '\'s %post to %added more people'), array('repostedee' => $retweeted_username, 'added' => $largest_added_audience)); $my_insight = new Insight(); $my_insight->instance_id = $instance->id; $my_insight->slug = 'top_amplifier'; //slug to label this insight's content $my_insight->date = date('Y-m-d'); //date of the data this insight applies to $my_insight->headline = $headline; // or just set a string like 'Ohai'; $my_insight->text = $insight_text; // or just set a strong like "Greetings humans"; $my_insight->header_image = $insight_retweeted_user->avatar; $my_insight->emphasis = Insight::EMPHASIS_MED; $my_insight->filename = basename(__FILE__, ".php"); $my_insight->setPosts(array($insight_retweet)); $my_insight->setPeople(array($insight_retweeted_user)); $this->insight_dao->insertInsight($my_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); $thanksgiving_day = date('m/j', strtotime("3 weeks thursday", mktime(0, 0, 0, 11, 1, date('Y')))); //test //$thanksgiving_day = date('11/21'); if (!$this->shouldGenerateAnnualInsight($this->slug, $instance, 'today', false, $thanksgiving_day)) { $this->logger->logInfo("Skipped generating insight", __METHOD__ . ',' . __LINE__); return; } $this->logger->logInfo("Begin generating insight", __METHOD__ . ',' . __LINE__); $user_dao = DAOFactory::getDAO('UserDAO'); $post_dao = DAOFactory::getDAO('PostDAO'); $iterator = $post_dao->getAllRepliesInRange($instance->network_user_id, $instance->network, $count = 0, $from = date('Y-m-d', strtotime('January 1')), $until = date('Y-m-d'), $page = 1, $order_by = 'pub_date', $direction = 'DESC', $is_public = false, $iterator = true); $thankees = array(); $seen = array(); foreach ($iterator as $post) { $author_id = $post->author_user_id; if ($author_id == $instance->network_user_id || in_array($author_id, $seen)) { // Skip ourselves or people we've seen. continue; } $text = strtolower($post->post_text); $has_thanks = preg_match('/(\\W|^)(thanks|thank you)(\\W|$)/', $text); if ($has_thanks) { if (preg_match('/(\\W|^)no (thanks|thank you)/', $text) || preg_match('/thank(s| you),? but/', $text)) { $has_thanks = false; } } if (!$has_thanks) { continue; } $seen[] = $author_id; $user = $user_dao->getDetails($author_id, $instance->network); if ($user) { $thankees[] = $user; } } $thankees_totals = array(); foreach ($post_iterator as $post) { $author_id = $post->author_user_id; if ($author_id == $instance->network_user_id) { // Skip ourselves continue; } $text = strtolower($post->post_text); $has_thanks = preg_match('/(\\W|^)(thanks|thank you)(\\W|$)/', $text); if ($has_thanks) { if (preg_match('/(\\W|^)no (thanks|thank you)/', $text) || preg_match('/thank(s| you),? but/', $text)) { $has_thanks = false; } } if (!$has_thanks) { continue; } if (isset($thankees_totals[$author_id])) { $thankees_totals[$author_id] = $thankees_totals[$author_id] + 1; } else { $thankees_totals[$author_id] = 0; } } arsort($thankees_totals, SORT_NUMERIC); foreach ($thankees_totals as $user_id => $count) { $user = $user_dao->getDetails($user_id, $instance->network); if ($user) { $thankees[] = $user; } } if (count($thankees) > 1) { $num = count($thankees); $insight = new Insight(); $insight->slug = $this->slug; $insight->instance_id = $instance->id; $insight->date = $this->insight_date; if ($instance->network == 'facebook') { $insight->headline = $num . " Facebook friends were thankful for " . $this->username; $insight->text = "It's great to have friends who share the love. These {$num} people were thankful for " . $this->username . " over the past year."; $insight->setHeroImage(array('url' => 'https://www.thinkup.com/assets/images/insights/2014-11/thanksgiving-3.jpg', 'alt_text' => '', 'credit' => 'Photo: paul bica', 'img_link' => 'https://www.flickr.com/photos/dexxus/2981387336/')); } else { $insight->headline = $num . " people were thankful for " . $this->username; if (count($thankees) > 20) { $insight->text = "These are just some of the people who shared an appreciation for " . $this->username . " this year."; } else { $insight->text = "These are all the people who shared an appreciation for " . $this->username . " this year."; } $insight->text = $insight->text . " Who says you can't build meaningful relationships on Twitter?"; $insight->setHeroImage(array('url' => 'https://www.thinkup.com/assets/images/insights/2014-11/thanksgiving-4.jpg', 'alt_text' => '', 'credit' => 'Photo: David Joyce', 'img_link' => 'https://www.flickr.com/photos/deapeajay/3024604627/')); } $insight->filename = basename(__FILE__, ".php"); $insight->emphasis = Insight::EMPHASIS_HIGH; $thankees_sliced = array_slice($thankees, 0, 20); $insight->setPeople($thankees_sliced); $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__); $my_insight = new Insight(); $my_insight->instance_id = $instance->id; $my_insight->date = $this->insight_date; //date of the data this insight applies to $my_insight->text = ''; $my_insight->filename = basename(__FILE__, ".php"); $follow_dao = DAOFactory::getDAO('FollowDAO'); // Least likely followers who are not verified, based on follower-to-followee ratio // We grab up to 10 possibilities, then filter for some spam account criteria and take the top 3 that remain $least_likely_followers = $follow_dao->getLeastLikelyFollowersByDay($instance->network_user_id, $instance->network, 0, 10); $least_likely_followers = array_filter($least_likely_followers, array($this, 'filterFollowers')); $least_likely_followers = array_slice($least_likely_followers, 0, 3); $total_followers = sizeof($least_likely_followers); if ($total_followers > 0) { //if not null, store insight if ($total_followers > 1) { $my_insight->headline = "{$this->username} got {$total_followers} interesting new followers"; $my_insight->slug = 'least_likely_followers'; $my_insight->emphasis = Insight::EMPHASIS_MED; $my_insight->setPeople($least_likely_followers); } else { $follower = $least_likely_followers[0]; $name = $this->getFollowerName($follower); $my_insight->headline = "{$this->username} got an interesting new follower"; $my_insight->slug = 'least_likely_followers'; $my_insight->emphasis = Insight::EMPHASIS_MED; $my_insight->setPeople($least_likely_followers); $my_insight->header_image = $follower->avatar; } } // Verified followers $verified_followers = $follow_dao->getVerifiedFollowersByDay($instance->network_user_id, $instance->network, 0, 3); $total_followers = sizeof($verified_followers); if ($total_followers > 0) { //if not null, store insight if ($total_followers > 1) { $my_insight->slug = 'verified_followers'; $my_insight->headline = "{$this->username} got {$total_followers} new verified followers!"; $my_insight->emphasis = Insight::EMPHASIS_HIGH; $my_insight->setPeople($verified_followers); } else { $follower = $verified_followers[0]; $name = $this->getFollowerName($follower); $my_insight->slug = 'verified_followers'; $my_insight->headline = "{$this->username} got a new verified follower!"; $my_insight->header_image = $verified_followers[0]->avatar; $my_insight->emphasis = Insight::EMPHASIS_HIGH; $my_insight->setPeople($verified_followers); } $total_verified = $follow_dao->getVerifiedFollowerCount($instance->network_user_id, $instance->network); if ($total_verified > $total_followers) { $my_insight->text = "That makes a total of <strong>" . number_format($total_verified) . " verified followers</strong>."; } $my_insight->header_image = 'https://www.thinkup.com/assets/images/insights/2014-07/verified.png'; } if ($my_insight->headline) { $this->insight_dao->insertInsight($my_insight); } //Local followers that are neither verified or least likely if (isset($user->location) && $user->location != "") { $local_followers_to_check = $follow_dao->getFollowersFromLocationByDay($instance->network_user_id, $instance->network, $user->location, 0); if (count($local_followers_to_check)) { //Clear out insight vars $my_insight = null; $my_insight = new Insight(); $my_insight->instance_id = $instance->id; $my_insight->date = $this->insight_date; //date of the data this insight applies to $my_insight->text = ''; $my_insight->filename = basename(__FILE__, ".php"); //Create list of user IDs that have already appeared in an insight $follower_ids_already_reported_on = array(); foreach ($least_likely_followers as $follower) { $follower_ids_already_reported_on[] = $follower->id; } foreach ($verified_followers as $follower) { $follower_ids_already_reported_on[] = $follower->id; } //Make sure none of the local followers have already been reported on //debug //print_r($follower_ids_already_reported_on); $local_followers = array(); foreach ($local_followers_to_check as $follower) { if (!in_array($follower->id, $follower_ids_already_reported_on)) { $local_followers[] = $follower; } } //debug //print_r($local_followers); $total_followers = count($local_followers); if ($total_followers) { $headline = "{$this->username} got " . ($total_followers > 1 ? "{$total_followers} new followers" : "a new follower") . " in " . $user->location; if (count($local_followers) == 1) { $header_image = $local_followers[0]->avatar; } else { $header_image = ''; } $my_insight = new Insight(); //REQUIRED: Set the insight's required attributes $my_insight->headline = $headline; // or just set a string like 'Ohai'; $my_insight->slug = 'local_followers'; //slug to label this insight's content $my_insight->instance_id = $instance->id; $my_insight->date = $this->insight_date; //date is often this or $simplified_post_date $my_insight->text = ''; // or just set a strong like "Greetings humans"; $my_insight->header_image = $header_image; $my_insight->filename = basename(__FILE__, ".php"); $my_insight->emphasis = Insight::EMPHASIS_MED; $my_insight->setPeople($local_followers); $this->insight_dao->insertInsight($my_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__); if ($instance->network != 'twitter') { $this->logger->logInfo("Done generating insight (Skipped non-Twitter)", __METHOD__ . ',' . __LINE__); return; } $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("Done generating insight (Skipped)", __METHOD__ . ',' . __LINE__); return; } $this->logger->logInfo("Should generate", __METHOD__ . ',' . __LINE__); $post_dao = DAOFactory::getDAO('PostDAO'); $amp_counts = $post_dao->getRetweetsPerUserInRange($instance->network_user_id, $instance->network, date('Y-m-d', strtotime('January 1')), date('Y-m-d')); $ampees = array(); foreach ($amp_counts as $tmp) { $ampees[$tmp['user_id']] = $tmp['count']; } arsort($ampees); $people = array(); $user_dao = DAOFactory::getDAO('UserDAO'); foreach ($ampees as $aid => $count) { $tmp_user = $user_dao->getDetails($aid, $instance->network); if ($tmp_user) { $people[] = $tmp_user; } } if (count($people) == 0) { $this->logger->logInfo("Done generating insight (Not enough data)", __METHOD__ . ',' . __LINE__); return; } $people = array_slice($people, 0, 3); $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 . ")"; } } $title = 'Who ' . $this->username . ' amplified most on Twitter, ' . $year; if (count($people) == 1) { $text = "Let's turn this tweet up to 11! In {$year}, " . $this->username . " retweeted this user more than any others{$qualified_year}."; } else { $text = "Let's turn this tweet up to 11! In {$year}, " . $this->username . " retweeted these users more than any others{$qualified_year}."; } $insight = new Insight(); $insight->instance_id = $instance->id; $insight->slug = $this->slug; $insight->date = "{$year}-{$this->run_date}"; $insight->headline = $title; $insight->text = $text; $insight->filename = basename(__FILE__, ".php"); $insight->emphasis = Insight::EMPHASIS_HIGH; $insight->setPeople($people); $this->insight_dao->insertInsight($insight); }
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__); $since_date = date("Y-m-d"); $filename = basename(__FILE__, ".php"); //Monthly if ($instance->network == 'facebook') { $day_of_month = 1; } elseif ($instance->network == 'instagram') { $day_of_month = 3; } else { //This doesn't actually run for Twitter since crawler doesn't capture favoriters $day_of_month = 3; } $should_generate_insight = self::shouldGenerateMonthlyInsight('biggest_fans_last_30_days', $instance, $insight_date = $since_date, $regenerate_existing_insight = false, $day_of_month = $day_of_month, null, array('twitter')); $prefix = $instance->network == 'twitter' ? '@' : ''; if ($should_generate_insight) { //it's the right day of the month // Past 30 days $fav_dao = DAOFactory::getDAO('FavoritePostDAO'); $fans = $fav_dao->getUsersWhoFavoritedMostOfYourPosts($instance->network_user_id, $instance->network, 30); if (isset($fans) && sizeof($fans) > 0) { $my_insight = new Insight(); //REQUIRED: Set the insight's required attributes $my_insight->slug = 'biggest_fans_last_30_days'; //slug to label this insight's content $my_insight->instance_id = $instance->id; $my_insight->date = $since_date; //date is often this or $simplified_post_date if (count($fans) == 1) { $my_insight->headline = $prefix . $fans[0]->username . " was {$this->username}'s biggest fan last month"; if ($fans[0]->avatar) { $my_insight->header_image = $fans[0]->avatar; } $my_insight->text = $prefix . $fans[0]->username . " " . $this->terms->getVerb('liked') . " {$this->username}'s " . $this->terms->getNoun('post', InsightTerms::PLURAL) . " the most over the last 30 days."; } else { $my_insight->headline = "{$this->username}'s biggest fans last month"; $my_insight->text = "They " . $this->terms->getVerb('liked') . " {$this->username}'s " . $this->terms->getNoun('post', InsightTerms::PLURAL) . " the most over the last 30 days."; } $my_insight->filename = basename(__FILE__, ".php"); $my_insight->emphasis = Insight::EMPHASIS_HIGH; $my_insight->setPeople($fans); $this->insight_dao->insertInsight($my_insight); } } //Weekly if ($instance->network == 'facebook') { $day_of_week = 3; } elseif ($instance->network == 'instagram') { $day_of_week = 4; } else { //This doesn't actually run for Twitter since crawler doesn't capture favoriters $day_of_week = 6; } $should_generate_insight = self::shouldGenerateWeeklyInsight('biggest_fans_last_7_days', $instance, $insight_date = $since_date, $regenerate_existing_insight = false, $day_of_week = $day_of_week, null, array('twitter')); if ($should_generate_insight) { //it's Sunday // Past 7 days $fav_dao = DAOFactory::getDAO('FavoritePostDAO'); $fans = $fav_dao->getUsersWhoFavoritedMostOfYourPosts($instance->network_user_id, $instance->network, 7); if (isset($fans) && sizeof($fans) > 0) { $my_insight = new Insight(); //REQUIRED: Set the insight's required attributes $my_insight->slug = 'biggest_fans_last_7_days'; //slug to label this insight's content $my_insight->instance_id = $instance->id; $my_insight->date = $since_date; //date is often this or $simplified_post_date if (count($fans) == 1) { $my_insight->headline = $prefix . $fans[0]->username . " was {$this->username}'s biggest admirer last week"; if ($fans[0]->avatar) { $my_insight->header_image = $fans[0]->avatar; } } else { $my_insight->headline = "{$this->username}'s biggest admirers last week"; } $my_insight->text = "Here's who " . $this->terms->getVerb('liked') . " {$this->username}'s " . $this->terms->getNoun('post', InsightTerms::PLURAL) . " most over the past week."; $my_insight->filename = basename(__FILE__, ".php"); $my_insight->emphasis = Insight::EMPHASIS_LOW; $my_insight->setPeople($fans); $this->insight_dao->insertInsight($my_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__); $joined_ts = strtotime($user->joined, TimeHelper::getTime()); $viable = $joined_ts > strtotime('2000-01-1'); $joined_day = date('m-d', $joined_ts); $is_twitter = $instance->network == 'twitter'; if ($viable && $is_twitter && date('m-d', TimeHelper::getTime()) == $joined_day) { $insight = new Insight(); $insight->slug = "twitterbirthday"; $insight->instance_id = $instance->id; $insight->date = $this->insight_date; $insight->emphasis = Insight::EMPHASIS_HIGH; $insight->filename = basename(__FILE__, ".php"); $follow_dao = DAOFactory::getDAO('FollowDAO'); $all_friends = $follow_dao->countTotalFriends($instance->network_user_id, $instance->network); $late_friends = $follow_dao->countTotalFriendsJoinedAfterDate($instance->network_user_id, $instance->network, $user->joined); $years = date('Y', TimeHelper::getTime()) - date('Y', $joined_ts); $ordinal_age = $this->terms->getOrdinalAdverb($years); $insight->headline = "A Very Happy {$ordinal_age} Twitter Birthday!"; $insight->text = $this->username . " joined Twitter {$years} year" . ($years == 1 ? '' : 's') . " ago today"; if ($all_friends > 0 && $late_friends > 0) { $percent_before = floor($late_friends / $all_friends * 100); $insight->text .= ", " . "before " . $percent_before . "% of the people " . $this->username . " follows did."; } else { $insight->text .= "."; } $week_seconds = 60 * 60 * 24 * 7; $followers = $follow_dao->getFriendsJoinedInTimeFrame($user->user_id, $instance->network, date('Y-m-d', $joined_ts - $week_seconds), date('Y-m-d', $joined_ts + $week_seconds)); $just_before = null; $just_after = null; $last_user = null; foreach ($followers as $follower) { if (strtotime($follower->joined, TimeHelper::getTime()) > $joined_ts) { $just_after = $follower; $just_before = $last_user; break; } $last_user = $follower; } if (!$just_after && $last_user) { $just_before = $last_user; } $bonus_text = array(); $people = array(); if ($just_before) { $time = TimeHelper::secondsToGeneralTime(abs($joined_ts - strtotime($just_before->joined, TimeHelper::getTime()))); $bonus_text[] = "@" . $just_before->username . " just beat " . $this->username . ", joining {$time} earlier"; $people[] = $just_before; } if ($just_after) { $time = TimeHelper::secondsToGeneralTime(abs($joined_ts - strtotime($just_after->joined, TimeHelper::getTime()))); $bonus_text[] = "@" . $just_after->username . " was a little slower, getting on Twitter {$time} later"; $people[] = $just_after; } if (count($bonus_text)) { $insight->text .= " " . join(', and ', $bonus_text) . "."; $insight->setPeople($people); } $res = $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__); $regenerate = false; //testing // $regenerate = true; $year = date('Y'); $should_generate_insight = self::shouldGenerateEndOfYearAnnualInsight($this->slug, $instance, $insight_date = "{$year}-{$this->run_date}", $regenerate, $day_of_year = $this->run_date, $count_related_posts = null, array('facebook')); if (!$should_generate_insight) { $this->logger->logInfo("Done generating insight (Skipped)", __METHOD__ . ',' . __LINE__); return; } $this->logger->logInfo("Should generate", __METHOD__ . ',' . __LINE__); $fave_dao = DAOFactory::getDAO('FavoritePostDAO'); $fave_counts = $fave_dao->getCountOfFavoritedUsersInRange($instance->network_user_id, $instance->network, date('Y-m-d', strtotime('January 1')), date('Y-m-d')); $favees = array(); foreach ($fave_counts as $tmp) { $favees[$tmp['user_id']] = $tmp['count']; } arsort($favees); $people = array(); $user_dao = DAOFactory::getDAO('UserDAO'); foreach ($favees as $aid => $count) { $tmp_user = $user_dao->getDetails($aid, $instance->network); if ($tmp_user) { $people[] = $tmp_user; } } if (count($people) == 0) { $this->logger->logInfo("Done generating insight (Not enough data)", __METHOD__ . ',' . __LINE__); return; } $people = array_slice($people, 0, 3); $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 . ")"; } } if (count($people) == 1) { $title = $this->username . "'s most-liked person on " . ucfirst($instance->network) . ", {$year}"; $text = $this->getVariableCopy(array("Every time you like a %post, a little red heart lights up. " . $this->username . " gave the most hearts to @" . $people[0]->username . " in {$year}" . $qualified_year . ".")); } else { $title = $this->username . "'s most-liked people on " . ucfirst($instance->network) . ", {$year}"; $text = $this->getVariableCopy(array("Every time you like a %post, a little red heart lights up. " . $this->username . " gave the most hearts to these fine folks in {$year}" . $qualified_year . ".")); } $insight = new Insight(); $insight->instance_id = $instance->id; $insight->slug = $this->slug; $insight->date = "{$year}-{$this->run_date}"; $insight->headline = $title; $insight->text = $text; $insight->filename = basename(__FILE__, ".php"); $insight->emphasis = Insight::EMPHASIS_HIGH; $insight->setPeople($people); $this->insight_dao->insertInsight($insight); }
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__); if ($instance->network == 'twitter') { $day_of_week = 3; } else { $day_of_week = 6; } $should_generate_insight = self::shouldGenerateWeeklyInsight('interactions', $instance, $insight_date = 'today', $regenerate_existing_insight = false, $day_of_week = 3, count($last_week_of_posts), $excluded_networks = array('facebook', 'google+', 'foursquare', 'youtube')); if ($should_generate_insight) { $user_dao = DAOFactory::getDAO('UserDAO'); $mentions_count = array(); $mentions_info = array(); $mentions_avatars = array(); $insight_data = array(); $insight_text = ''; if ($instance->network == 'twitter') { $talk_time = 15; } else { $talk_time = 38; } foreach ($last_week_of_posts as $post) { if ($post->in_reply_to_user_id && $instance->network_user_id != $post->in_reply_to_user_id) { $mentioned_user = $user_dao->getDetails($post->in_reply_to_user_id, $instance->network); if (isset($mentioned_user)) { $mention_in_post = $instance->network == 'twitter' ? '@' : ''; $mention_in_post .= $mentioned_user->username; $mentions_info[$mention_in_post] = $mentioned_user; // Update mention count if (array_key_exists($mention_in_post, $mentions_count)) { $mentions_count[$mention_in_post]++; } else { $mentions_count[$mention_in_post] = 1; } } } } if (count($mentions_count)) { // Get most mentioned user arsort($mentions_count); $most_mentioned_user = each($mentions_count); // Add mentions to dataset $users_mentioned = array(); foreach (array_slice($mentions_count, 0, 10) as $mention => $count) { $mention_info['mention'] = $mention; $mention_info['count'] = $count; $mention_info['user'] = $mentions_info[$mention]; $users_mentioned[] = $mention_info; } } if (isset($most_mentioned_user) && $talk_time * $most_mentioned_user['value'] >= 60) { $headline = $this->username . " replied to " . $most_mentioned_user['key'] . " <strong>" . $this->terms->getOccurrencesAdverb($most_mentioned_user['value']) . "</strong> last week"; $conversation_seconds = $this->terms->getOccurrencesAdverb($most_mentioned_user['value']) * $talk_time; $milestones = $this->convertSecondsToMilestone($conversation_seconds); $insight_text = 'Time having good conversation is time well spent.'; // $header_image = $users_mentioned[0][user]->avatar; $header_image = $users_mentioned[0]["user"]->avatar; //Instantiate the Insight object $my_insight = new Insight(); //REQUIRED: Set the insight's required attributes $my_insight->instance_id = $instance->id; $my_insight->slug = 'interactions'; //slug to label this insight's content $my_insight->date = $this->insight_date; //date of the data this insight applies to $my_insight->headline = $headline; $my_insight->text = $insight_text; $my_insight->header_image = $header_image; $my_insight->emphasis = Insight::EMPHASIS_MED; $my_insight->filename = basename(__FILE__, ".php"); $my_insight->setPeople($users_mentioned); $my_insight->setMilestones($milestones); $this->insight_dao->insertInsight($my_insight); } } $this->logger->logInfo("Done generating insight", __METHOD__ . ',' . __LINE__); }