Browse Source

add incoming tweets to the queue

master
Aaron Parecki 7 years ago
parent
commit
21c776793a
No known key found for this signature in database GPG Key ID: 276C2817346D6056
5 changed files with 121 additions and 3 deletions
  1. +7
    -1
      app/Http/Controllers/HomeController.php
  2. +60
    -2
      app/Http/Controllers/TwitterController.php
  3. +19
    -0
      app/Tweet.php
  4. +32
    -0
      database/migrations/2017_07_02_192057_tweet_date.php
  5. +3
    -0
      resources/views/home.blade.php

+ 7
- 1
app/Http/Controllers/HomeController.php View File

@ -3,6 +3,8 @@
namespace App\Http\Controllers; namespace App\Http\Controllers;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Twitter;
use App\Tweet;
class HomeController extends Controller class HomeController extends Controller
{ {
@ -23,6 +25,10 @@ class HomeController extends Controller
*/ */
public function index() public function index()
{ {
return view('home');
$queue = Tweet::queued();
return view('home', [
'queue' => $queue
]);
} }
} }

+ 60
- 2
app/Http/Controllers/TwitterController.php View File

@ -4,14 +4,72 @@ namespace App\Http\Controllers;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Illuminate\Routing\Controller as BaseController; use Illuminate\Routing\Controller as BaseController;
use Log; use Log;
use App\Player, App\Team, App\Tweet, App\TransitCenter, App\TransitLine, App\Document;
class TwitterController extends BaseController class TwitterController extends BaseController
{ {
public function input(Request $request) public function input(Request $request)
{ {
Log::info(json_encode($request->all()));
return 'got it';
$data = $request->all();
// Find the user who tweeted this
$twitter_user_id = $data['user']['id_str'];
$player = Player::where('twitter_user_id', $twitter_user_id)->first();
if(isset($data['extended_tweet']['full_text']))
$text = $data['extended_tweet']['full_text'];
else
$text = $data['text'];
// Unshorten URLs
if(isset($data['entities']['urls'])) {
foreach($data['entities']['urls'] as $url) {
$text = str_replace($url['url'], $url['expanded_url'], $text);
}
}
// Remove media URLs from tweet text
$photos = [];
if(isset($data['extended_entities']['media'])) {
foreach($data['extended_entities']['media'] as $media) {
$text = str_replace($media['url'], '', $text);
$photos[] = $media['media_url_https'];
}
$text = trim($text);
}
// Find the mission hashtag
if(strpos($text, '#transitspy') !== false)
$mission = 1;
elseif(strpos($text, '#intercept') !== false)
$mission = 2;
elseif(strpos($text, '#airlair') !== false)
$mission = 3;
elseif(strpos($text, '#transittea') !== false)
$mission = 4;
elseif(strpos($text, '#sing') !== false)
$mission = 5;
elseif(strpos($text, '#passport') !== false)
$mission = 6;
elseif(strpos($text, '#document') !== false)
$mission = 7;
else
$mission = 0;
$tweet = new Tweet;
$tweet->player_id = $player ? $player->id : 0;
$tweet->team_id = $player ? $player->team->id : 0;
$tweet->text = $text;
$tweet->photo = json_encode($photos);
$tweet->mission = $mission;
$tweet->tweet_date = date('Y-m-d H:i:s', strtotime($data['created_at']));
$tweet->save();
// TODO: Broadcast this to the web interface
return $data['id_str'];
} }

+ 19
- 0
app/Tweet.php View File

@ -0,0 +1,19 @@
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Tweet extends Model
{
protected $fillable = [
'player_id', 'team_id', 'text', 'photo', 'claimed_at', 'processed', 'mission', 'tweet_date',
'm1_transit_line_id', 'm1_non_trimet',
'm2_transit_center_id', 'm2_with_other_team',
'm3_complete', 'm4_complete', 'm5_complete', 'm5_tip',
'm6_complete', 'm7_document_id'
];
public static function queued() {
return Tweet::whereNull('claimed_at')->where('processed', 0)->where('mission', '>', 0)->get();
}
}

+ 32
- 0
database/migrations/2017_07_02_192057_tweet_date.php View File

@ -0,0 +1,32 @@
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class TweetDate extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('tweets', function (Blueprint $table) {
$table->datetime('tweet_date');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('tweets', function (Blueprint $table) {
$table->dropColumn('tweet_date');
});
}
}

+ 3
- 0
resources/views/home.blade.php View File

@ -9,6 +9,9 @@
<div class="panel-body"> <div class="panel-body">
You are logged in! You are logged in!
</div> </div>
</div> </div>
</div> </div>

Loading…
Cancel
Save