Skip to content

Commit

Permalink
Добавил все умные плейлисты яндекс музыки на главный экран
Browse files Browse the repository at this point in the history
  • Loading branch information
levovix0 committed Jun 2, 2021
1 parent bd770e7 commit 3676458
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 87 deletions.
2 changes: 1 addition & 1 deletion ID.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ refPlaylist ID::toPlaylist() const
if (client == ckYandex) {
if (YClient::instance == nullptr) return nullptr;
if (id == -1) return YClient::instance->userDailyPlaylist()->toPlaylist();
return refPlaylist(YClient::instance->playlist(id));
return nullptr;
}
if (client == ckNone) {
if (YClient::instance == nullptr) return nullptr;
Expand Down
1 change: 1 addition & 0 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ int main(int argc, char *argv[])
qmlRegisterType<DFileDialog>("DMusic", 1, 0, "DFileDialog");
qmlRegisterType<YPlaylist>("DMusic", 1, 0, "YPlaylist");
qmlRegisterType<YLikedTracks>("DMusic", 1, 0, "YLikedTracks");
qmlRegisterType<YPlaylistsModel>("DMusic", 1, 0, "YPlaylistsModel");

qmlRegisterSingletonType<Messages>("DMusic", 1, 0, "Messages", &Messages::qmlInstance);
qmlRegisterSingletonType<YClient>("DMusic", 1, 0, "YClient", &YClient::qmlInstance);
Expand Down
25 changes: 11 additions & 14 deletions main.qml
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,7 @@ Window {
}

function afterLogin() {
_userDailyPlaylist.playlist = YClient.userDailyPlaylist()
_userLikedPlaylist.playlist = YClient.userLikedTracks()
_yandexHomePlaylistsRepeater.model = YClient.homePlaylistsModel()
}

function autologin() {
Expand Down Expand Up @@ -235,24 +234,22 @@ Window {
else if (event.key == Qt.Key_A) _player.prev()
}

PlaylistEntry {
id: _userLikedPlaylist
Row {
id: _yandexHomePlaylists
spacing: 25
anchors.left: root.left
anchors.top: _title.bottom
anchors.leftMargin: 25
anchors.topMargin: 25

onPlay: YClient.playPlaylist(playlist)
}

PlaylistEntry {
id: _userDailyPlaylist
anchors.left: _userLikedPlaylist.right
anchors.top: _title.bottom
anchors.leftMargin: 25
anchors.topMargin: 25
Repeater {
id: _yandexHomePlaylistsRepeater
PlaylistEntry {
playlist: element

onPlay: YClient.playPlaylist(playlist)
onPlay: YClient.playPlaylist(playlist)
}
}
}

ListModel {
Expand Down
64 changes: 36 additions & 28 deletions translations/russian.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,44 +119,52 @@ usage: %1 [options]
<context>
<name>YClient</name>
<message>
<location filename="../yapi.cpp" line="679"/>
<location filename="../yapi.cpp" line="678"/>
<source>Failed to initialize yandex music client</source>
<translation>Не удалось инициализировать клиент Яндекс.Музыки</translation>
</message>
<message>
<location filename="../yapi.cpp" line="808"/>
<source>Failed to load one of Yandex.Music smart playlists</source>
<translation>Не удалось загрузить один из умных плейлистов Яндекс.Музыки</translation>
</message>
<message>
<location filename="../yapi.cpp" line="812"/>
<source>Failed to load Yandex.Music smart playlists</source>
<translation>Не удалось загрузить умные плейлисты Яндекс.Музыки</translation>
</message>
<message>
<source>Failed to load Yandex.Music user liked tracks (playlist with id 3)</source>
<translation type="vanished">Не удалось загрузить понравившиеся треки пользователя Яндекс.Музыки</translation>
</message>
<message>
<location filename="../yapi.cpp" line="751"/>
<source>Failed to load Yandex.Music user liked tracks</source>
<translation>Не удалось загрузить понравившиеся треки пользователя Яндекс.Музыки</translation>
<translation type="vanished">Не удалось загрузить понравившиеся треки пользователя Яндекс.Музыки</translation>
</message>
<message>
<location filename="../yapi.cpp" line="774"/>
<source>Failed to load Yandex.Music playlist (id: %1)</source>
<translation>Не удалось загрузить плейлист Яндекс.Музыки (id: %1)</translation>
<translation type="vanished">Не удалось загрузить плейлист Яндекс.Музыки (id: %1)</translation>
</message>
<message>
<location filename="../yapi.cpp" line="794"/>
<location filename="../yapi.cpp" line="765"/>
<source>Failed to load Yandex.Music daily playlist</source>
<translation>Не удалось загрузить плейлист дня Яндекс.Музыки</translation>
</message>
</context>
<context>
<name>YLikedTracks</name>
<message>
<location filename="../yapi.cpp" line="612"/>
<location filename="../yapi.cpp" line="611"/>
<source>Favorites</source>
<translation>Мне нравится</translation>
</message>
<message>
<location filename="../yapi.cpp" line="631"/>
<location filename="../yapi.cpp" line="630"/>
<source>Failed to load Yandex.Music user liked tracks</source>
<translation>Не удалось загрузить понравившиеся треки пользователя Яндекс.Музыки</translation>
</message>
<message>
<location filename="../yapi.cpp" line="624"/>
<location filename="../yapi.cpp" line="623"/>
<source>Yandex music api is not initialized</source>
<translation>Api Яндекс.Музыки не инициализировано</translation>
</message>
Expand All @@ -168,85 +176,85 @@ usage: %1 [options]
<context>
<name>YTrack</name>
<message>
<location filename="../yapi.cpp" line="174"/>
<location filename="../yapi.cpp" line="173"/>
<source>Failed to get Yandex.Music track media (id: %1)</source>
<translation>Не удалось загрузить медиа для трека Яндекс.Музыки (id: %1)</translation>
</message>
<message>
<location filename="../yapi.cpp" line="389"/>
<location filename="../yapi.cpp" line="388"/>
<source>Failed to load Yandex.Music track (id: %1)</source>
<translation>Не удалось загрузить трек Яндекс.Музыки (id: %1)</translation>
</message>
</context>
<context>
<name>main</name>
<message>
<location filename="../main.qml" line="118"/>
<location filename="../main.qml" line="117"/>
<source>Play</source>
<extracomment>Play button</extracomment>
<translation>Прослушать</translation>
</message>
<message>
<location filename="../main.qml" line="104"/>
<location filename="../main.qml" line="103"/>
<source>Play playlist</source>
<translation>Прослушать плейлист</translation>
</message>
<message>
<location filename="../main.qml" line="91"/>
<location filename="../main.qml" line="90"/>
<source>ID</source>
<translation></translation>
</message>
<message>
<location filename="../main.qml" line="131"/>
<location filename="../main.qml" line="130"/>
<source>Play downloaded</source>
<translation>Прослушать скачанное</translation>
</message>
<message>
<location filename="../main.qml" line="143"/>
<location filename="../main.qml" line="142"/>
<source>Play custom</source>
<translation>Прослушать добавленный</translation>
</message>
<message>
<location filename="../main.qml" line="157"/>
<location filename="../main.qml" line="156"/>
<source>Title</source>
<translation>Заголовок</translation>
</message>
<message>
<location filename="../main.qml" line="167"/>
<location filename="../main.qml" line="166"/>
<source>Artists</source>
<translation>Авторы</translation>
</message>
<message>
<location filename="../main.qml" line="177"/>
<location filename="../main.qml" line="176"/>
<source>Extra</source>
<translation>Дополнительная информация</translation>
</message>
<message>
<location filename="../main.qml" line="182"/>
<location filename="../main.qml" line="204"/>
<location filename="../main.qml" line="181"/>
<location filename="../main.qml" line="203"/>
<source>Chose media</source>
<translation>Выберите медиафайл</translation>
</message>
<message>
<location filename="../main.qml" line="183"/>
<location filename="../main.qml" line="204"/>
<location filename="../main.qml" line="182"/>
<location filename="../main.qml" line="203"/>
<source>Audio (*.mp3 *.wav *.ogg *.m4a)</source>
<translation>Звук (*.mp3 *.wav *.ogg *.m4a)</translation>
</message>
<message>
<location filename="../main.qml" line="193"/>
<location filename="../main.qml" line="206"/>
<location filename="../main.qml" line="192"/>
<location filename="../main.qml" line="205"/>
<source>Chose cover</source>
<translation>Выберите обложку</translation>
</message>
<message>
<location filename="../main.qml" line="194"/>
<location filename="../main.qml" line="206"/>
<location filename="../main.qml" line="193"/>
<location filename="../main.qml" line="205"/>
<source>Image (*.jpg *.png *.svg)</source>
<translation>Изображение (*.jpg *.png *.svg)</translation>
</message>
<message>
<location filename="../main.qml" line="222"/>
<location filename="../main.qml" line="221"/>
<source>Add custom track</source>
<translation>Добавить трек</translation>
</message>
Expand Down
93 changes: 56 additions & 37 deletions yapi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,16 +81,15 @@ void repeat_if_error_async(std::function<void()> f, std::function<void(bool succ
}


YTrack::YTrack(qint64 id, YClient* client) : Track((QObject*)client)
YTrack::YTrack(qint64 id, QObject* parent) : Track(parent)
{
_id = id;
_client = client;
}

YTrack::YTrack(object obj, YClient* client) : Track((QObject*)client)
YTrack::YTrack(object obj, QObject* parent) : Track(parent)
{
_id = obj.get("id").to<qint64>();
_client = client;
_fetchYandex(obj);
}

YTrack::~YTrack()
Expand Down Expand Up @@ -335,7 +334,7 @@ void YTrack::_fetchYandex()
{
QMutexLocker lock(&_mtx);
if (_py != py::none) return;
auto _pys = _client->fetchTracks(_id);
auto _pys = YClient::instance->fetchTracks(_id);
if (_pys.empty()) {
_fetchYandex(none);
} else {
Expand Down Expand Up @@ -736,44 +735,16 @@ QVector<object> YClient::fetchTracks(qint64 id)
return tracks;
}

Playlist* YClient::likedTracks()
{
DPlaylist* res = new DPlaylist(this);
if (!initialized()) return res;

try {
auto a = me.call("users_likes_tracks").get("tracks_ids");
for (auto&& p : a) {
if (!p.contains(":")) continue;
res->add(track(p.call("split", ":")[0].to<int>()));
}
} catch (py::error& e) {
Messages::error(tr("Failed to load Yandex.Music user liked tracks"), e.what());
}
return res;
}

YLikedTracks* YClient::userLikedTracks()
YLikedTracks* YClient::likedTracks()
{
return YLikedTracks::instance;
}

Playlist* YClient::playlist(int id)
YPlaylist* YClient::playlist(int id)
{
if (id == 3) return likedTracks();
DPlaylist* res = new DPlaylist(this);
if (!initialized()) return res;

try {
auto a = me.call("playlists_list", me.get("me").get("account").get("uid").to<QString>() + ":" + QString::number(id))[0].call("fetch_tracks");
for (auto&& p : a) {
if (!p.has("id")) continue;
res->add(track(p.get("id").to<int>()));
}
} catch (py::error& e) {
Messages::error(tr("Failed to load Yandex.Music playlist (id: %1)").arg(id), e.what());
}
return res;
if (!initialized()) return nullptr;
return new YPlaylist(me.call("playlists_list", me.get("me").get("account").get("uid").to<QString>() + ":" + QString::number(id))[0]);
}

Playlist* YClient::oneTrack(qint64 id)
Expand Down Expand Up @@ -824,6 +795,25 @@ Playlist* YClient::downloadsPlaylist()
return res;
}

YPlaylistsModel* YClient::homePlaylistsModel()
{
auto res = new YPlaylistsModel(this);
if (!initialized()) return res;
res->playlists.append(likedTracks());
try {
for (auto&& p : me.call("landing", std::vector<object>{"personalplaylists"}).get("blocks")[0].get("entities")) {
try {
res->playlists.append(new YPlaylist(p.get("data").get("data")));
} catch (py::error& e) {
Messages::error(tr("Failed to load one of Yandex.Music smart playlists"), e.what());
}
}
} catch (py::error& e) {
Messages::error(tr("Failed to load Yandex.Music smart playlists"), e.what());
}
return res;
}

void YClient::playPlaylist(YPlaylist* playlist)
{
if (playlist == nullptr) return;
Expand All @@ -834,3 +824,32 @@ void YClient::addUserTrack(QString media, QString cover, QString title, QString
{
UserTrack().setup(media, cover, title, artists, extra);
}

YPlaylistsModel::YPlaylistsModel(QObject* parent) : QAbstractListModel(parent)
{

}

int YPlaylistsModel::rowCount(const QModelIndex&) const
{
return playlists.length();
}

QVariant YPlaylistsModel::data(const QModelIndex& index, int) const
{
if (index.row() >= playlists.length()) return QVariant::Invalid;
QVariant res;
res.setValue(playlists[index.row()]);
return res;
}


QHash<int, QByteArray> YPlaylistsModel::roleNames() const
{
static QHash<int, QByteArray>* pHash = nullptr;
if (!pHash) {
pHash = new QHash<int, QByteArray>;
(*pHash)[Qt::UserRole + 1] = "element";
}
return *pHash;
}
Loading

0 comments on commit 3676458

Please sign in to comment.