Skip to content

Commit

Permalink
Interim check in for twitter
Browse files Browse the repository at this point in the history
  • Loading branch information
newscloud committed Mar 9, 2015
1 parent d4b347c commit cbb0b30
Show file tree
Hide file tree
Showing 14 changed files with 704 additions and 2 deletions.
23 changes: 23 additions & 0 deletions controllers/MomentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use app\models\Moment;
use app\models\MomentSearch;
use app\models\Gram;
use app\models\Twitter;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
Expand Down Expand Up @@ -67,6 +68,20 @@ public function actionBrowse($id)
'moment_id'=>$id,
]);
}

public function actionBrowse_twitter($id)
{
// browse twitter results for moment $id
$dataProvider = new ActiveDataProvider([
'query' => Twitter::find()->where(['moment_id'=>$id])->orderBy('tweeted_at ASC')
]);

return $this->render('browse_twitter', [
'dataProvider' => $dataProvider,
'moment_id'=>$id,
]);
}

public function actionPurge($id)
{
Moment::purge($id);
Expand All @@ -80,6 +95,14 @@ public function actionInstagram($id)
$model->searchInstagram();
return $this->redirect(['browse', 'id' => $model->id]);
}

public function actionTwitter($id)
{
$model = $this->findModel($id);
$model->searchTwitter();
return $this->redirect(['browse_twitter', 'id' => $model->id]);
}

/**
* Creates a new Moment model.
* If creation is successful, the browser will be redirected to the 'view' page.
Expand Down
121 changes: 121 additions & 0 deletions controllers/TwitterController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
<?php

namespace app\controllers;

use Yii;
use app\models\Twitter;
use app\models\TwitterSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;

/**
* TwitterController implements the CRUD actions for Twitter model.
*/
class TwitterController extends Controller
{
public function behaviors()
{
return [
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'delete' => ['post'],
],
],
];
}

/**
* Lists all Twitter models.
* @return mixed
*/
public function actionIndex()
{
$searchModel = new TwitterSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);

return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}

/**
* Displays a single Twitter model.
* @param integer $id
* @return mixed
*/
public function actionView($id)
{
return $this->render('view', [
'model' => $this->findModel($id),
]);
}

/**
* Creates a new Twitter model.
* If creation is successful, the browser will be redirected to the 'view' page.
* @return mixed
*/
public function actionCreate()
{
$model = new Twitter();

if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->id]);
} else {
return $this->render('create', [
'model' => $model,
]);
}
}

/**
* Updates an existing Twitter model.
* If update is successful, the browser will be redirected to the 'view' page.
* @param integer $id
* @return mixed
*/
public function actionUpdate($id)
{
$model = $this->findModel($id);

if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->id]);
} else {
return $this->render('update', [
'model' => $model,
]);
}
}

/**
* Deletes an existing Twitter model.
* If deletion is successful, the browser will be redirected to the 'index' page.
* @param integer $id
* @return mixed
*/
public function actionDelete($id)
{
$this->findModel($id)->delete();

return $this->redirect(['index']);
}

/**
* Finds the Twitter model based on its primary key value.
* If the model is not found, a 404 HTTP exception will be thrown.
* @param integer $id
* @return Twitter the loaded model
* @throws NotFoundHttpException if the model cannot be found
*/
protected function findModel($id)
{
if (($model = Twitter::findOne($id)) !== null) {
return $model;
} else {
throw new NotFoundHttpException('The requested page does not exist.');
}
}
}
35 changes: 35 additions & 0 deletions migrations/m150309_174014_create_twitter_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

use yii\db\Schema;
use yii\db\Migration;

class m150309_174014_create_twitter_table extends Migration
{
public function up()
{
$tableOptions = null;
if ($this->db->driverName === 'mysql') {
$tableOptions = 'CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE=InnoDB';
}

$this->createTable('{{%twitter}}', [
'id' => Schema::TYPE_PK,
'moment_id' => Schema::TYPE_INTEGER . ' NOT NULL',
'tweet_id' => Schema::TYPE_BIGINT . ' NOT NULL',
'twitter_id' => Schema::TYPE_BIGINT . ' NOT NULL',
'screen_name' => Schema::TYPE_STRING . ' NOT NULL DEFAULT 0',
'text' => Schema::TYPE_TEXT . ' NOT NULL ',
'tweeted_at' => Schema::TYPE_INTEGER . ' NOT NULL',
'created_at' => Schema::TYPE_INTEGER . ' NOT NULL',
'updated_at' => Schema::TYPE_INTEGER . ' NOT NULL',
], $tableOptions);
$this->addForeignKey('fk_twitter_moment', '{{%twitter}}', 'moment_id', '{{%moment}}', 'id', 'CASCADE', 'CASCADE');
}


public function down()
{
$this->dropForeignKey('fk_twitter_moment','{{%twitter}}');
$this->dropTable('{{%twitter}}');
}
}
83 changes: 82 additions & 1 deletion models/Moment.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use yii\db\ActiveRecord;
use app\models\Gram;
use Instagram;
use TwitterAPIExchange;

/**
* This is the model class for table "Moment".
Expand Down Expand Up @@ -58,6 +59,11 @@ public function getGrams()
{
return $this->hasMany(Gram::className(), ['moment_id' => 'id']);
}

public function getTwitters()
{
return $this->hasMany(Twitter::className(), ['moment_id' => 'id']);
}

/**
* @inheritdoc
Expand Down Expand Up @@ -126,9 +132,84 @@ public function searchInstagram() {
$i = new Gram(); $i->add($this->id,$m->user->username,$m->link,$m->created_time,$m->images->thumbnail->url,$caption);
}
}


public function searchTwitter() {
// Load your Twitter application keys
$settings = array(
'oauth_access_token' => \Yii::$app->params['twitter']['oauth_token'],
'oauth_access_token_secret' => \Yii::$app->params['twitter']['oauth_secret'],
'consumer_key' => \Yii::$app->params['twitter']['key'],
'consumer_secret' => \Yii::$app->params['twitter']['secret'],
);
// Connect to Twitter
$twitter = new TwitterAPIExchange($settings);
// Query settings for search
$url = 'https://api.twitter.com/1.1/search/tweets.json';
$requestMethod = 'GET';
// rate limit of 180 queries
$limit = 180;
$query_count=1;
$count = 100;
$result_type = 'recent';
// calculate valid timestamp range
$valid_start = $this->start_at;
// $until_date and $valid_end = // start time + duration
$valid_end = $this->start_at + ($this->duration*60);
$until_date = date('Y-m-d',$valid_end);
$distance_km = $this->distance/1000; // distance in km
// Unused: &since=$since_date
// $since_date = '2015-03-05';
// Perform first query with until_date
$getfield ="?result_type=$result_type&geocode=".$this->latitude.",".$this->longitude.",".$distance_km."mi&include_entities=false&until=$until_date&count=$count";
$tweets = json_decode($twitter->setGetfield($getfield)
->buildOauth($url, $requestMethod)
->performRequest());
var_dump($tweets);
die();
$max_id = 0;
echo 'Count Statuses: '.count($tweets->statuses).'<br>';
echo 'Max Tweet Id: '.$max_id.'<br>';
foreach ($tweets->statuses as $t) {
// check if tweet in valid time range
if ($t->created_at >= $valid_start && $t->created_at <= $valid_end)
{
// print_r($t);echo "<br><br>";
$i = new Twitter(); $i->add($this->id,$t->id_str,$t->user->id_str,$t->user->screen_name,$t->created_at,(isset($t->text)?$t->text:''));
}
if ($max_id ==0) {
$max_id = intval($t->id_str);
} else {
$max_id = min($max_id, intval($t->id_str));
}
}
// Perform all subsequent queries with addition of updated maximum_tweet_id
while ($query_count<=$limit) {
$query_count+=1;
echo 'Request #: '.$query_count.'<br>';
// Perform subsequent query with max_id
$getfield ="?result_type=$result_type&geocode=".$this->latitude.",".$this->longitude.",".$distance_km."mi&include_entities=false&max_id=$max_id&count=$count";
$tweets = json_decode($twitter->setGetfield($getfield)
->buildOauth($url, $requestMethod)
->performRequest());
echo 'Count Statuses: '.count($tweets->statuses).'<br>';
echo 'Max Tweet Id: '.$max_id.'<br>';
foreach ($tweets->statuses as $t) {
// check if tweet in valid time range
if ($t->created_at >= $valid_start && $t->created_at <= $valid_end)
{
$i = new Twitter(); $i->add($this->id,$t->id_str,$t->user->id_str,$t->user->screen_name,$t->created_at,(isset($t->text)?$t->text:''));
} else if ($t->created_at < $valid_start) {
// stop querying when earlier than valid_start
break;
}
$max_id = min($max_id,intval($t->id_str))-1;
}
} // end while
}

public static function purge($moment_id) {
Gram::deleteAll('moment_id='.$moment_id);
Twitter::deleteAll('moment_id='.$moment_id);
}

}
Loading

0 comments on commit cbb0b30

Please sign in to comment.