Skip to content

Instantly share code, notes, and snippets.

@amgando
Created August 8, 2010 05:49
Show Gist options
  • Save amgando/513637 to your computer and use it in GitHub Desktop.
Save amgando/513637 to your computer and use it in GitHub Desktop.
<?php
include_once('plugins/pog/objects/class.trend.php');
include_once('plugins/pog/objects/class.trendhistory.php');
include_once('plugins/pog/objects/class.database.php');
include_once('plugins/pog/configuration.php');
function get_trend_data($hotness, $direction)
{
$t = new Trend();
return $t->GetList(array(array("hotness", "=", "$hotness"), array("direction", "=", "$direction")), 'created_on', false, 5);
}
$img_overview = "images/bw_overview";
$img_graph = "images/bw_graph";
$img_history = "images/bw_history";
$img_newsarchive = "images/bw_newsarchive";
$hotness = array("Volcanic", "On_Fire", "Spicy");
$direction = array("new", "up", "down");
?>
<html>
<head>
<LINK REL=StyleSheet HREF="style.css" TYPE="text/css" MEDIA=screen>
</head>
<body>
<?php foreach ($hotness as $h): ?>
<h1><i><?= $h ?></i> keywords</h1>
<?php foreach ($direction as $d): ?>
<?php $current = get_trend_data($h, $d) ?>
<?php if(count($current) != 0): ?>
<h3><?= $d ?></h3>
<table>
<?php foreach ($current as $trend): ?>
<tr><td width="200"><?= $trend->keyword ?></td>
<td><a href="<?= $trend->url_overview ?>"><img alt="overview" src="<?= $img_overview ?>.png" border=0></a></td>
<td><a href="<?= $trend->url_history ?>"><img alt="trending" src="<?= $img_history ?>.png" border=0></a></td>
<td><a href="<?= $trend->url_newsarchive ?>"><img alt="archives" src="<?= $img_newsarchive ?>.png" border=0></a></td>
</tr>
<?php endforeach; ?>
</table>
<?php endif; ?>
<?php endforeach ?>
<?php endforeach ?>
</body>
</html>
<?php
include_once('plugins/pog/objects/class.trend.php');
include_once('plugins/pog/objects/class.trendhistory.php');
include_once('plugins/pog/objects/class.database.php');
include_once('plugins/pog/configuration.php');
// get the latest trends
$buffer = get_url("http://www.google.com/trends/hottrends/atom/hourly");
// if we got something, work it
if (!empty($buffer))
{
$pattern = '/class="(.+) (new|down|up)(\d*)"><a href="(.+)">(.+)<\/a>/';
preg_match_all($pattern, $buffer, $matches, PREG_SET_ORDER);
}
else die("no data found at $url!");
// discover all trends as of this moment
$trends = array();
for ($i=0; $i < count($matches); $i++) {
$t = new TrendObject();
$t->hotness = $matches[$i][1];
$t->direction = $matches[$i][2];
$t->magnitude = $matches[$i][3];
$t->url_overview = $matches[$i][4]; // http://www.google.com/trends/hottrends?q=mark+hurd&date=2010-8-6&sa=X
$t->keyword = $matches[$i][5];
$t->url_history();
$t->url_newsarchive();
$t->url_graph();
$trends[$i] = $t->archive();
}
function get_url($url)
{
$curl_handle = curl_init();
curl_setopt($curl_handle,CURLOPT_URL, $url);
curl_setopt($curl_handle,CURLOPT_CONNECTTIMEOUT, 2);
curl_setopt($curl_handle,CURLOPT_RETURNTRANSFER, 1);
$buffer = curl_exec($curl_handle);
curl_close($curl_handle);
return $buffer;
}
function extract_related_searches(&$subject)
{
// <b>Related searches:</b><br>rick fox <br><br>
}
function extract_peak(&$subject)
{
// <b>Peak:</b><br>4 hours ago<br><br>
}
class TrendObject
{
public $keyword; // [5][i++][0]
public $hotness; // [1][i++][0]
public $direction; // [2][i++][0]
public $magnitude; // [3][i++][0]
public $url_overview; // [4][i++][0] http://www.google.com/trends/hottrends?q=mark+hurd&date=2010-8-6&sa=X
public $url_graph;
public $url_history;
public $url_newsarchive;
function url_graph()
{
// http://www.google.com/trends/viz?hl=&q=mark+hurd&date=2010-8-6&graph=hot_img&sa=X
if (!is_null($this->keyword) && $this->keyword != '') {
$k = urlencode($this->keyword);
$this->url_graph = "http://www.google.com/trends/viz?hl=&q=$k&date=2010-8-6&graph=hot_img&sa=X";
} else {
$this->url_graph = "http://www.google.com/trends";
}
}
function url_history()
{
// http://www.google.com/trends?q=mark+hurd&ctab=0
if (!is_null($this->keyword) && $this->keyword != '') {
$k = urlencode($this->keyword);
$this->url_history = "http://www.google.com/trends?q=$k&ctab=0";
} else {
$this->url_graph = "http://www.google.com/trends";
}
}
function url_newsarchive()
{
// http://www.google.com/archivesearch?q=mark+hurd&as_ldate=1/2004&as_hdate=8/2010&nav=m&scoring=t
if (!is_null($this->keyword) && $this->keyword != '') {
$k = urlencode($this->keyword);
$this->url_newsarchive = "http://www.google.com/archivesearch?q=$k&as_ldate=1/2004&as_hdate=8/2010&nav=m&scoring=t";
} else {
$this->url_graph = "http://www.google.com/trends";
}
}
function archive()
{
$t = new Trend();
$t->hotness = $this->hotness;
$t->direction = $this->direction;
$t->magnitude = $this->magnitude;
$t->url_overview = $this->url_overview;
$t->keyword = $this->keyword;
$t->url_history = $this->url_history;
$t->url_newsarchive = $this->url_newsarchive;
$t->url_graph = $this->url_graph;
$existing = $t->GetList(array(array("keyword", "=", "$this->keyword")));
if (count($existing) != 0) {
$h = new TrendHistory();
$h->trendid = $t->trendid;
$h->keyword = $t->keyword;
$h->hotness = $t->hotness;
$h->direction = $t->direction;
$h->magnitude = $t->magnitude;
$h->Save();
} else {
// save new entry
$t->Save();
}
return $this;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment