Skip to content
Permalink
Browse files

Artist biography summary

  • Loading branch information...
Thoulah committed May 2, 2019
1 parent 47872b3 commit 2c4a1daa17e2f5ff425e7136ccbd1856c4bb9242

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
@@ -44,7 +44,7 @@ public function actionLastfmWeeklyArtist(): int {
foreach (User::find()->where(['blocked_at' => null])->all() as $user) :
$profile = Profile::findOne(['user_id' => $user->id]);
if (isset($profile->lastfm)) :
$response = Webrequest::getLastfmApi('user.getweeklyartistchart', $profile->lastfm, $this->limit);
$response = Webrequest::getLastfmApi('user.getweeklyartistchart', ['user' => $profile->lastfm, 'limit' => $this->limit]);
if (!$response->isOK)
continue;
@@ -85,7 +85,7 @@ public function actionOui(): int {
*/
public function actionWebfeed(string $type, string $name, string $url, string $desc): int {
$count = 0;
$response = Webrequest::getUrl('', $url);
$response = Webrequest::getUrl('', $url)->send();
if (!$response->isOK)
return self::EXIT_CODE_ERROR;
@@ -1,9 +1,10 @@
<?php
namespace app\commands;
use Yii;
use app\models\{Console, Image, Video};
use app\models\music\{Lyrics1Artists, Lyrics2Albums, Lyrics3Tracks};
use app\models\{Console, Image, Video, Webrequest};
use app\models\music\{Lyrics1Artists, Lyrics2Albums, Lyrics3Tracks, LyricsArtistInfo};
use app\models\user\{Profile, User};
use yii\helpers\ArrayHelper;
/**
* Handles all actions related to music.
@@ -100,14 +101,35 @@ public function actionAlbumPdf(): void {
Console::endProgress(true);
}
/**
* Retrieves and stores artist information.
*/
public function actionArtistInfo() {
$query = LyricsArtistInfo::find()->where(['not', ['mbid' => null]]);
foreach ($query->each() as $artist) :
$response = Webrequest::getLastfmApi('artist.getInfo', ['mbid' => $artist->mbid]);
if (!$response->isOK) :
Console::write($artist->parent, [Console::FG_GREEN], 5);
Console::writeError("ERROR!", [Console::BOLD, Console::FG_RED, CONSOLE::BLINK]);
continue;
endif;
$artistInfo = LyricsArtistInfo::findOne(['parent' => $artist->parent]);
$artistInfo->mbid = ArrayHelper::getValue($response->data, 'artist.mbid');
$artistInfo->summary = trim(ArrayHelper::getValue($response->data, 'artist.bio.summary'));
$artistInfo->save();
endforeach;
}
/**
* Checking status of album playlists and track videos.
*/
public function actionVideos(): int {
$video = new Video();
foreach (['playlists', 'videos'] as $type) :
if ($type === 'playlists') :
$query = Lyrics1Artists::find()->orderBy(['name' => SORT_ASC])->with(['albums' => function ($q) { $q->where(['not', ['playlist_source' => null, 'playlist_id' => null]]); }]);
$query = Lyrics1Artists::find()->orderBy(['name' => SORT_ASC])->with(['albums' => function($q) { $q->where(['not', ['playlist_source' => null, 'playlist_id' => null]]); }]);
foreach ($query->each() as $artist)
foreach ($artist->albums as $album)
$data[$album->playlist_source][] = ['id' => $album->playlist_id, 'artist' => $artist->name, 'year' => $album->year, 'name' => $album->name];
@@ -3,21 +3,19 @@
use Yii;
use mister42\Secrets;
use yii\helpers\Url;
use yii\httpclient\{Client, CurlTransport, Response};
use yii\httpclient\{Client, CurlTransport, Response, Request};
class Webrequest {
public static function getDiscogsApi(string $content): Response {
return self::getUrl('https://api.discogs.com/', $content);
return self::getUrl('https://api.discogs.com/', $content)->send();
}
public static function getLastfmApi(string $method, string $user, int $limit): Response {
public static function getLastfmApi(string $method, array $data): Response {
$secrets = (new Secrets())->getValues();
return self::getUrl('https://ws.audioscrobbler.com/2.0/', '', [
return self::getUrl('https://ws.audioscrobbler.com/2.0/', '', array_merge($data, [
'api_key' => $secrets['last.fm']['API'],
'limit' => $limit,
'method' => $method,
'user' => $user,
]);
]))->send();
}
public static function getYoutubeApi(string $id, string $content): Response {
@@ -26,24 +24,20 @@ public static function getYoutubeApi(string $id, string $content): Response {
'id' => $id,
'key' => $secrets['google']['API'],
'part' => $content === 'videos' ? 'snippet,status' : 'contentDetails,status',
]);
])->send();
}
public static function getUrl(string $base, string $url, array $data = []): Response {
$client = new Client(['baseUrl' => $base]);
public static function getUrl(string $base, string $url, array $data = []): Request {
$client = ($base === 'file') ? new Client(['transport' => CurlTransport::class]) : new Client(['baseUrl' => $base]);
return $client->createRequest()
->addHeaders(['user-agent' => Yii::$app->name.' (+'.Yii::$app->params['shortDomain'].')'])
->setData($data)
->setUrl($url)
->send();
->setUrl($url);
}
public static function saveUrl(string $url, string $file): Response {
public static function saveUrl(string $url, string $file, array $data = []): Response {
$fh = fopen($file, 'w');
$client = new Client(['transport' => CurlTransport::class]);
$response = $client->createRequest()
->addHeaders(['user-agent' => Yii::$app->name.' (+'.Yii::$app->params['shortDomain'].')'])
->setUrl($url)
$response = self::getUrl('file', $url, $data)
->setOutputFile($fh)
->send();
fclose($fh);
@@ -18,7 +18,7 @@ public function saveCollection(int $user, array $data, string $status): array {
$collectionItem = new Collection();
$collectionItem->image = null;
if ($image = ArrayHelper::getValue($item, 'basic_information.cover_image')) :
$img = Webrequest::getUrl($image, '');
$img = Webrequest::getUrl($image, '')->send();
if ($img = Image::resize($img->content, 250))
$collectionItem->image = $img;
endif;
@@ -1,7 +1,7 @@
<?php
namespace app\models\music;
use Yii;
use yii\db\BatchQueryResult;
use yii\db\{ActiveQuery, BatchQueryResult};
class Lyrics1Artists extends \yii\db\ActiveRecord {
public static function tableName(): string {
@@ -48,6 +48,7 @@ public static function albumsList(string $artist): array {
return self::find()
->orderBy(['year' => SORT_DESC, 'name' => SORT_ASC])
->innerJoinWith('artist', 'tracks')
->with('artistInfo')
->where(['or', 'artist.name=:artist', 'artist.url=:artist'])
->addParams([':artist' => $artist])
->all();
@@ -93,6 +94,11 @@ public function getArtist(): LyricsQuery {
return $this->hasOne(Lyrics1Artists::class, ['id' => 'parent']);
}
public function getArtistInfo(): ActiveQuery {
return $this->hasOne(LyricsArtistInfo::class, ['parent' => 'id'])
->via('artist');
}
public function getTracks(): ActiveQuery {
return $this->hasMany(Lyrics3Tracks::class, ['parent' => 'id']);
}
@@ -35,7 +35,7 @@ public function lastSeen(int $userid, bool $update = false): int {
public function updateUser(Profile $profile, int $lastSeen) {
if (isset($profile->lastfm)) :
$response = Webrequest::getLastfmApi('user.getrecenttracks', $profile->lastfm, $this->limit);
$response = Webrequest::getLastfmApi('user.getrecenttracks', ['limit' => $this->limit, 'user' => $profile->lastfm]);
if (!$response->isOK)
return false;
@@ -5,7 +5,7 @@
class WebrequestTest extends TestCase {
public function testLastfmApi() {
$this->assertTrue(Webrequest::getLastfmApi('user.getweeklyartistchart', 'rj', 5)->isOK);
$this->assertTrue(Webrequest::getLastfmApi('user.getweeklyartistchart', ['user' => 'rj', 'limit' => 5])->isOK);
}
public function testYoutubeApi() {
@@ -12,16 +12,24 @@
Html::tag('div',
Html::tag('h1', $data[0]->artist->name, ['class' => 'float-left']).
Html::tag('div',
($data[0]->artist->buy
? Html::a(Yii::$app->icon->show('bandcamp', ['prefix' => 'fab fa-']), $data[0]->artist->buy, ['class' => 'btn btn-secondary ml-1', 'title' => Yii::t('mr42', 'Buy Music of {artist}', ['artist' => $data[0]->artist->name])])
($data[0]->artistInfo->buy
? Html::a(Yii::$app->icon->show('bandcamp', ['prefix' => 'fab fa-']), $data[0]->artistInfo->buy, ['class' => 'btn btn-secondary ml-1', 'title' => Yii::t('mr42', 'Buy Music of {artist}', ['artist' => $data[0]->artist->name])])
: '').
($data[0]->artist->website
? Html::a(Yii::$app->icon->show('globe'), $data[0]->artist->website, ['class' => 'btn btn-secondary ml-1', 'title' => Yii::t('mr42', 'Website of {artist}', ['artist' => $data[0]->artist->name])])
($data[0]->artistInfo->website
? Html::a(Yii::$app->icon->show('globe'), $data[0]->artistInfo->website, ['class' => 'btn btn-secondary ml-1', 'title' => Yii::t('mr42', 'Website of {artist}', ['artist' => $data[0]->artist->name])])
: '')
, ['class' => 'float-right'])
, ['class' => 'col'])
, ['class' => 'row']);
if ($data[0]->artistInfo->summaryParsed) :
echo Html::tag('div',
Html::tag('div',
$data[0]->artistInfo->summaryParsed
, ['class' => 'col'])
, ['class' => 'row']);
endif;
foreach ($data as $album) :
echo Html::beginTag('div', ['class' => 'row']);
echo Html::beginTag('div', ['class' => ($album === end($data)) ? 'col mb-1' : 'col mb-3']);

0 comments on commit 2c4a1da

Please sign in to comment.
You can’t perform that action at this time.