Skip to content

Commit

Permalink
Pulling Geo Data From Tweets
Browse files Browse the repository at this point in the history
Closes #416 - This also allows us to add location to any message type
with the DB schema change.
  • Loading branch information
brianherbert committed Apr 19, 2012
1 parent 086191e commit cb7ecd4
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 21 deletions.
2 changes: 1 addition & 1 deletion application/config/version.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@
* The Ushahidi Engine DB revision number
* Increments when changes are made to the Ushahidi DB schema.
*/
$config['ushahidi_db_version'] = "82";
$config['ushahidi_db_version'] = "83";
10 changes: 8 additions & 2 deletions application/controllers/admin/reports.php
Original file line number Diff line number Diff line change
Expand Up @@ -489,8 +489,14 @@ public function edit($id = FALSE, $saved = FALSE)
$form['person_first'] = $message->reporter->reporter_first;
$form['person_last'] = $message->reporter->reporter_last;

// Does the sender of this message have a location?
if ($message->reporter->location->loaded)
// Does the message itself have a location?
if ($message->latitude != NULL AND $message->longitude != NULL)
{
$form['latitude'] = $message->latitude;
$form['longitude'] = $message->longitude;
}
// As a fallback, does the sender of this message have a location?
elseif ($message->reporter->location->loaded)
{
$form['location_id'] = $message->reporter->location->id;
$form['latitude'] = $message->reporter->location->latitude;
Expand Down
41 changes: 28 additions & 13 deletions application/controllers/scheduler/s_twitter.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ class S_Twitter_Controller extends Controller {

// Cache instance
protected $cache;

public function __construct()
{
parent::__construct();

// Load cache
$this->cache = new Cache;
}
Expand Down Expand Up @@ -67,9 +67,9 @@ public function index()
curl_setopt($curl_handle,CURLOPT_RETURNTRANSFER,1); //Set curl to store data in variable instead of print
$buffer = curl_exec($curl_handle);
curl_close($curl_handle);

$have_results = $this->add_hash_tweets($buffer); //if FALSE, we will drop out of the loop

$page++;
}
}
Expand All @@ -87,20 +87,23 @@ private function add_hash_tweets($data)
{
return false;
}

$services = new Service_Model();
$service = $services->where('service_name', 'Twitter')->find();

if ( ! $service)
{
$this->_unlock();
return false;
}

$tweets = json_decode($data, false);
if ( ! $tweets)
{
$this->_unlock();
return false;
}

if (isset($tweets->{'error'}))
{
$this->_unlock();
Expand Down Expand Up @@ -135,11 +138,21 @@ private function add_hash_tweets($data)
$reporter->save();
}

if ($reporter->level_id > 1 &&
if ($reporter->level_id > 1 &&
count(ORM::factory("message")
->where("service_messageid = '".$tweet->{'id_str'}."'")
->find_all()) == 0)
{

// Grab geo data if it exists from the tweet
$tweet_lat = null;
$tweet_lon = null;
if ($tweet->{'geo'} != null)
{
$tweet_lat = $tweet->{'geo'}->coordinates[0];
$tweet_lon = $tweet->{'geo'}->coordinates[1];
}

// Save Tweet as Message
$message = new Message_Model();
$message->parent_id = 0;
Expand All @@ -153,11 +166,13 @@ private function add_hash_tweets($data)
$tweet_date = date("Y-m-d H:i:s",strtotime($tweet->{'created_at'}));
$message->message_date = $tweet_date;
$message->service_messageid = $tweet->{'id_str'};
$message->latitude = $tweet_lat;
$message->longitude = $tweet_lon;
$message->save();

// Action::message_twitter_add - Twitter Message Received!
Event::run('ushahidi_action.message_twitter_add', $message);

// Auto-Create A Report if Reporter is Trusted
$reporter_weight = $reporter->level->level_weight;
$reporter_location = $reporter->location;
Expand Down Expand Up @@ -197,23 +212,23 @@ private function add_hash_tweets($data)
}
}
}

$this->_unlock();
return true;
}

private function _lock()
{
// *************************************
// Create A 15 Minute RETRIEVE LOCK
// Create A 5 Minute RETRIEVE LOCK
// This lock is released at the end of execution
// Or expires automatically
$twitter_lock = $this->cache->get(Kohana::config('settings.subdomain')."_twitter_lock");
if ( ! $twitter_lock)
{
// Lock doesn't exist
$timestamp = time();
$this->cache->set(Kohana::config('settings.subdomain')."_twitter_lock", $timestamp, array("twitter"), 900);
$this->cache->set(Kohana::config('settings.subdomain')."_twitter_lock", $timestamp, array("twitter"), 300);
return false;
}
else
Expand All @@ -222,7 +237,7 @@ private function _lock()
return true;
}
}

private function _unlock()
{
$this->cache->delete(Kohana::config('settings.subdomain')."_twitter_lock");
Expand Down
15 changes: 11 additions & 4 deletions application/views/admin/messages.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,9 @@
$message_date = date('Y-m-d H:i', strtotime($message->message_date));
$message_type = $message->message_type;
$message_level = $message->message_level;

$latitude = $message->latitude;
$longitude = $message->longitude;

$level_id = $message->reporter->level_id;
?>
<tr <?php if ($message_level == "99") {
Expand All @@ -137,7 +139,7 @@
<p><a href="javascript:preview('message_preview_<?php echo $message_id?>')"><?php echo Kohana::lang('ui_main.preview_message');?></a></p>
<div id="message_preview_<?php echo $message_id?>" style="display:none;">
<?php echo $message_detail; ?>

<?php
// Retrieve Attachments if any
foreach($message->media as $photo)
Expand Down Expand Up @@ -196,11 +198,16 @@
<?php
if ($message_type == 2)
{
?><li class="none-separator">To: <strong><?php echo $message_to; ?></strong><?php
?><li class="none-separator"><?php echo Kohana::lang('ui_admin.to');?>: <strong><?php echo $message_to; ?></strong><?php
}
else
{
?><li class="none-separator">From: <a href="<?php echo url::site()."admin/messages/reporters/index/".$service_id."?k=".urlencode($message_from);?>"><strong class="reporters_<?php echo $level_id?>"><?php echo $message_from; ?></strong></a><?php
?><li class="none-separator"><?php echo Kohana::lang('ui_admin.from');?>: <a href="<?php echo url::site()."admin/messages/reporters/index/".$service_id."?k=".urlencode($message_from);?>"><strong class="reporters_<?php echo $level_id?>"><?php echo $message_from; ?></strong></a><?php
}

if ($latitude != NULL AND $longitude != NULL)
{
?><li class="none-separator"> @ <?php echo $latitude; ?>,<?php echo $longitude; ?></li><?php
}
?>
</ul>
Expand Down
5 changes: 5 additions & 0 deletions sql/upgrade82-83.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
ALTER TABLE `message` ADD `latitude` DOUBLE NULL DEFAULT NULL;

ALTER TABLE `message` ADD `longitude` DOUBLE NULL DEFAULT NULL;

UPDATE `settings` SET `db_version` = '83' WHERE `id` = 1 LIMIT 1;
4 changes: 3 additions & 1 deletion sql/ushahidi.sql
Original file line number Diff line number Diff line change
Expand Up @@ -998,6 +998,8 @@ CREATE TABLE IF NOT EXISTS `message` (
`message_type` tinyint(4) DEFAULT '1' COMMENT '1 - INBOX, 2 - OUTBOX (From Admin), 3 - DELETED',
`message_date` datetime DEFAULT NULL,
`message_level` tinyint(4) DEFAULT '0' COMMENT '0 - UNREAD, 1 - READ, 99 - SPAM',
`latitude` double DEFAULT NULL,
`longitude` double DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `user_id` (`user_id`),
KEY `incident_id` (`incident_id`),
Expand Down Expand Up @@ -1448,4 +1450,4 @@ CREATE TABLE IF NOT EXISTS `verified` (
*
*/
UPDATE `settings` SET `ushahidi_version` = '2.2.1' WHERE `id`=1 LIMIT 1;
UPDATE `settings` SET `db_version` = '82' WHERE `id` = 1 LIMIT 1;
UPDATE `settings` SET `db_version` = '83' WHERE `id` = 1 LIMIT 1;

0 comments on commit cb7ecd4

Please sign in to comment.