| Conditions | 34 |
| Paths | 684 |
| Total Lines | 182 |
| Code Lines | 116 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 51 | public function processSite($groupID, $guidChar, $process, bool $local = false): void |
||
| 52 | { |
||
| 53 | $res = $this->getTvReleases($groupID, $guidChar, $process, parent::PROCESS_TVMAZE); |
||
| 54 | |||
| 55 | $tvCount = \count($res); |
||
| 56 | |||
| 57 | if ($tvCount === 0) { |
||
| 58 | |||
| 59 | return; |
||
| 60 | } |
||
| 61 | |||
| 62 | if ($res instanceof \Traversable) { |
||
|
|
|||
| 63 | $this->titleCache = []; |
||
| 64 | $processed = 0; |
||
| 65 | $matched = 0; |
||
| 66 | $skipped = 0; |
||
| 67 | |||
| 68 | foreach ($res as $row) { |
||
| 69 | $processed++; |
||
| 70 | $siteId = false; |
||
| 71 | $this->posterUrl = ''; |
||
| 72 | |||
| 73 | // Clean the show name for better match probability |
||
| 74 | $release = $this->parseInfo($row['searchname']); |
||
| 75 | if (\is_array($release) && $release['name'] !== '') { |
||
| 76 | if (\in_array($release['cleanname'], $this->titleCache, false)) { |
||
| 77 | if ($this->echooutput) { |
||
| 78 | cli()->primaryOver(' → '); |
||
| 79 | cli()->alternateOver($this->truncateTitle($release['cleanname'])); |
||
| 80 | cli()->primaryOver(' → '); |
||
| 81 | cli()->alternate('Skipped (previously failed)'); |
||
| 82 | } |
||
| 83 | $this->setVideoNotFound(parent::PROCESS_TMDB, $row['id']); |
||
| 84 | $skipped++; |
||
| 85 | |||
| 86 | continue; |
||
| 87 | } |
||
| 88 | |||
| 89 | // Find the Video ID if it already exists by checking the title against stored TVMaze titles |
||
| 90 | $videoId = $this->getByTitle($release['cleanname'], parent::TYPE_TV, parent::SOURCE_TVMAZE); |
||
| 91 | |||
| 92 | // Force local lookup only |
||
| 93 | // $local = true, $lookupsetting = false and vice versa |
||
| 94 | $lookupSetting = $local !== true; |
||
| 95 | |||
| 96 | if ($videoId === 0 && $lookupSetting) { |
||
| 97 | // If lookups are allowed lets try to get it. |
||
| 98 | if ($this->echooutput) { |
||
| 99 | cli()->primaryOver(' → '); |
||
| 100 | cli()->headerOver($this->truncateTitle($release['cleanname'])); |
||
| 101 | cli()->primaryOver(' → '); |
||
| 102 | cli()->info('Searching TVMaze...'); |
||
| 103 | } |
||
| 104 | |||
| 105 | // Get the show from TVMaze |
||
| 106 | $tvMazeShow = $this->getShowInfo((string) $release['cleanname']); |
||
| 107 | |||
| 108 | $dupeCheck = false; |
||
| 109 | |||
| 110 | if (\is_array($tvMazeShow)) { |
||
| 111 | $siteId = (int) $tvMazeShow['tvmaze']; |
||
| 112 | // Check if we have the TVDB ID already, if we do use that Video ID, unless it is 0 |
||
| 113 | if ((int) $tvMazeShow['tvdb'] !== 0) { |
||
| 114 | $dupeCheck = $this->getVideoIDFromSiteID('tvdb', $tvMazeShow['tvdb']); |
||
| 115 | } |
||
| 116 | if ($dupeCheck === false) { |
||
| 117 | $videoId = $this->add($tvMazeShow); |
||
| 118 | } else { |
||
| 119 | $videoId = $dupeCheck; |
||
| 120 | // Update any missing fields and add site IDs |
||
| 121 | $this->update($videoId, $tvMazeShow); |
||
| 122 | } |
||
| 123 | } else { |
||
| 124 | $videoId = $dupeCheck; |
||
| 125 | } |
||
| 126 | } else { |
||
| 127 | if ($this->echooutput && $videoId > 0) { |
||
| 128 | cli()->primaryOver(' → '); |
||
| 129 | cli()->headerOver($this->truncateTitle($release['cleanname'])); |
||
| 130 | cli()->primaryOver(' → '); |
||
| 131 | cli()->info('Found in DB'); |
||
| 132 | } |
||
| 133 | $siteId = $this->getSiteIDFromVideoID('tvmaze', $videoId); |
||
| 134 | } |
||
| 135 | |||
| 136 | if (is_numeric($videoId) && $videoId > 0 && is_numeric($siteId) && $siteId > 0) { |
||
| 137 | // Now that we have valid video and tvmaze ids, try to get the poster |
||
| 138 | $this->getPoster($videoId); |
||
| 139 | |||
| 140 | $seriesNo = preg_replace('/^S0*/i', '', $release['season']); |
||
| 141 | $episodeNo = preg_replace('/^E0*/i', '', $release['episode']); |
||
| 142 | |||
| 143 | if ($episodeNo === 'all') { |
||
| 144 | // Set the video ID and leave episode 0 |
||
| 145 | $this->setVideoIdFound($videoId, $row['id'], 0); |
||
| 146 | if ($this->echooutput) { |
||
| 147 | cli()->primaryOver(' → '); |
||
| 148 | cli()->headerOver($this->truncateTitle($release['cleanname'])); |
||
| 149 | cli()->primaryOver(' → '); |
||
| 150 | cli()->primary('Full Season matched'); |
||
| 151 | } |
||
| 152 | $matched++; |
||
| 153 | |||
| 154 | continue; |
||
| 155 | } |
||
| 156 | |||
| 157 | // Download all episodes if new show to reduce API usage |
||
| 158 | if ($this->countEpsByVideoID($videoId) === false) { |
||
| 159 | $this->getEpisodeInfo($siteId, -1, -1, '', $videoId); |
||
| 160 | } |
||
| 161 | |||
| 162 | // Check if we have the episode for this video ID |
||
| 163 | $episode = $this->getBySeasonEp($videoId, $seriesNo, $episodeNo, $release['airdate']); |
||
| 164 | |||
| 165 | if ($episode === false) { |
||
| 166 | // Send the request for the episode to TVMaze |
||
| 167 | $tvMazeEpisode = $this->getEpisodeInfo( |
||
| 168 | $siteId, |
||
| 169 | (int) $seriesNo, |
||
| 170 | (int) $episodeNo, |
||
| 171 | $release['airdate'] |
||
| 172 | ); |
||
| 173 | |||
| 174 | if ($tvMazeEpisode) { |
||
| 175 | $episode = $this->addEpisode($videoId, $tvMazeEpisode); |
||
| 176 | } |
||
| 177 | } |
||
| 178 | |||
| 179 | if ($episode !== false && is_numeric($episode) && $episode > 0) { |
||
| 180 | // Mark the releases video and episode IDs |
||
| 181 | $this->setVideoIdFound($videoId, $row['id'], $episode); |
||
| 182 | if ($this->echooutput) { |
||
| 183 | cli()->primaryOver(' → '); |
||
| 184 | cli()->headerOver($this->truncateTitle($release['cleanname'])); |
||
| 185 | cli()->primaryOver(' S'); |
||
| 186 | cli()->warningOver(sprintf('%02d', $seriesNo)); |
||
| 187 | cli()->primaryOver('E'); |
||
| 188 | cli()->warningOver(sprintf('%02d', $episodeNo)); |
||
| 189 | cli()->primaryOver(' ✓ '); |
||
| 190 | cli()->primary('MATCHED (TVMaze)'); |
||
| 191 | } |
||
| 192 | $matched++; |
||
| 193 | } else { |
||
| 194 | // Processing failed, set the episode ID to the next processing group |
||
| 195 | $this->setVideoIdFound($videoId, $row['id'], 0); |
||
| 196 | $this->setVideoNotFound(parent::PROCESS_TMDB, $row['id']); |
||
| 197 | if ($this->echooutput) { |
||
| 198 | cli()->primaryOver(' → '); |
||
| 199 | cli()->alternateOver($this->truncateTitle($release['cleanname'])); |
||
| 200 | cli()->primaryOver(' → '); |
||
| 201 | cli()->warning('Episode not found'); |
||
| 202 | } |
||
| 203 | } |
||
| 204 | } else { |
||
| 205 | // Processing failed, set the episode ID to the next processing group |
||
| 206 | $this->setVideoNotFound(parent::PROCESS_TMDB, $row['id']); |
||
| 207 | $this->titleCache[] = $release['cleanname'] ?? null; |
||
| 208 | if ($this->echooutput) { |
||
| 209 | cli()->primaryOver(' → '); |
||
| 210 | cli()->alternateOver($this->truncateTitle($release['cleanname'])); |
||
| 211 | cli()->primaryOver(' → '); |
||
| 212 | cli()->warning('Not found'); |
||
| 213 | } |
||
| 214 | } |
||
| 215 | } else { |
||
| 216 | // Processing failed, set the episode ID to the next processing group |
||
| 217 | $this->setVideoNotFound(parent::PROCESS_TMDB, $row['id']); |
||
| 218 | $this->titleCache[] = $release['cleanname'] ?? null; |
||
| 219 | if ($this->echooutput) { |
||
| 220 | cli()->primaryOver(' → '); |
||
| 221 | cli()->alternateOver(mb_substr($row['searchname'], 0, 50)); |
||
| 222 | cli()->primaryOver(' → '); |
||
| 223 | cli()->error('Parse failed'); |
||
| 224 | } |
||
| 225 | } |
||
| 226 | } |
||
| 227 | |||
| 228 | // Display summary |
||
| 229 | if ($this->echooutput && $matched > 0) { |
||
| 230 | echo "\n"; |
||
| 231 | cli()->primaryOver(' ✓ TVMaze: '); |
||
| 232 | cli()->primary(sprintf('%d matched, %d skipped', $matched, $skipped)); |
||
| 233 | } |
||
| 545 |