2016-12-17 16:59:59 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Services;
|
|
|
|
|
2017-01-06 09:40:27 +01:00
|
|
|
use App\AlternativeTitle;
|
2017-01-25 14:44:51 +01:00
|
|
|
use App\Services\Models\EpisodeService;
|
2017-01-23 22:26:04 +01:00
|
|
|
use App\Services\Models\ItemService;
|
2017-01-06 13:00:18 +01:00
|
|
|
use App\Setting;
|
|
|
|
use Carbon\Carbon;
|
2016-12-17 16:59:59 +01:00
|
|
|
|
|
|
|
class FileParser {
|
|
|
|
|
2017-01-25 14:44:51 +01:00
|
|
|
const ADDED = 'added';
|
|
|
|
const REMOVED = 'removed';
|
|
|
|
|
|
|
|
private $itemService;
|
|
|
|
private $episodeService;
|
2017-01-06 09:40:27 +01:00
|
|
|
private $tmdb;
|
|
|
|
private $alternativeTitle;
|
|
|
|
private $itemCategory;
|
2016-12-17 16:59:59 +01:00
|
|
|
|
2017-01-23 22:26:04 +01:00
|
|
|
public function __construct(
|
2017-01-25 14:44:51 +01:00
|
|
|
ItemService $itemService,
|
|
|
|
EpisodeService $episodeService,
|
2017-01-23 22:26:04 +01:00
|
|
|
TMDB $tmdb,
|
|
|
|
AlternativeTitle $alternativeTitle
|
|
|
|
){
|
2017-01-25 14:44:51 +01:00
|
|
|
$this->itemService = $itemService;
|
|
|
|
$this->episodeService = $episodeService;
|
2017-01-06 09:40:27 +01:00
|
|
|
$this->tmdb = $tmdb;
|
|
|
|
$this->alternativeTitle = $alternativeTitle;
|
2016-12-17 16:59:59 +01:00
|
|
|
}
|
|
|
|
|
2017-01-06 09:40:27 +01:00
|
|
|
/**
|
|
|
|
* Make a request to flox-file-parser and get local files data.
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function fetch()
|
2016-12-17 16:59:59 +01:00
|
|
|
{
|
2017-01-06 13:00:18 +01:00
|
|
|
$this->updateTimestamp();
|
|
|
|
|
2017-01-06 09:40:27 +01:00
|
|
|
return json_decode(
|
2017-01-23 22:26:04 +01:00
|
|
|
file_get_contents(base_path('tests/fixtures/fp/all.json'))
|
2017-01-06 09:40:27 +01:00
|
|
|
);
|
2016-12-17 16:59:59 +01:00
|
|
|
}
|
|
|
|
|
2017-01-06 09:40:27 +01:00
|
|
|
/**
|
2017-01-25 14:44:51 +01:00
|
|
|
* Loop over all local files.
|
2017-01-06 09:40:27 +01:00
|
|
|
*
|
|
|
|
* @param $files
|
|
|
|
*/
|
2017-01-25 14:44:51 +01:00
|
|
|
public function updateDatabase($files)
|
2016-12-17 16:59:59 +01:00
|
|
|
{
|
2017-01-06 09:40:27 +01:00
|
|
|
foreach($files as $type => $items) {
|
|
|
|
$this->itemCategory = $type;
|
2016-12-17 16:59:59 +01:00
|
|
|
|
2017-01-06 09:40:27 +01:00
|
|
|
foreach($items as $item) {
|
2017-01-25 14:44:51 +01:00
|
|
|
$this->handleStatus($item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-01-06 09:40:27 +01:00
|
|
|
|
2017-01-25 14:44:51 +01:00
|
|
|
/**
|
|
|
|
* Check which status the file has.
|
|
|
|
*
|
|
|
|
* @param $item
|
|
|
|
* @return bool|mixed|void
|
|
|
|
*/
|
|
|
|
private function handleStatus($item)
|
|
|
|
{
|
|
|
|
switch($item->status) {
|
|
|
|
case self::ADDED:
|
|
|
|
return $this->addItem($item);
|
|
|
|
case self::REMOVED:
|
|
|
|
return $this->removeSrc($item);
|
|
|
|
}
|
|
|
|
}
|
2017-01-06 09:40:27 +01:00
|
|
|
|
2017-01-25 14:44:51 +01:00
|
|
|
/**
|
|
|
|
* See if it can find the item in our database. Otherwise search in TMDb.
|
|
|
|
*
|
|
|
|
* @param $item
|
|
|
|
* @return bool|mixed
|
|
|
|
*/
|
|
|
|
private function addItem($item)
|
|
|
|
{
|
|
|
|
$title = $item->name;
|
2017-01-06 09:40:27 +01:00
|
|
|
|
2017-01-25 14:44:51 +01:00
|
|
|
// See if file is already in our database.
|
|
|
|
if($found = $this->itemService->findBy('title', $title)) {
|
|
|
|
return $this->storeSrc($item, $found->tmdb_id);
|
2017-01-06 09:40:27 +01:00
|
|
|
}
|
2017-01-25 14:44:51 +01:00
|
|
|
|
|
|
|
// Otherwise make a new TMDb request.
|
|
|
|
return $this->tmdbSearch($title, $item);
|
2017-01-06 09:40:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Make a new request to TMDb and check against the database. Otherwise create a new item.
|
|
|
|
*
|
|
|
|
* @param $title
|
|
|
|
* @param $item
|
2017-01-23 22:26:04 +01:00
|
|
|
* @return bool|mixed
|
2017-01-06 09:40:27 +01:00
|
|
|
*/
|
|
|
|
private function tmdbSearch($title, $item)
|
|
|
|
{
|
|
|
|
$result = $this->tmdb->search($title);
|
|
|
|
|
|
|
|
if( ! $result) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->findOrCreateItem($result[0], $item);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check tmdb_id against the database or create a new item.
|
|
|
|
*
|
|
|
|
* @param $firstResult
|
|
|
|
* @param $item
|
2017-01-25 14:44:51 +01:00
|
|
|
* @return mixed
|
2017-01-06 09:40:27 +01:00
|
|
|
*/
|
|
|
|
private function findOrCreateItem($firstResult, $item)
|
|
|
|
{
|
|
|
|
$tmdbId = $firstResult['tmdb_id'];
|
|
|
|
|
|
|
|
// Check against our database.
|
2017-01-25 14:44:51 +01:00
|
|
|
if($this->itemService->findBy('tmdb_id', $tmdbId)) {
|
|
|
|
return $this->storeSrc($item, $tmdbId);
|
2017-01-06 09:40:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise create a new item from the result.
|
2017-01-25 14:44:51 +01:00
|
|
|
$created = $this->itemService->create($firstResult);
|
2017-01-06 09:40:27 +01:00
|
|
|
|
2017-01-25 14:44:51 +01:00
|
|
|
return $this->storeSrc($item, $created->tmdb_id);
|
2017-01-06 09:40:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-01-25 14:44:51 +01:00
|
|
|
* Store src from local file into items for movies or episodes for tv shows.
|
2017-01-06 09:40:27 +01:00
|
|
|
*
|
|
|
|
* @param $item
|
2017-01-25 14:44:51 +01:00
|
|
|
* @param $tmdbId
|
2017-01-06 09:40:27 +01:00
|
|
|
* @return mixed
|
|
|
|
*/
|
2017-01-25 14:44:51 +01:00
|
|
|
private function storeSrc($item, $tmdbId)
|
2017-01-06 09:40:27 +01:00
|
|
|
{
|
2017-01-25 14:44:51 +01:00
|
|
|
$model = $this->findItem($item, $tmdbId);
|
|
|
|
|
|
|
|
if($model) {
|
|
|
|
return $model->update([
|
|
|
|
'src' => $item->src,
|
|
|
|
]);
|
2017-01-06 09:40:27 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-01-25 14:44:51 +01:00
|
|
|
* Remove src for local file in items for movies or episodes for tv shows.
|
2017-01-06 09:40:27 +01:00
|
|
|
*
|
|
|
|
* @param $item
|
2017-01-25 14:44:51 +01:00
|
|
|
* @return mixed
|
2017-01-06 09:40:27 +01:00
|
|
|
*/
|
2017-01-25 14:44:51 +01:00
|
|
|
private function removeSrc($item)
|
|
|
|
{
|
|
|
|
$model = $this->findItemBySrc($item);
|
|
|
|
|
|
|
|
if($model) {
|
|
|
|
return $model->update([
|
|
|
|
'src' => null,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $item
|
|
|
|
* @param $tmdbId
|
|
|
|
* @return \Illuminate\Support\Collection|mixed
|
|
|
|
*/
|
|
|
|
private function findItem($item, $tmdbId)
|
2017-01-06 09:40:27 +01:00
|
|
|
{
|
|
|
|
if($this->itemCategory == 'tv') {
|
2017-01-25 14:44:51 +01:00
|
|
|
return $this->episodeService->findBy('episode', $tmdbId, $item);
|
2017-01-06 09:40:27 +01:00
|
|
|
}
|
|
|
|
|
2017-01-25 14:44:51 +01:00
|
|
|
return $this->itemService->findBy('tmdb_id', $tmdbId);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $item
|
|
|
|
* @return \Illuminate\Support\Collection|mixed
|
|
|
|
*/
|
|
|
|
private function findItemBySrc($item)
|
|
|
|
{
|
|
|
|
if($this->itemCategory == 'tv') {
|
|
|
|
return $this->episodeService->findBy('src', $item->src);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->itemService->findBy('src', $item->src);
|
2016-12-17 16:59:59 +01:00
|
|
|
}
|
2017-01-06 13:00:18 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Update last time we fetched flox-file-parser.
|
|
|
|
*/
|
|
|
|
private function updateTimestamp()
|
|
|
|
{
|
|
|
|
Setting::first()->update([
|
|
|
|
'last_fetch_to_file_parser' => Carbon::now(),
|
|
|
|
]);
|
|
|
|
}
|
2017-01-24 16:11:47 +01:00
|
|
|
}
|