Permalink
Browse files

Less Discogs requests

  • Loading branch information...
Thoulah committed Jun 19, 2018
1 parent 6a4dbc6 commit db17c21d007dc0ecb119ad94f32e726a17a4f119
@@ -28,16 +28,17 @@ public function actionDiscogsCollection(): int {
if (!$response->isOK) :
continue;
endif;
Collection::deleteAll(['user_id' => $profile->user_id]);
$discogs->saveCollection($profile->user_id, $response->data['releases']);
$ids = $discogs->saveCollection($profile->user_id, $response->data['releases']);
for ($x = 2; $x < (int) ArrayHelper::getValue($response->data, 'pagination.pages'); $x++) :
$response = Webrequest::getDiscogsApi("/users/{$profile->discogs}/collection/folders/0/releases?".http_build_query(['page' => $x]));
if (!$response->isOK) :
continue;
endif;
$discogs->saveCollection($profile->user_id, $response->data['releases']);
$subids = $discogs->saveCollection($profile->user_id, $response->data['releases']);
$ids = array_merge($ids, $subids);
endfor;
Collection::deleteAll(['AND', 'user_id' => $profile->user_id, ['NOT IN', 'id', $ids]]);
endif;
endforeach;
@@ -77,7 +77,7 @@ public function actionAlbumpdf() {
public function actionAlbumcover() {
$tracks = Lyrics3Tracks::tracksList($this->artist, $this->year, $this->album);
if (!ArrayHelper::keyExists(0, $tracks) || !ArrayHelper::isIn($this->size, [100, 500, 800, 'cover'])) :
if (!ArrayHelper::keyExists(0, $tracks) || !ArrayHelper::isIn($this->size, [100, 500, 800])) :
throw new NotFoundHttpException('Cover not found.');
endif;
$this->redirectIfNotUrl('albumcover', $tracks);
@@ -1,7 +1,6 @@
<?php
namespace app\controllers;
use Yii;
use app\commands\FeedController;
use app\models\webhook\Github;
use yii\web\{Response, NotFoundHttpException, UnauthorizedHttpException};
@@ -95,11 +95,7 @@ public static function getCover(int $size, $album): array {
$album = $album[0]->album;
endif;
if ($size !== 'cover') :
$album->image = Image::resize($album->image, $size);
endif;
return [$fileName, $album->image];
return [$fileName, Image::resize($album->image, $size)];
}
public function getArtist() {
@@ -1,28 +1,37 @@
<?php
namespace app\models\music;
use app\models\{Image, Webrequest};
use yii\console\Controller;
use yii\helpers\ArrayHelper;
class Collection extends \yii\db\ActiveRecord {
public static function tableName(): string {
return '{{%discogs_collection}}';
}
public function saveCollection(int $user, array $data) {
public function saveCollection(int $user, array $data): array {
foreach ($data as $item) :
$addItem = new Collection();
$addItem->id = (int) ArrayHelper::getValue($item, 'basic_information.id');
$addItem->user_id = $user;
$addItem->artist = ArrayHelper::getValue($item, 'basic_information.artists.0.name');
$addItem->year = (int)ArrayHelper::getValue($item, 'basic_information.year');
$addItem->title = ArrayHelper::getValue($item, 'basic_information.title');
$addItem->image = null;
if ($image = ArrayHelper::getValue($item, 'basic_information.cover_image')) :
$img = Webrequest::getUrl($image, '');
$addItem->image = Image::resize($img->content, 250);
$id[] = (int) ArrayHelper::getValue($item, 'basic_information.id');
$insert = false;
if (!$collectionItem = Collection::findOne(['id' => (int) ArrayHelper::getValue($item, 'basic_information.id'), 'user_id' => $user])) :
$insert = true;
$collectionItem = new Collection();
endif;
$addItem->save();
$collectionItem->id = (int) ArrayHelper::getValue($item, 'basic_information.id');
$collectionItem->user_id = $user;
$collectionItem->artist = ArrayHelper::getValue($item, 'basic_information.artists.0.name');
$collectionItem->year = (int) ArrayHelper::getValue($item, 'basic_information.year');
$collectionItem->title = ArrayHelper::getValue($item, 'basic_information.title');
if ($insert) :
$collectionItem->image = null;
if ($image = ArrayHelper::getValue($item, 'basic_information.cover_image')) :
$img = Webrequest::getUrl($image, '');
$collectionItem->image = Image::resize($img->content, 250);
endif;
endif;
$collectionItem->save();
endforeach;
return $id;
}
}

0 comments on commit db17c21

Please sign in to comment.