1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
use Goutte\Client; |
4
|
|
|
use Illuminate\Database\Eloquent\Collection as EloquentCollection; |
5
|
|
|
use Illuminate\Queue\Jobs\Job; |
6
|
|
|
use Illuminate\Support\Collection; |
7
|
|
|
use Symfony\Component\DomCrawler\Crawler; |
8
|
|
|
|
9
|
|
|
class MovieScraperWorker { |
|
|
|
|
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* HTTP client for requests |
13
|
|
|
* |
14
|
|
|
* @var Client |
15
|
|
|
*/ |
16
|
|
|
protected $client; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* URL to scrape |
20
|
|
|
* |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
protected $url = 'http://pro.boxoffice.com/statistics/release-calendar?display_all=yes'; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @param Client $client |
27
|
|
|
*/ |
28
|
|
|
function __construct(Client $client) { |
|
|
|
|
29
|
|
|
$this->client = $client; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Scrape for new movie data |
34
|
|
|
* |
35
|
|
|
* @param Job $job |
36
|
|
|
* @param $data |
37
|
|
|
*/ |
38
|
|
|
public function fire(Job $job, $data) { |
|
|
|
|
39
|
|
|
$movies = $this->getMovies(); |
40
|
|
|
|
41
|
|
|
$this->scanDatabaseForChanges($movies); |
|
|
|
|
42
|
|
|
|
43
|
|
|
Log::info('Releases researched', [count($movies)]); |
44
|
|
|
|
45
|
|
|
$job->delete(); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Get the upcoming movies |
50
|
|
|
* |
51
|
|
|
* @return Collection|array[] |
52
|
|
|
*/ |
53
|
|
|
private function getMovies() { |
54
|
|
|
// Limit the scraped movie release date to the max length of the league |
55
|
|
|
$limit = Carbon::now()->addWeeks(Config::get('draft.maximum_weeks')); |
56
|
|
|
|
57
|
|
|
$crawler = $this->client->request('GET', $this->url, []); |
58
|
|
|
// Generate a list of movies |
59
|
|
|
$movies = $crawler->filter('.data_table tbody tr')->each(function (Crawler $node) use ($limit) { |
60
|
|
|
|
61
|
|
|
$cols = $node->children(); |
62
|
|
|
|
63
|
|
|
$release_date = Carbon::createFromFormat('M j, Y|', trim($cols->eq(0)->text())); |
64
|
|
|
if ($release_date->gt($limit)) { |
65
|
|
|
return false; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
try { |
69
|
|
|
$info = [ |
70
|
|
|
'release' => $release_date, |
71
|
|
|
'name' => $cols->eq(1)->children()->eq(0)->text(), |
72
|
|
|
'boxoffice_id' => str_replace('/statistics/movies/', '', $cols->eq(1)->children()->eq(0) |
73
|
|
|
->attr('href')) |
74
|
|
|
]; |
75
|
|
|
|
76
|
|
|
} catch (Exception $e) { |
77
|
|
|
return false; |
78
|
|
|
} |
79
|
|
|
// If anything is missing, return false |
80
|
|
|
$valid = array_reduce($info, function ($state, $item) { |
81
|
|
|
return $state && $item; |
82
|
|
|
}, true); |
83
|
|
|
|
84
|
|
|
return $valid ? $info : false; |
85
|
|
|
}); |
86
|
|
|
|
87
|
|
|
$movies = new Collection(array_filter($movies)); |
88
|
|
|
|
89
|
|
|
return $movies; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Scan database for new movies and add them to the database |
94
|
|
|
* |
95
|
|
|
* @param Collection $movies |
|
|
|
|
96
|
|
|
*/ |
97
|
|
|
private function scanDatabaseForChanges(Collection $new_data) { |
|
|
|
|
98
|
|
|
$new_data = $new_data->keyBy('boxoffice_id'); |
99
|
|
|
|
100
|
|
|
$movies = Movie::whereIn('boxoffice_id', $new_data->keys())->get(); |
101
|
|
|
|
102
|
|
|
$movies_by_id = $movies->keyBy('boxoffice_id'); |
103
|
|
|
$new_movies = new Collection(array_diff_key($new_data->toArray(), $movies_by_id->toArray())); |
104
|
|
|
|
105
|
|
|
$this->updateMovies($new_data, $movies_by_id); |
106
|
|
|
$this->addMovies($new_movies); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Update current movies with new data |
111
|
|
|
* |
112
|
|
|
* @param Collection $new_data |
113
|
|
|
* @param EloquentCollection $current_data |
114
|
|
|
*/ |
115
|
|
|
private function updateMovies(Collection $new_data, EloquentCollection $current_data) { |
|
|
|
|
116
|
|
|
$current_data->map(function(Movie $movie) use($new_data) { |
117
|
|
|
$data = $new_data->get($movie->boxoffice_id); |
118
|
|
|
|
119
|
|
|
$movie->name = $data['name']; |
120
|
|
|
$movie->release = $data['release']; |
121
|
|
|
|
122
|
|
|
if($movie->isDirty()) { |
123
|
|
|
$movie->save(); |
124
|
|
|
} |
125
|
|
|
}); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Add new movies |
130
|
|
|
* |
131
|
|
|
* @param Collection $new_movies |
132
|
|
|
*/ |
133
|
|
|
private function addMovies(Collection $new_movies) { |
|
|
|
|
134
|
|
|
$new_movies->map(function($data) { |
135
|
|
|
$movie = Movie::create($data); |
|
|
|
|
136
|
|
|
}); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
|
140
|
|
|
} |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.