|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Services\TvProcessing\Pipes; |
|
4
|
|
|
|
|
5
|
|
|
use App\Services\TvProcessing\TvProcessingPassable; |
|
6
|
|
|
use App\Services\TvProcessing\TvProcessingResult; |
|
7
|
|
|
use App\Services\TvProcessing\TvReleaseContext; |
|
8
|
|
|
use Blacklight\ColorCLI; |
|
9
|
|
|
use Closure; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Base class for TV processing pipe handlers. |
|
13
|
|
|
* |
|
14
|
|
|
* Each pipe is responsible for processing releases through a specific provider. |
|
15
|
|
|
*/ |
|
16
|
|
|
abstract class AbstractTvProviderPipe |
|
17
|
|
|
{ |
|
18
|
|
|
protected int $priority = 50; |
|
19
|
|
|
protected bool $echoOutput = true; |
|
20
|
|
|
protected ColorCLI $colorCli; |
|
21
|
|
|
protected array $titleCache = []; |
|
22
|
|
|
|
|
23
|
|
|
public function __construct() |
|
24
|
|
|
{ |
|
25
|
|
|
$this->colorCli = new ColorCLI(); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Handle the TV processing request. |
|
30
|
|
|
*/ |
|
31
|
|
|
public function handle(TvProcessingPassable $passable, Closure $next): TvProcessingPassable |
|
32
|
|
|
{ |
|
33
|
|
|
// If we already have a match, skip processing |
|
34
|
|
|
if ($passable->shouldStopProcessing()) { |
|
35
|
|
|
return $next($passable); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
// Skip if we don't have valid parsed info |
|
39
|
|
|
if (! $passable->hasValidParsedInfo()) { |
|
40
|
|
|
return $next($passable); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
// Skip if this provider shouldn't process this release |
|
44
|
|
|
if ($this->shouldSkip($passable)) { |
|
45
|
|
|
$passable->updateResult( |
|
46
|
|
|
TvProcessingResult::skipped('Provider skipped', $this->getName()), |
|
47
|
|
|
$this->getName() |
|
48
|
|
|
); |
|
49
|
|
|
return $next($passable); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
// Attempt to process with this provider |
|
53
|
|
|
$result = $this->process($passable); |
|
54
|
|
|
|
|
55
|
|
|
// Update the result |
|
56
|
|
|
$passable->updateResult($result, $this->getName()); |
|
57
|
|
|
|
|
58
|
|
|
return $next($passable); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Get the priority of this provider (lower = higher priority). |
|
63
|
|
|
*/ |
|
64
|
|
|
public function getPriority(): int |
|
65
|
|
|
{ |
|
66
|
|
|
return $this->priority; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Get the name of this provider for debugging/logging. |
|
71
|
|
|
*/ |
|
72
|
|
|
abstract public function getName(): string; |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Get the status code for releases awaiting this provider. |
|
76
|
|
|
*/ |
|
77
|
|
|
abstract public function getStatusCode(): int; |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Attempt to process the release through this provider. |
|
81
|
|
|
*/ |
|
82
|
|
|
abstract protected function process(TvProcessingPassable $passable): TvProcessingResult; |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Check if this provider should be skipped for the given passable. |
|
86
|
|
|
*/ |
|
87
|
|
|
protected function shouldSkip(TvProcessingPassable $passable): bool |
|
|
|
|
|
|
88
|
|
|
{ |
|
89
|
|
|
return false; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Set echo output flag. |
|
94
|
|
|
*/ |
|
95
|
|
|
public function setEchoOutput(bool $echo): self |
|
96
|
|
|
{ |
|
97
|
|
|
$this->echoOutput = $echo; |
|
98
|
|
|
return $this; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Check if a title is in the failure cache. |
|
103
|
|
|
*/ |
|
104
|
|
|
protected function isInTitleCache(string $title): bool |
|
105
|
|
|
{ |
|
106
|
|
|
return in_array($title, $this->titleCache, true); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* Add a title to the failure cache. |
|
111
|
|
|
*/ |
|
112
|
|
|
protected function addToTitleCache(string $title): void |
|
113
|
|
|
{ |
|
114
|
|
|
$this->titleCache[] = $title; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* Clear the title cache. |
|
119
|
|
|
*/ |
|
120
|
|
|
public function clearTitleCache(): void |
|
121
|
|
|
{ |
|
122
|
|
|
$this->titleCache = []; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* Truncate title for display purposes. |
|
127
|
|
|
*/ |
|
128
|
|
|
protected function truncateTitle(string $title, int $maxLength = 45): string |
|
129
|
|
|
{ |
|
130
|
|
|
if (mb_strlen($title) <= $maxLength) { |
|
131
|
|
|
return $title; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
return mb_substr($title, 0, $maxLength - 3) . '...'; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* Output match success message. |
|
139
|
|
|
*/ |
|
140
|
|
|
protected function outputMatch(string $title, ?int $season = null, ?int $episode = null, ?string $airdate = null): void |
|
141
|
|
|
{ |
|
142
|
|
|
if (! $this->echoOutput) { |
|
143
|
|
|
return; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
$this->colorCli->primaryOver(' → '); |
|
147
|
|
|
$this->colorCli->headerOver($this->truncateTitle($title)); |
|
148
|
|
|
|
|
149
|
|
|
if ($season !== null && $episode !== null) { |
|
150
|
|
|
$this->colorCli->primaryOver(' S'); |
|
151
|
|
|
$this->colorCli->warningOver(sprintf('%02d', $season)); |
|
152
|
|
|
$this->colorCli->primaryOver('E'); |
|
153
|
|
|
$this->colorCli->warningOver(sprintf('%02d', $episode)); |
|
154
|
|
|
} elseif ($airdate !== null) { |
|
155
|
|
|
$this->colorCli->primaryOver(' | '); |
|
156
|
|
|
$this->colorCli->warningOver($airdate); |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
$this->colorCli->primaryOver(' ✓ '); |
|
160
|
|
|
$this->colorCli->primary('MATCHED (' . $this->getName() . ')'); |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* Output not found message. |
|
165
|
|
|
*/ |
|
166
|
|
|
protected function outputNotFound(string $title): void |
|
167
|
|
|
{ |
|
168
|
|
|
if (! $this->echoOutput) { |
|
169
|
|
|
return; |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
$this->colorCli->primaryOver(' → '); |
|
173
|
|
|
$this->colorCli->alternateOver($this->truncateTitle($title)); |
|
174
|
|
|
$this->colorCli->primaryOver(' → '); |
|
175
|
|
|
$this->colorCli->warning('Not found'); |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
/** |
|
179
|
|
|
* Output searching message. |
|
180
|
|
|
*/ |
|
181
|
|
|
protected function outputSearching(string $title): void |
|
182
|
|
|
{ |
|
183
|
|
|
if (! $this->echoOutput) { |
|
184
|
|
|
return; |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
$this->colorCli->primaryOver(' → '); |
|
188
|
|
|
$this->colorCli->headerOver($this->truncateTitle($title)); |
|
189
|
|
|
$this->colorCli->primaryOver(' → '); |
|
190
|
|
|
$this->colorCli->info('Searching ' . $this->getName() . '...'); |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
/** |
|
194
|
|
|
* Output found in DB message. |
|
195
|
|
|
*/ |
|
196
|
|
|
protected function outputFoundInDb(string $title): void |
|
197
|
|
|
{ |
|
198
|
|
|
if (! $this->echoOutput) { |
|
199
|
|
|
return; |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
$this->colorCli->primaryOver(' → '); |
|
203
|
|
|
$this->colorCli->headerOver($this->truncateTitle($title)); |
|
204
|
|
|
$this->colorCli->primaryOver(' → '); |
|
205
|
|
|
$this->colorCli->info('Found in DB'); |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
|
|
/** |
|
209
|
|
|
* Output skipped message. |
|
210
|
|
|
*/ |
|
211
|
|
|
protected function outputSkipped(string $title, string $reason = 'previously failed'): void |
|
212
|
|
|
{ |
|
213
|
|
|
if (! $this->echoOutput) { |
|
214
|
|
|
return; |
|
215
|
|
|
} |
|
216
|
|
|
|
|
217
|
|
|
$this->colorCli->primaryOver(' → '); |
|
218
|
|
|
$this->colorCli->alternateOver($this->truncateTitle($title)); |
|
219
|
|
|
$this->colorCli->primaryOver(' → '); |
|
220
|
|
|
$this->colorCli->alternate('Skipped (' . $reason . ')'); |
|
221
|
|
|
} |
|
222
|
|
|
} |
|
223
|
|
|
|
|
224
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.