Skip to content

Commit

Permalink
New insight: Lorenzo Llamas
Browse files Browse the repository at this point in the history
  • Loading branch information
anildash authored and ginatrapani committed Feb 27, 2015
1 parent d475563 commit e26432a
Show file tree
Hide file tree
Showing 2 changed files with 258 additions and 0 deletions.
126 changes: 126 additions & 0 deletions webapp/plugins/insightsgenerator/insights/llamas.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
<?php
/*
Plugin Name: Lorenzo Llamas
Description: Did you talk about llamas?
When: February 27, 2015
*/
/**
*
* ThinkUp/webapp/plugins/insightsgenerator/insights/llamas.php
*
* Copyright (c) 2015 Anil Dash
*
* LICENSE:
*
* This file is part of ThinkUp (http://thinkup.com).
*
* ThinkUp is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
* License as published by the Free Software Foundation, either version 2 of the License, or (at your option) any
* later version.
*
* ThinkUp is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with ThinkUp. If not, see
* <http://www.gnu.org/licenses/>.
*
* Copyright (c) 2014-2015 Chris Moyer, Anil Dash
*
* @author Chris Moyer chris@inarow.net, Anil Dash anil@thinkup.com
* @license http://www.gnu.org/licenses/gpl.html
* @copyright 2014-2015 Chris Moyer, Anil Dash
*/

class LlamasInsight extends InsightPluginParent implements InsightPlugin {
/**
* Slug for this insight
**/
var $slug = 'llamas';
/**
* Date to run this insight
**/

public function generateInsight(Instance $instance, User $user, $last_week_of_posts, $number_days) {
if (Utils::isTest() || date("Y-m-d") == '2015-02-27') {
parent::generateInsight($instance, $user, $last_week_of_posts, $number_days);
$this->logger->logInfo("Begin generating insight", __METHOD__.','.__LINE__);

$hero_image = array(
'url' => 'https://www.thinkup.com/assets/images/insights/2015-02/llama.jpg',
'alt_text' => 'Llama',
'credit' => 'Photo: Eric Kilby',
'img_link' => 'https://www.flickr.com/photos/ekilby/8564867495/'
);

$should_generate_insight = self::shouldGenerateWeeklyInsight(
$this->slug,
$instance,
$insight_date = 'today',
$regenerate_existing_insight=false,
$day_of_week=5,
count($last_week_of_posts)
);

if ($should_generate_insight) {
$this->logger->logInfo("Should generate", __METHOD__.','.__LINE__);
$post_dao = DAOFactory::getDAO('PostDAO');

$topics = array(
'llama' => array("llama","llamas"),
);

$matches = array();
foreach ($last_week_of_posts as $post) {
foreach ($topics as $key => $strings) {
foreach ($strings as $string) {
if (preg_match_all('/\b'.$string.'\b/i', $post->post_text) > 0) {
$matches[$key] = array('term'=>$string, 'post'=>$post);
unset($topics[$key]);
break;
}
}
}
}

$insight = new Insight();

if (count($matches) == 0) {
$headline = $this->username.' managed to avoid llamageddon!';
$insight_text = "It seems like half the internet was " .
"<a href='http://www.theverge.com/2015/2/26/8116693/live-the-internet-is-going-bananas-".
"for-this-llama-chase'>talking about runaway llamas</a> " .
'yesterday. Kudos to ' . $this->username . ' for showing a llama restraint.';
$posts = null;
}
else {
$headline = $this->username." showed a whole llama love";
$posts = array();
$mentioned = array();
foreach ($matches as $m) {
if (count($posts) < 3) {
$posts[] = $m['post'];
}
}
$insight_text = "Two runaway llamas <a href='http://www.theverge.com/2015/2/26/8116693/live-".
"the-internet-is-going-bananas-for-this-llama-chase'>took over Twitter yesterday</a>" .
", and like a llama people, " . $this->username . " couldn't resist.";
}

$insight->instance_id = $instance->id;
$insight->slug = $this->slug;
$insight->date = "2015-02-27";
$insight->headline = $headline;
$insight->text = $insight_text;
$insight->filename = basename(__FILE__, ".php");
$insight->emphasis = Insight::EMPHASIS_HIGH;
$insight->setHeroImage($hero_image);
$this->insight_dao->insertInsight($insight);
}
}
$this->logger->logInfo("Done generating insight", __METHOD__.','.__LINE__);
}
}

$insights_plugin_registrar = PluginRegistrarInsights::getInstance();
$insights_plugin_registrar->registerInsightPlugin('LlamasInsight');
132 changes: 132 additions & 0 deletions webapp/plugins/insightsgenerator/tests/TestOfLlamasInsight.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
<?php
/**
*
* ThinkUp/webapp/plugins/insightsgenerator/tests/TestOfLlamasInsight.php
*
* Copyright (c) 2012-2015 Gina Trapani
*
* LICENSE:
*
* This file is part of ThinkUp (http://thinkup.com).
*
* ThinkUp is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
* License as published by the Free Software Foundation, either version 2 of the License, or (at your option) any
* later version.
*
* ThinkUp is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with ThinkUp. If not, see
* <http://www.gnu.org/licenses/>.
*
* Test for the LlamasInsight class.
*
* Copyright (c) 2014-2015 Chris Moyer
*
* @author Chris Moyer chris@inarow.net
* @license http://www.gnu.org/licenses/gpl.html
* @copyright 2014-2015 Adam Pash
*/

require_once dirname(__FILE__) . '/../../../../tests/init.tests.php';
require_once THINKUP_WEBAPP_PATH.'_lib/extlib/simpletest/autorun.php';
require_once THINKUP_WEBAPP_PATH.'_lib/extlib/simpletest/web_tester.php';
require_once THINKUP_ROOT_PATH. 'webapp/plugins/insightsgenerator/model/class.InsightPluginParent.php';
require_once THINKUP_ROOT_PATH. 'webapp/plugins/insightsgenerator/insights/llamas.php';

class TestOfLlamasInsight extends ThinkUpInsightUnitTestCase {

public function setUp(){
parent::setUp();
$instance = new Instance();
$instance->id = 10;
$instance->network_username = 'rihanna';
$instance->author_id = '18';
$instance->network_user_id = '18';
$instance->network = 'twitter';
$this->instance = $instance;
}

public function tearDown() {
parent::tearDown();
}

public function testNoLlamas() {
$insight_plugin = new LlamasInsight();
$insight_dao = new InsightMySQLDAO();
$insight_plugin->generateInsight($this->instance, new User(), array(), 3);
$result = $insight_dao->getInsight($insight_plugin->slug, $this->instance->id, date('Y-m-d'));
$this->assertNotNull($result);
$this->assertEqual($result->headline, "@rihanna managed to avoid llamageddon!");
$this->assertEqual($result->text, "It seems like half the internet was " .
"<a href='http://www.theverge.com/2015/2/26/8116693/live-the-internet-is-going-bananas-for-this-llama-".
"chase'>talking about runaway llamas</a> ".
"yesterday. Kudos to @rihanna for showing a llama restraint.");
$data = unserialize($result->related_data);
$this->assertNull($data['posts']);

$this->dumpRenderedInsight($result, $this->instance, "No Llamas");
}

public function testYesLlamas() {
$year = date('Y');
$builders = array();
$builders[] = FixtureBuilder::build('posts',
array(
'pub_date' => $year.'-02-26', 'post_id' => 1, 'author_username' => $this->instance->network_username,
'author_user_id' => $this->instance->network_user_id, 'network' => $this->instance->network,
'post_text' => 'This Llama stuff is crazy!',
)
);
$post = new Post();
$post->pub_date = '2015-02-23';
$post->post_text = 'This Llama stuff is crazy!';
$post->author_username = $this->instance->network_username;
$post->author_user_id = $this->instance->network_user_id;

$insight_plugin = new LlamasInsight();
$insight_dao = new InsightMySQLDAO();
$insight_plugin->generateInsight($this->instance, new User(), array($post), 3);
$result = $insight_dao->getInsight($insight_plugin->slug, $this->instance->id, date('Y-m-d'));
$this->assertNotNull($result);

$this->assertEqual($result->headline, "@rihanna showed a whole llama love");
$this->assertEqual($result->text, "Two runaway llamas <a href='http://www.theverge.com/2015/2/26/8116693"
."/live-the-internet-is-going-bananas-for-this-llama-chase'>took over Twitter yesterday</a>,"
." and like a llama people, @rihanna couldn't resist.");

$this->dumpRenderedInsight($result, $this->instance, "Got Llamas");
}

public function testWordInWord() {
$year = date('Y');
$builders = array();
$builders[] = FixtureBuilder::build('posts',
array(
'pub_date' => $year.'-02-26', 'post_id' => 1, 'author_username' => $this->instance->network_username,
'author_user_id' => $this->instance->network_user_id, 'network' => $this->instance->network,
'post_text' => 'Bollamas Lanches is a diner in Brazil.',
)
);
$post = new Post();
$post->pub_date = '2015-02-26';
$post->post_text = 'Bollamas Lanches is a diner in Brazil.';
$post->author_username = $this->instance->network_username;
$post->author_user_id = $this->instance->network_user_id;

$insight_plugin = new LlamasInsight();
$insight_dao = new InsightMySQLDAO();
$insight_plugin->generateInsight($this->instance, new User(), array($post), 3);
$result = $insight_dao->getInsight($insight_plugin->slug, $this->instance->id, date('Y-m-d'));
$this->assertNotNull($result);

$this->assertNotEqual($result->headline, "@rihanna showed a whole llama love");
$this->assertNotEqual($result->text, "Two runaway llamas took over Twitter yesterday, and @rihanna was "
. "no exception.");
$data = unserialize($result->related_data);
$this->assertNotEqual(1, count($data['posts']));

$this->dumpRenderedInsight($result, $this->instance, "No Llamas, just bollamas");
}
}

0 comments on commit e26432a

Please sign in to comment.