Skip to content

Commit

Permalink
context tweets
Browse files Browse the repository at this point in the history
  • Loading branch information
allenan committed May 21, 2012
1 parent f43777c commit bcdccc7
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 60 deletions.
9 changes: 6 additions & 3 deletions ajax/search.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
$pages = (isset($_GET['pages'])) ? ((int)$_GET['pages'])/100 : 5 ;
//$pages = 12;
//$pages = ((int)$_GET['pages'])/100;
$q = (isset($_GET['q'])) ? $_GET['q'] : "bieber" ;
$q = (isset($_GET['q'])) ? $_GET['q'] : "cubs" ;
$args = array(
'q' => $q,
'since_id' => '0',
Expand Down Expand Up @@ -64,12 +64,14 @@
foreach ($results as $result) {
$date = strtotime($result['created_at']);
$from_user = $result['from_user_id'];

$tweet = array('text' => $result['text'], 'from_user_id' => $from_user, 'from_user_name' => $result['from_user_name'], 'from_user' => $result['from_user'], 'created_at' => $result['created_at'], 'profile_image_url' => $result['profile_image_url']);
//$result['text'] = str_replace(PHP_EOL, '', $result['text']);
//print_r($result);
//echo "{$result['id_str']}\t{$date}\t{$result['from_user']}\t\t{$result['text']}" . PHP_EOL;
foreach ($result['entities']['hashtags'] as $hashtag ) {
if ($hashtag['text'] != '') {
$tweetSweep->addHashtag($hashtag['text'], $date, $from_user);
$tweetSweep->addHashtag($hashtag['text'], $date, $from_user, $tweet);
//echo $hashtag['text'];
//echo '<br/>';
}
Expand Down Expand Up @@ -116,9 +118,10 @@
<tbody>
<?php $i = 0; ?>
<?php foreach ($tweetSweep->hashtagStruct as $index => $h): ?>

<tr>
<td><a class="add-btn" data-content="<?php echo $h['text']; ?>" data-prefix="#" href="#"><img src="img/add.png"/></a></td>
<td><a class="hashtagInfo" data-toggle="modal" href="#myModal" target="_blank" data-index="<?php echo $index ?>"><?php echo '#'.$h['text']?></a></td>
<td><a class="hashtagInfo" data-toggle="modal" href="#myModal" target="_blank" data-index="<?php echo $index ?>" ><?php echo '#'.$h['text']?></a></td>
<td><?php echo $h['count']?></td>
</tr>
<?php $i++;?>
Expand Down
7 changes: 7 additions & 0 deletions js/session.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php
session_start();
$hashtagStruct = json_encode(unserialize($_SESSION['var_cache']));
?>
function setHashtagStruct(){
hashtagStruct = <?=$hashtagStruct;?>;
}
53 changes: 52 additions & 1 deletion js/tweetsweep.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,29 @@
function linkify(inputText) {
var replaceText, replacePattern1, replacePattern2, replacePattern3;

//URLs starting with http://, https://, or ftp://
replacePattern1 = /(\b(https?|ftp):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/gim;
replacedText = inputText.replace(replacePattern1, '<a href="$1" target="_blank">$1</a>');

//URLs starting with "www." (without // before it, or it'd re-link the ones done above).
replacePattern2 = /(^|[^\/])(www\.[\S]+(\b|$))/gim;
replacedText = replacedText.replace(replacePattern2, '$1<a href="http://$2" target="_blank">$2</a>');

//Change email addresses to mailto:: links.
replacePattern3 = /(\w+@[a-zA-Z_]+?\.[a-zA-Z]{2,6})/gim;
replacedText = replacedText.replace(replacePattern3, '<a href="mailto:$1">$1</a>');

//Change hashtags to links to twitter search
replacePattern4 = /#([a-zA-Z0-9]*)/gim;
replacedText = replacedText.replace(replacePattern4, '<a href="http://twitter.com/#!/search/%23$1" target="_blank">#$1</a>');

//match @ mentions
replacePattern5 = /@([a-zA-Z0-9]*)/gim;
replacedText = replacedText.replace(replacePattern5, '<a href="http://twitter.com/#!/$1" target="_blank">@$1</a>');

return replacedText
}

//Setup
$.ajaxSetup ({
cache: false
Expand Down Expand Up @@ -102,6 +128,11 @@ function search(q){
assignAdds();
assignHashtagModal();
plotTime();
$.ajax({
url: 'js/session.php',
dataType: "script",
success: function() {setHashtagStruct();}
});
});

// var scriptUrl = "js/flot-tweetsweep.php";
Expand Down Expand Up @@ -258,9 +289,29 @@ function updateUserTimeline () {
$(this).click(function(){
var index = $(this).data("index");
var hashtag = $(this).html();

$('#myModal h3').html(hashtag + " Details");
plotTime(index);
//console.log($(this));
console.log($(this).data('index'));

$('#context-tweets').find('tbody').empty();

$.each(hashtagStruct[index].tweets, function(i, t){
$('#context-tweets').find('tbody')
.append($('<tr>')
.append($('<img>').attr('src', t.profile_image_url))
.append($('<td>').html(
'<div><strong>'+t.from_user_name+' '+'</strong>'+'@'+t.from_user+'</div>'+
'<p>'+linkify(t.text)+'</p>'+
'<small>'+t.created_at+'</small>'
))
);
});

// $('#context-tweets').find('tbody')
// .append($('<tr>')
// .append($('<td>').append($('<img>').attr('src', 'http://placehold.it/20x20')))
// );
});
});
}
Expand Down
5 changes: 4 additions & 1 deletion lib/TweetSweep.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ function __construct()
$this->userMentionStruct = array();
}

function addHashtag($hashtag, $date, $from_user)
function addHashtag($hashtag, $date, $from_user, $tweet)
{
$date = $this->roundDate($date);
//see if hashtag exists in the struct
Expand All @@ -25,11 +25,13 @@ function addHashtag($hashtag, $date, $from_user)
//If the time is already a key, then increment it
$this->hashtagStruct[$hashtag]['times'][strval($date)] = array('time' => $date, 'realtime' => date("h:i:s A T, M jS, Y", $date), 'count' => $this->hashtagStruct[$hashtag]['times'][strval($date)]['count'] + 1);
$this->hashtagStruct[$hashtag]['users'][] = $from_user;
$this->hashtagStruct[$hashtag]['tweets'][] = $tweet;
} else {
//echo 'not found<br/>';
//It the key is not defined, make a new time
$this->hashtagStruct[$hashtag]['times'][strval($date)] = array('time' => $date, 'realtime' => date("h:i:s A T, M jS, Y", $date), 'count' => 1);
$this->hashtagStruct[$hashtag]['users'][] = $from_user;
$this->hashtagStruct[$hashtag]['tweets'][] = $tweet;
}


Expand All @@ -43,6 +45,7 @@ function addHashtag($hashtag, $date, $from_user)
//make a new time key/value
$this->hashtagStruct[$hashtag]['times'][strval($date)] = array('time' => $date, 'realtime' => date("h:i:s A T, M jS, Y", $date), 'count' => 1);
$this->hashtagStruct[$hashtag]['users'][] = $from_user;
$this->hashtagStruct[$hashtag]['tweets'][] = $tweet;
}
}

Expand Down
63 changes: 8 additions & 55 deletions templates/components/modal.phtml
Original file line number Diff line number Diff line change
@@ -1,65 +1,18 @@
<div id="myModal" class="modal hide fade">
<div id="myModal" class="modal hide fade">
<div class="modal-header">
<button class="close" data-dismiss="modal">&times;</button>
<h3>#Hashtag Details</h3>
</div>
<div class="modal-body">
<div id="flot" style="width:420px;height:200px;"></div>


<hr>

<table id="user-table" style="width:100%;">
<tbody><tr>
<td><h4>Tweets</h4></td>

</tr>
<!-- <pre> -->
<!-- </pre> -->
<tr style="border-top:1px solid #e8e8e8; border-bottom:1px solid #e8e8e8;">
<td>
<img src="https://twimg0-a.akamaihd.net/profile_images/1744682717/Cubs_Logo_normal.jpg">
</td>
<td>
<div><strong>Chicago Cubs </strong>@Cubs</div>
<p class="js-tweet-text">Tonight's <a href="/#!/search/%23Cubs" title="#Cubs" class=" twitter-hashtag pretty-link" data-query-source="hashtag_click"><s><strong>#</strong></s><strong><b>Cubs</b></strong></a> vs. <a href="/#!/search/%23Reds" title="#Reds" class=" twitter-hashtag pretty-link" data-query-source="hashtag_click"><s>#</s><b>Reds</b></a> game in Cincinnati has been postponed due to rain. A makeup date has not been announced.</p>
<small><a href="https://twitter.com/#!/Cubs/status/197495131060977664">1 May</a> <span>via Twitter for iPhone</span></small>
</td>
</tr>



<!-- <pre> -->
<!-- </pre> -->
<tr style="border-top:1px solid #e8e8e8; border-bottom:1px solid #e8e8e8;">
<td>
<img src="https://twimg0-a.akamaihd.net/profile_images/1928143605/BB_Twitter_normal.png">
</td>
<td>
<div><strong>Bullpen Brian </strong>@bullpenbrian</div>
<p class="js-tweet-text"><a class=" twitter-atreply pretty-link" data-screen-name="cliffy46405" href="/#!/cliffy46405" rel="nofollow"><s>@</s><b>cliffy46405</b></a> Trying to hang our hat on something positive for DeWitt--beside his beard! <a href="/#!/search/%23Cubs" title="#Cubs" class=" twitter-hashtag pretty-link" data-query-source="hashtag_click"><s><strong>#</strong></s><strong><b>Cubs</b></strong></a></p>
<small><a href="https://twitter.com/_andrew_allen_/189511565081456640">2 May</a> <span>via HootSuite</span></small>
</td>
</tr>



<!-- <pre> -->
<!-- </pre> -->
<tr style="border-top:1px solid #e8e8e8; border-bottom:1px solid #e8e8e8;">
<td>
<img src="https://twimg0-a.akamaihd.net/profile_images/87216862/DSCF1595_normal.jpg">
</td>
<td>
<div><strong>Phil Rizzuto </strong>@philrizzuto</div>
<p class="js-tweet-text">Great foul ball seats! ( <a href="/#!/search/%23reds" title="#reds" class=" twitter-hashtag pretty-link" data-query-source="hashtag_click"><s>#</s><b>reds</b></a> vs <a href="/#!/search/%23cubs" title="#cubs" class=" twitter-hashtag pretty-link" data-query-source="hashtag_click"><s><strong>#</strong></s><strong><b>cubs</b></strong></a> game w/ <a href="http://t.co/r6gtFeMA" data-expanded-url="http://MLB.com" title="http://MLB.com" target="_blank" rel="nofollow" class="twitter-timeline-link" data-ultimate-url="http://mlb.mlb.com/index.jsp">MLB.com</a> <a href="http://t.co/AQGBq0G8" data-expanded-url="http://m.mlb.com/apps" title="http://m.mlb.com/apps" target="_blank" rel="nofollow" class="twitter-timeline-link" data-ultimate-url="http://m.mlb.com/apps">m.mlb.com/apps</a>)</p>
<small><a href="https://twitter.com/_andrew_allen_/189494135999307776">2 May</a> <span>via MLB.com At the Ballpark on iOS</span></small>
</td>
</tr>



</tbody></table>
<table id="context-tweets" style="width:100%;">
<tbody>
<tr>
<td><h4>Tweets</h4></td>
</tr>
</tbody>
</table>

</div>

Expand Down
5 changes: 5 additions & 0 deletions templates/dashboard.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@
-moz-border-radius: 5px;
border-radius: 5px;
}
#context-tweets tbody tr{
border-top:1px solid #e8e8e8;
border-bottom:1px solid #e8e8e8;
}
</style>
<link href="css/bootstrap-responsive.css" rel="stylesheet">
<link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.18/themes/base/jquery-ui.css" rel="stylesheet">
Expand Down Expand Up @@ -174,6 +178,7 @@
<script src="js/tweetsweep.js"></script>
<script src="js/flot/jquery.flot.js"></script>
<script src="js/doTimeout.jQuery.js"></script>
<script type="text/javascript" src="js/session.php"></script>

<script src="js/flot-tweetsweep.js"></script>

Expand Down

0 comments on commit bcdccc7

Please sign in to comment.