summaryrefslogtreecommitdiff
path: root/app/Console/Commands/SyncVideos.php
blob: 4f9f80036745b391e72ced99fc3942e0245a6d44 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
<?php

namespace App\Console\Commands;

use App\Rugby\Model;
use App\Youtube\Service;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Storage;

class SyncVideos extends Command
{
    protected $signature = 'sync:videos';
    protected $description = 'Sync downloaded videos with existing matches';

    public function handle()
    {
        $videos = Model\Video::whereNull('match_id')->get();

        $this->info('Found ' . $videos->count() . ' videos!');
        foreach ($videos as $video) {
            $filename = $video->getFilename();

            $this->info('Searching for match from filename: ' . $filename);

            $tournaments = Model\Tournament::all()->filter(
                function ($object) use ($filename) {
                    return $object->isMatch($filename);
                }
            );

            if (!$tournaments->count()) {
                $this->info('Could not find tournament from filename: ' . $filename);
                return;
            }

            $this->info('Found Tournmament:' . $tournaments->first()->name);

            $tournament_ids = $tournaments->pluck('id');

            $teams = Model\Team::all()->filter(
                function ($object) use ($filename) {
                    return $object->isMatch($filename);
                }
            );

            if (!$teams->count()) {
                $this->info('Could not find teams from filename: ' . $filename);
                continue;
            }

            if ($teams->count() == 1) {
                $this->info('Could only find 1 team (' . $teams->first()->name . ') from filename: ' . $filename);
                continue;
            }

            $this->info('Found Teams:' . $teams->first()->name, $teams->slice(1, 1)->first()->name);

            $team_ids = $teams->pluck('name')->toArray();

            $matches = $tournaments->first()->matches;

            foreach ($matches as $match) {
                if (in_array($match->getHomeTeam()->name, $team_ids)) {
                    if (in_array($match->getAwayTeam()->name, $team_ids)) {

                        $this->info('Found Match for filename: ' . $filename);
                        $match->videos()->save($video);
                        Storage::disk('local')->move(
                            'youtube/video/' . $video->getFilename(),
                            'public/matches/' . $video->getFilename()
                        );

                        continue 2;
                    }
                }
            }

            $this->info('Failed to find Match for filename: ' . $filename);
        }

        return Command::SUCCESS;
    }

}