summaryrefslogtreecommitdiff
path: root/app
diff options
context:
space:
mode:
Diffstat (limited to 'app')
-rw-r--r--app/Console/Commands/ScrapeFile.php59
-rw-r--r--app/Console/Commands/ScrapeFiles.php64
-rw-r--r--app/Console/Commands/ScrapeUrl.php61
-rw-r--r--app/Http/Controllers/Controller.php13
-rw-r--r--app/Rugby/Factory/DataAdapter.php8
-rw-r--r--app/Rugby/Factory/Service.php71
-rw-r--r--app/Rugby/Factory/SixnationsrugbyAdapter.php100
-rw-r--r--app/Rugby/Model/Match.php59
-rw-r--r--app/Rugby/Model/Team.php34
-rw-r--r--app/Rugby/Model/Tournament.php19
-rw-r--r--app/Rugby/Model/Venue.php17
11 files changed, 504 insertions, 1 deletions
diff --git a/app/Console/Commands/ScrapeFile.php b/app/Console/Commands/ScrapeFile.php
new file mode 100644
index 0000000..4cfef90
--- /dev/null
+++ b/app/Console/Commands/ScrapeFile.php
@@ -0,0 +1,59 @@
+<?php
+
+namespace App\Console\Commands;
+
+use App\Rugby\Factory\Service;
+use App\Rugby\Factory\SixnationsrugbyAdapter;
+use Illuminate\Console\Command;
+use Illuminate\Support\Facades\Storage;
+
+class ScrapeFile extends Command
+{
+ /**
+ * The name and signature of the console command.
+ *
+ * @var string
+ */
+ protected $signature = 'scrape:file { filename }';
+
+ /**
+ * The console command description.
+ *
+ * @var string
+ */
+ protected $description = 'Scrape a file for data';
+
+ /**
+ * Create a new command instance.
+ *
+ * @return void
+ */
+ public function __construct()
+ {
+ parent::__construct();
+ }
+
+ /**
+ * Execute the console command.
+ *
+ * @return mixed
+ */
+ public function handle()
+ {
+ $filename = $this->argument('filename');
+
+ if (!Storage::disk('local')->exists($filename)) {
+ $this->error('Could not load file at: ' . Storage::disk('local')->path($filename));
+ return Command::FAILURE;
+ }
+
+ $raw_data = Storage::disk('local')->get($filename);
+
+ $service = new Service(new SixnationsrugbyAdapter($raw_data, 'Six Nations'));
+
+ $service->save();
+
+ return Command::SUCCESS;
+ }
+
+}
diff --git a/app/Console/Commands/ScrapeFiles.php b/app/Console/Commands/ScrapeFiles.php
new file mode 100644
index 0000000..05b7176
--- /dev/null
+++ b/app/Console/Commands/ScrapeFiles.php
@@ -0,0 +1,64 @@
+<?php
+
+namespace App\Console\Commands;
+
+use App\Rugby\Factory\Service;
+use App\Rugby\Factory\SixnationsrugbyAdapter;
+use Illuminate\Console\Command;
+use Illuminate\Support\Facades\Storage;
+
+class ScrapeFiles extends Command
+{
+ /**
+ * The name and signature of the console command.
+ *
+ * @var string
+ */
+ protected $signature = 'scrape:files { directory }';
+
+ /**
+ * The console command description.
+ *
+ * @var string
+ */
+ protected $description = 'Scrape a file for data';
+
+ /**
+ * Create a new command instance.
+ *
+ * @return void
+ */
+ public function __construct()
+ {
+ parent::__construct();
+ }
+
+ /**
+ * Execute the console command.
+ *
+ * @return mixed
+ */
+ public function handle()
+ {
+ $directory = $this->argument('directory');
+
+ if (!Storage::disk('local')->exists($directory)) {
+ $this->error('Could not load file at: ' . Storage::disk('local')->path($directory));
+ return Command::FAILURE;
+ }
+
+ $files = Storage::disk('local')->files($directory);
+
+
+ foreach ($files as $file) {
+ $tournament = 'Six Nations ' . explode('-', explode('.txt', $file)[0])[1];
+ $raw_data = Storage::disk('local')->get($file);
+
+ $service = new Service(new SixnationsrugbyAdapter($raw_data, $tournament));
+ $service->save();
+ }
+
+ return Command::SUCCESS;
+ }
+
+}
diff --git a/app/Console/Commands/ScrapeUrl.php b/app/Console/Commands/ScrapeUrl.php
new file mode 100644
index 0000000..c02080e
--- /dev/null
+++ b/app/Console/Commands/ScrapeUrl.php
@@ -0,0 +1,61 @@
+<?php
+
+namespace App\Console\Commands;
+
+use Illuminate\Console\Command;
+use Goutte\Client;
+
+class ScrapeUrl extends Command
+{
+ /**
+ * The name and signature of the console command.
+ *
+ * @var string
+ */
+ protected $signature = 'scrape:url { url }';
+
+ /**
+ * The console command description.
+ *
+ * @var string
+ */
+ protected $description = 'Scrape a webpage for data';
+
+ protected $client;
+
+ /**
+ * Create a new command instance.
+ *
+ * @return void
+ */
+ public function __construct()
+ {
+ parent::__construct();
+ $this->client = new Client();
+ }
+
+ /**
+ * Execute the console command.
+ *
+ * @return mixed
+ */
+ public function handle()
+ {
+ $url = $this->argument('url');
+
+ if ($url != 'https://www.sixnationsrugby.com/fixtures/') {
+ $this->error('Url not supported');
+ return;
+ }
+
+ $crawler = $this->client->request('GET', $this->argument('url'));
+
+ $crawler->filter('div.fixtures__top-tier')->each(
+ function ($node) {
+ print $node->text()."\n";
+ }
+ );
+
+
+ }
+}
diff --git a/app/Http/Controllers/Controller.php b/app/Http/Controllers/Controller.php
index a0a2a8a..cd4ac15 100644
--- a/app/Http/Controllers/Controller.php
+++ b/app/Http/Controllers/Controller.php
@@ -5,9 +5,20 @@ namespace App\Http\Controllers;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Foundation\Validation\ValidatesRequests;
-use Illuminate\Routing\Controller as BaseController;
+use Illuminate\Routing\Controller as BaseController;//
+use App\Rugby\Model;
class Controller extends BaseController
{
use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
+
+ public function index()
+ {
+ return view(
+ 'index',
+ [
+ 'tournaments' => Model\Tournament::orderBy('name', 'desc')->get()
+ ]
+ );
+ }
}
diff --git a/app/Rugby/Factory/DataAdapter.php b/app/Rugby/Factory/DataAdapter.php
new file mode 100644
index 0000000..b4971ce
--- /dev/null
+++ b/app/Rugby/Factory/DataAdapter.php
@@ -0,0 +1,8 @@
+<?php
+
+namespace App\Rugby\Factory;
+
+interface DataAdapter
+{
+
+}
diff --git a/app/Rugby/Factory/Service.php b/app/Rugby/Factory/Service.php
new file mode 100644
index 0000000..fa8028f
--- /dev/null
+++ b/app/Rugby/Factory/Service.php
@@ -0,0 +1,71 @@
+<?php
+
+namespace App\Rugby\Factory;
+
+use App\Rugby\Model;
+use App\Rugby\Factory\DataAdapter;
+
+class Service
+{
+ protected $data_adapter;
+
+ public function __construct(DataAdapter $data_adapter)
+ {
+ $this->data_adapter = $data_adapter;
+ }
+
+ public function save()
+ {
+ $tournament = Model\Tournament::where(['name' => $this->data_adapter->getTournamentName()])->first();
+
+ if (!$tournament) {
+ $tournament = Model\Tournament::create(['name' => $this->data_adapter->getTournamentName()]);
+ }
+
+ foreach ($this->data_adapter->getData() as $match_data) {
+ $this->processMatch($match_data, $tournament);
+ }
+ }
+
+ protected function processMatch(array $data, Model\Tournament $tournament)
+ {
+ $venue = Model\Venue::where('name', '=', $data['venue_name'])->first();
+
+ if (!$venue) {
+ $venue = Model\Venue::create(
+ [
+ 'city' => $data['venue_city'],
+ 'name' => $data['venue_name']
+ ]
+ );
+ }
+
+ $home_team = Model\Team::where('name', '=', $data['team_home'])->first();
+
+ if (!$home_team) {
+ $home_team = Model\Team::Create(['name' => $data['team_home']]);
+ }
+
+ $away_team = Model\Team::where('name', '=', $data['team_away'])->first();
+
+ if (!$away_team) {
+ $away_team = Model\Team::Create(['name' => $data['team_away']]);
+ }
+
+ $match = Model\Match::create(
+ [
+ 'date' => (new \Carbon\Carbon($data['match_date']))->format('Y-m-d H:i:s'),
+ 'score' => $data['match_score'],
+ 'half_score' => $data['match_half_score'],
+ 'referee' => $data['match_referee'] ?? null,
+ ]
+ );
+
+ $match->venue_id = $venue->id;
+ $match->save();
+
+ $match->tournaments()->save($tournament);
+ $match->teams()->attach($home_team, ['is_home' => '1']);
+ $match->teams()->attach($away_team, ['is_home' => '0']);
+ }
+}
diff --git a/app/Rugby/Factory/SixnationsrugbyAdapter.php b/app/Rugby/Factory/SixnationsrugbyAdapter.php
new file mode 100644
index 0000000..6b6edd7
--- /dev/null
+++ b/app/Rugby/Factory/SixnationsrugbyAdapter.php
@@ -0,0 +1,100 @@
+<?php
+
+namespace App\Rugby\Factory;
+
+use App\Rugby\Factory\DataAdapter;
+use Illuminate\Support\Facades\Storage;
+
+class SixnationsrugbyAdapter implements DataAdapter
+{
+ protected $data;
+ protected $filepath;
+ protected $tournament_name;
+
+ public function __construct(string $data, string $tournament_name)
+ {
+ $this->tournament_name = $tournament_name;
+ $this->data = $this->processData($this->cleanData($data));
+ }
+
+ public function getData()
+ {
+ return $this->data;
+ }
+
+
+ public function getTournamentName()
+ {
+ return $this->tournament_name;
+ }
+
+
+ protected function cleanData($data)
+ {
+ // string
+ $data = str_replace("Round 2", "", $data);
+ $data = str_replace("Round 3", "", $data);
+ $data = str_replace("Round 4", "", $data);
+ $data = str_replace("Round 5", "", $data);
+ $data = str_replace("BBC logo", "", $data);
+ $data = str_replace("ITV logo", "", $data);
+ $data = str_replace("S4C logo", "", $data);
+ $data = str_replace("Preview", "\na\nb\nc", $data);
+ $data = str_replace("Match Centre", "\na\nb\nc", $data);
+
+ // array
+ $data = explode('Round 1', $data)[1];
+ $data = explode('Store', $data)[0];
+ $data = ltrim(str_replace("\n\n", "\n", $data), "\n");
+ $data = explode("\n", $data);
+
+ return $data;
+ }
+
+ protected function processData($data)
+ {
+ $count = count($data) - 1;
+
+ $processed_data = [];
+
+ for ($i = 0; $i < $count; $i+= 10) {
+ $raw_match_data = array_slice($data, $i, 10);
+
+ $match_data = [
+ 'match_date' => $raw_match_data[0],
+ 'team_home' => $raw_match_data[1],
+ 'team_away' => $raw_match_data[4]
+ ];
+
+ $score = explode(' HT: ', $raw_match_data[3]);
+
+ $match_data['match_score'] = ($score[0] ?? null);
+ $match_data['match_half_score'] = ($score[1] ?? null);
+ $match_info = explode(' Ref: ', $raw_match_data[6]);
+
+ if (isset($match_info[0])) {
+ $venue_info = explode(', ', $match_info[0]);
+
+ $match_data['venue_name'] = ($venue_info[0] ?? null);
+ $match_data['venue_city'] = ($venue_info[1] ?? null);
+ }
+
+ $match_data['match_referee'] = ($match_info[1] ?? null);
+
+ $processed_data[] = $match_data;
+ }
+
+ return $processed_data;
+ }
+
+ public function getRef($raw)
+ {
+ $ref = explode(' Ref:', $raw[6]);
+
+ if (isset($ref[1])) {
+ return $ref[1];
+ }
+
+ return null;
+ }
+}
diff --git a/app/Rugby/Model/Match.php b/app/Rugby/Model/Match.php
new file mode 100644
index 0000000..5447ef5
--- /dev/null
+++ b/app/Rugby/Model/Match.php
@@ -0,0 +1,59 @@
+<?php
+
+namespace App\Rugby\Model;
+
+use App\Rugby\Model\Team;
+use App\Rugby\Model\Tournament;
+use App\Rugby\Model\Venue;
+
+use Illuminate\Database\Eloquent\Model;
+
+class Match extends Model
+{
+ protected $table = 'matches';
+
+ protected $casts = ['date' => 'datetime:Y-m-d'];
+
+ protected $fillable = ['score', 'half_score', 'referee', 'date'];
+
+ public function teams()
+ {
+ return $this->belongsToMany(Team::class, 'match_team');
+ }
+
+ public function venue()
+ {
+ return $this->hasOne(Venue::class, 'id');
+ }
+
+ public function getDisplayName()
+ {
+ $venue = Venue::find($this->venue_id);
+
+ if (!$venue) {
+ return 'Unknown';
+ }
+
+ return $venue->name;
+ }
+
+ public function getDisplayDate()
+ {
+ return (new \Carbon\Carbon($this->date))->format('M d Y');
+ }
+
+ public function homeTeam()
+ {
+ return $this->teams()->wherePivot('is_home', '=', true);
+ }
+
+ public function awayTeam()
+ {
+ return $this->teams()->wherePivot('is_home', '=', false);
+ }
+
+ public function tournaments()
+ {
+ return $this->belongsToMany(Tournament::class, 'match_tournament');
+ }
+}
diff --git a/app/Rugby/Model/Team.php b/app/Rugby/Model/Team.php
new file mode 100644
index 0000000..2582f33
--- /dev/null
+++ b/app/Rugby/Model/Team.php
@@ -0,0 +1,34 @@
+<?php
+
+namespace App\Rugby\Model;
+
+use Illuminate\Database\Eloquent\Model;
+
+use App\Rugby\Model\Match;
+
+class Team extends Model
+{
+ protected $table = 'teams';
+
+ protected $fillable = ['name'];
+
+ public function matches()
+ {
+ return $this->belongsToMany(Match::class)->withPivot('is_home');
+ }
+
+ public function homeMatches()
+ {
+ return $this->matches()->wherePivot('is_home', '=', true);
+ }
+
+ public function awayMatches()
+ {
+ return $this->matches()->wherePivot('is_home', '=', false);
+ }
+
+ public function getName()
+ {
+ return $this->name ?: '';
+ }
+}
diff --git a/app/Rugby/Model/Tournament.php b/app/Rugby/Model/Tournament.php
new file mode 100644
index 0000000..64d9a45
--- /dev/null
+++ b/app/Rugby/Model/Tournament.php
@@ -0,0 +1,19 @@
+<?php
+
+namespace App\Rugby\Model;
+
+use App\Rugby\Model\Match;
+
+use Illuminate\Database\Eloquent\Model;
+
+class Tournament extends Model
+{
+ protected $table = 'tournaments';
+
+ protected $fillable = ['name'];
+
+ public function matches()
+ {
+ return $this->belongsToMany(Match::class);
+ }
+}
diff --git a/app/Rugby/Model/Venue.php b/app/Rugby/Model/Venue.php
new file mode 100644
index 0000000..be85c23
--- /dev/null
+++ b/app/Rugby/Model/Venue.php
@@ -0,0 +1,17 @@
+<?php
+
+namespace App\Rugby\Model;
+
+use App\Rugby\Model\Match;
+
+use Illuminate\Database\Eloquent\Model;
+
+class Venue extends Model
+{
+ protected $fillable = ['name', 'city'];
+
+ public function matches()
+ {
+ return $this->belongsToMany(Match::class);
+ }
+}