2016-12-17 16:59:59 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Services;
|
|
|
|
|
2017-01-06 09:40:27 +01:00
|
|
|
use App\AlternativeTitle;
|
|
|
|
use App\Episode;
|
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-06 09:40:27 +01:00
|
|
|
private $item;
|
|
|
|
private $episode;
|
|
|
|
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(
|
|
|
|
ItemService $item,
|
|
|
|
Episode $episode,
|
|
|
|
TMDB $tmdb,
|
|
|
|
AlternativeTitle $alternativeTitle
|
|
|
|
){
|
2017-01-06 09:40:27 +01:00
|
|
|
$this->item = $item;
|
|
|
|
$this->episode = $episode;
|
|
|
|
$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
|
|
|
/**
|
|
|
|
* Loop over local files and see if it can find them in database. Otherwise search in TMDb.
|
|
|
|
*
|
|
|
|
* @param $files
|
|
|
|
*/
|
|
|
|
public function store($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) {
|
|
|
|
$title = $item->name;
|
|
|
|
|
|
|
|
// See if file is already in our database.
|
2017-01-23 22:26:04 +01:00
|
|
|
if($found = $this->item->findBy('title', $title)) {
|
2017-01-06 09:40:27 +01:00
|
|
|
$this->handleStatus($item, $found->tmdb_id);
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise make a new TMDb request.
|
|
|
|
$this->tmdbSearch($title, $item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
* @return \Exception|mixed
|
|
|
|
*/
|
|
|
|
private function findOrCreateItem($firstResult, $item)
|
|
|
|
{
|
|
|
|
$tmdbId = $firstResult['tmdb_id'];
|
|
|
|
|
|
|
|
// Check against our database.
|
2017-01-23 22:26:04 +01:00
|
|
|
if($this->item->findBy('tmdb_id', $tmdbId)) {
|
2017-01-06 09:40:27 +01:00
|
|
|
return $this->handleStatus($item, $tmdbId);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise create a new item from the result.
|
2017-01-23 22:26:04 +01:00
|
|
|
$created = $this->item->create($firstResult);
|
2017-01-06 09:40:27 +01:00
|
|
|
|
|
|
|
return $this->handleStatus($item, $created->tmdb_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check which status the file has.
|
|
|
|
* Create new src if the status is 'added'.
|
|
|
|
* Update src if status is 'updated'.
|
|
|
|
* Remove src if status is 'removed'.
|
|
|
|
*
|
|
|
|
* @param $item
|
|
|
|
* @param $tmdb_id
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function handleStatus($item, $tmdbId)
|
|
|
|
{
|
|
|
|
if($item->status == 'added') {
|
|
|
|
return $this->storeSrc($item, $tmdbId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Store src from local file into items for movies or episodes for tv shows.
|
|
|
|
*
|
|
|
|
* @param $item
|
|
|
|
* @param $tmdb_id
|
|
|
|
*/
|
|
|
|
private function storeSrc($item, $tmdbId)
|
|
|
|
{
|
|
|
|
if($this->itemCategory == 'tv') {
|
|
|
|
$model = $this->episode->findEpisode($tmdbId, $item);
|
|
|
|
} else {
|
2017-01-23 22:26:04 +01:00
|
|
|
$model = $this->item->findBy('tmdb_id', $tmdbId);
|
2017-01-06 09:40:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return $model->update([
|
|
|
|
'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(),
|
|
|
|
]);
|
|
|
|
}
|
2016-12-17 16:59:59 +01:00
|
|
|
}
|