yapi.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861
  1. #include <thread>
  2. #include <functional>
  3. #include <QJsonObject>
  4. #include <QJsonArray>
  5. #include <QJsonDocument>
  6. #include <QGuiApplication>
  7. #include <QMediaPlayer>
  8. #include "yapi.hpp"
  9. #include "file.hpp"
  10. #include "settings.hpp"
  11. #include "utils.hpp"
  12. #include "Messages.hpp"
  13. #include "AudioPlayer.hpp"
  14. using namespace py;
  15. object repeat_if_error(std::function<object()> f, int n = 10, std::string s = "NetworkError") {
  16. if (s == "") {
  17. while (true) {
  18. try {
  19. return f();
  20. } catch (error& e) {
  21. --n;
  22. if (n <= 0) throw std::move(e);
  23. }
  24. }
  25. } else {
  26. while (true) {
  27. try {
  28. return f();
  29. } catch (error& e) {
  30. if (e.type != s) throw std::move(e);
  31. --n;
  32. if (n <= 0) throw std::move(e);
  33. }
  34. }
  35. }
  36. return nullptr;
  37. }
  38. void repeat_if_error(std::function<void()> f, std::function<void(bool success)> r, int n = 10, std::string s = "NetworkError") {
  39. int tries = n;
  40. if (s == "") {
  41. while (true) {
  42. try {
  43. f();
  44. return r(true);
  45. } catch (error&) {
  46. --tries;
  47. if (tries <= 0) {
  48. return r(false);
  49. }
  50. }
  51. }
  52. } else {
  53. while (true) {
  54. try {
  55. f();
  56. return r(true);
  57. } catch (error& e) {
  58. if (e.type != s) {
  59. return r(false);
  60. }
  61. --tries;
  62. if (tries <= 0) {
  63. return r(false);
  64. }
  65. }
  66. }
  67. }
  68. }
  69. void do_async(std::function<void()> f) {
  70. std::thread(f).detach();
  71. }
  72. void repeat_if_error_async(std::function<void()> f, std::function<void(bool success)> r, int n = 10, std::string s = "NetworkError") {
  73. do_async([=]() {
  74. repeat_if_error(f, r, n, s);
  75. });
  76. }
  77. YTrack::YTrack(qint64 id, QObject* parent) : Track(parent)
  78. {
  79. _id = id;
  80. }
  81. YTrack::YTrack(object obj, QObject* parent) : Track(parent)
  82. {
  83. _id = obj.get("id").to<qint64>();
  84. _fetchYandex(obj);
  85. }
  86. YTrack::~YTrack()
  87. {
  88. }
  89. YTrack::YTrack(QObject* parent) : Track(parent)
  90. {
  91. }
  92. QString YTrack::idInt()
  93. {
  94. return QString::number(_id);
  95. }
  96. QString YTrack::title()
  97. {
  98. if (!_checkedDisk) _loadFromDisk();
  99. if (_title.isEmpty() && !_noTitle) {
  100. do_async([this](){ _fetchYandex(); saveMetadata(); });
  101. }
  102. return _title;
  103. }
  104. QString YTrack::artistsStr()
  105. {
  106. if (!_checkedDisk) _loadFromDisk();
  107. if (_author.isEmpty() && !_noAuthor) {
  108. do_async([this](){ _fetchYandex(); saveMetadata(); });
  109. }
  110. return _author;
  111. }
  112. QString YTrack::extra()
  113. {
  114. if (!_checkedDisk) _loadFromDisk();
  115. if (_extra.isEmpty() && !_noExtra) {
  116. do_async([this](){ _fetchYandex(); saveMetadata(); });
  117. }
  118. return _extra;
  119. }
  120. QString YTrack::cover()
  121. {
  122. if (!_checkedDisk) _loadFromDisk();
  123. if (Settings::ym_saveCover()) {
  124. if (_cover.isEmpty()) {
  125. if (!_noCover) _downloadCover(); // async
  126. else emit coverAborted();
  127. return "qrc:resources/player/no-cover.svg";
  128. }
  129. auto s = _relativePathToCover? QDir::cleanPath(Settings::ym_savePath() + QDir::separator() + _cover) : _cover;
  130. if (!fileExists(s)) {
  131. if (_relativePathToCover)
  132. _downloadCover(); // async
  133. return "qrc:resources/player/no-cover.svg";
  134. }
  135. return "file:" + s;
  136. } else {
  137. if (_py == none) do_async([this](){
  138. _fetchYandex();
  139. saveMetadata();
  140. if (!_py.has("cover_uri")) return;
  141. emit coverChanged(_coverUrl());
  142. });
  143. else
  144. return _coverUrl();
  145. }
  146. return "qrc:resources/player/no-cover.svg";
  147. }
  148. QMediaContent YTrack::media()
  149. {
  150. if (!_checkedDisk) _loadFromDisk();
  151. if (Settings::ym_downloadMedia()) {
  152. if (_media.isEmpty()) {
  153. if (!_noMedia) _downloadMedia(); // async
  154. else {
  155. Messages::error(tr("Failed to get Yandex.Music track media (id: %1)").arg(_id));
  156. emit mediaAborted();
  157. }
  158. return {};
  159. }
  160. auto media = QDir::cleanPath(Settings::ym_savePath() + QDir::separator() + _media);
  161. if (QFile::exists(media))
  162. return QMediaContent("file:" + media);
  163. emit mediaAborted();
  164. } else {
  165. if (_py == none) do_async([this](){
  166. _fetchYandex();
  167. saveMetadata();
  168. emit mediaChanged(QMediaContent(QUrl(_py.call("get_download_info")[0].call("get_direct_link").to<QString>())));
  169. });
  170. else
  171. return QMediaContent(QUrl(_py.call("get_download_info")[0].call("get_direct_link").to<QString>()));
  172. }
  173. return {};
  174. }
  175. qint64 YTrack::duration()
  176. {
  177. if (!_checkedDisk) _loadFromDisk();
  178. if (_duration == 0 && !_noMedia) {
  179. do_async([this](){ _fetchYandex(); saveMetadata(); });
  180. }
  181. return _duration;
  182. }
  183. bool YTrack::liked()
  184. {
  185. if (!_checkedDisk) _loadFromDisk();
  186. if (!_hasLiked) {
  187. do_async([this](){ _checkLiked(); saveMetadata(); });
  188. }
  189. return _liked;
  190. }
  191. qint64 YTrack::id()
  192. {
  193. return _id;
  194. }
  195. bool YTrack::available()
  196. {
  197. //TODO: check _py is not nil
  198. return _py.get("available").to<bool>();
  199. }
  200. QVector<YArtist> YTrack::artists()
  201. {
  202. //TODO: check _py is not nil
  203. return _py.get("artists").to<QVector<YArtist>>();
  204. }
  205. QString YTrack::coverPath()
  206. {
  207. return Settings::ym_coverPath(id());
  208. }
  209. QString YTrack::metadataPath()
  210. {
  211. return Settings::ym_metadataPath(id());
  212. }
  213. QString YTrack::mediaPath()
  214. {
  215. return Settings::ym_mediaPath(id());
  216. }
  217. QJsonObject YTrack::jsonMetadata()
  218. {
  219. QJsonObject info;
  220. info["hasTitle"] = !_noTitle;
  221. info["hasAuthor"] = !_noAuthor;
  222. info["hasExtra"] = !_noExtra;
  223. info["hasCover"] = !_noCover;
  224. info["hasMedia"] = !_noMedia;
  225. info["title"] = _title;
  226. info["extra"] = _extra;
  227. info["artistsNames"] = _author;
  228. info["artists"] = toQJsonArray(_artists);
  229. info["cover"] = _cover;
  230. info["relativePathToCover"] = _relativePathToCover;
  231. info["duration"] = _duration;
  232. if (_hasLiked) info["liked"] = _liked;
  233. return info;
  234. }
  235. QString YTrack::stringMetadata()
  236. {
  237. auto json = QJsonDocument(jsonMetadata()).toJson(QJsonDocument::Compact);
  238. return json.data();
  239. }
  240. void YTrack::saveMetadata()
  241. {
  242. if (!Settings::ym_saveInfo()) return;
  243. if (_id <= 0) return;
  244. File(metadataPath()).writeAll(jsonMetadata());
  245. }
  246. void YTrack::setLiked(bool liked)
  247. {
  248. do_async([this, liked](){
  249. QMutexLocker lock(&_mtx);
  250. _fetchYandex();
  251. repeat_if_error([this, liked]() {
  252. if (liked) {
  253. _py.call("like");
  254. } else {
  255. _py.call("dislike");
  256. }
  257. _liked = liked;
  258. emit likedChanged(liked);
  259. }, [](bool) {}, Settings::ym_repeatsIfError());
  260. saveMetadata();
  261. });
  262. }
  263. bool YTrack::_loadFromDisk()
  264. {
  265. _checkedDisk = true;
  266. if (!Settings::ym_saveInfo()) return false;
  267. if (_id <= 0) return false;
  268. auto metadataPath = Settings::ym_metadataPath(_id);
  269. if (!fileExists(metadataPath)) return false;
  270. QJsonObject doc = File(metadataPath).allJson().object();
  271. _noTitle = !doc["hasTitle"].toBool(true);
  272. _noAuthor = !doc["hasAuthor"].toBool(true);
  273. _noExtra = !doc["hasExtra"].toBool(true);
  274. _noCover = !doc["hasCover"].toBool(true);
  275. _noMedia = !doc["hasMedia"].toBool(true);
  276. _title = doc["title"].toString("");
  277. _author = doc["artistsNames"].toString("");
  278. _extra = doc["extra"].toString("");
  279. _cover = doc["cover"].toString("");
  280. _relativePathToCover = doc["relativePathToCover"].toBool(true);
  281. _media = _noMedia? "" : QString::number(_id) + ".mp3";
  282. auto liked = doc["liked"];
  283. if (liked.isBool()) _hasLiked = true;
  284. _liked = liked.toBool(false);
  285. if (!doc["duration"].isDouble() && !_noMedia) {
  286. //TODO: load duration from media and save data to file. use taglib
  287. _duration = 0;
  288. do_async([this](){ _fetchYandex(); saveMetadata(); });
  289. } else {
  290. _duration = doc["duration"].toInt();
  291. }
  292. return true;
  293. }
  294. void YTrack::_fetchYandex()
  295. {
  296. QMutexLocker lock(&_mtx);
  297. if (_py != py::none) return;
  298. auto _pys = YClient::instance->fetchTracks(_id);
  299. if (_pys.empty()) {
  300. _fetchYandex(none);
  301. } else {
  302. _fetchYandex(_pys[0]);
  303. }
  304. }
  305. void YTrack::_fetchYandex(object _pys)
  306. {
  307. QMutexLocker lock(&_mtx);
  308. try {
  309. if (_pys == none) {
  310. _title = "";
  311. _author = "";
  312. _extra = "";
  313. _cover = "";
  314. _media = "";
  315. _duration = 0;
  316. _noTitle = true;
  317. _noAuthor = true;
  318. _noExtra = true;
  319. _noCover = true;
  320. _noMedia = true;
  321. _liked = false;
  322. } else {
  323. _py = _pys;
  324. _title = _py.get("title").to<QString>();
  325. _noTitle = _title.isEmpty();
  326. emit titleChanged(_title);
  327. auto artists_py = _py.get("artists").to<QVector<py::object>>();
  328. QVector<QString> artists_str;
  329. artists_str.reserve(artists_py.length());
  330. for (auto&& e : artists_py) {
  331. artists_str.append(e.get("name").to<QString>());
  332. _artists.append(e.get("id").to<qint64>());
  333. }
  334. _author = join(artists_str, ", ");
  335. _noAuthor = _author.isEmpty();
  336. emit artistsStrChanged(_author);
  337. _extra = _py.get("version").to<QString>();
  338. _noExtra = _extra.isEmpty();
  339. emit extraChanged(_extra);
  340. _duration = _py.get("duration_ms").to<qint64>();
  341. emit durationChanged(_duration);
  342. }
  343. } catch (py::error& e) {
  344. Messages::error(tr("Failed to load Yandex.Music track (id: %1)").arg(_id));
  345. }
  346. }
  347. void YTrack::_downloadCover()
  348. {
  349. do_async([this](){
  350. QMutexLocker lock(&_mtx);
  351. _fetchYandex();
  352. if (_noCover) {
  353. _cover = "";
  354. emit coverAborted();
  355. return;
  356. }
  357. repeat_if_error([this]() {
  358. _py.call("download_cover", std::initializer_list<object>{coverPath(), Settings::ym_coverQuality()});
  359. _cover = QString::number(_id) + ".png";
  360. }, [this](bool success) {
  361. if (success) emit coverChanged(cover());
  362. else {
  363. _noCover = true;
  364. emit coverAborted();
  365. }
  366. }, Settings::ym_repeatsIfError());
  367. saveMetadata();
  368. });
  369. }
  370. void YTrack::_downloadMedia()
  371. {
  372. do_async([this](){
  373. QMutexLocker lock(&_mtx);
  374. _fetchYandex();
  375. if (_noMedia) {
  376. emit mediaAborted();
  377. return;
  378. }
  379. repeat_if_error([this]() {
  380. _py.call("download", mediaPath());
  381. _media = QString::number(_id) + ".mp3";
  382. }, [this](bool success) {
  383. if (success) emit mediaChanged(media());
  384. else {
  385. _noCover = true;
  386. emit mediaAborted();
  387. }
  388. }, Settings::ym_repeatsIfError());
  389. saveMetadata();
  390. });
  391. }
  392. void YTrack::_checkLiked()
  393. {
  394. do_async([this](){
  395. QMutexLocker lock(&_mtx);
  396. _fetchYandex();
  397. repeat_if_error([this]() {
  398. auto ult = _py.get("client").call("users_likes_tracks").get("tracks_ids");
  399. _liked = false;
  400. for (auto&& p : ult) {
  401. if (!p.contains(":")) continue;
  402. if (p.call("split", ":")[0].to<int>() == _id) {
  403. _liked = true;
  404. break;
  405. }
  406. }
  407. }, [this](bool success) {
  408. _hasLiked = success;
  409. if (success) emit likedChanged(_liked);
  410. }, Settings::ym_repeatsIfError());
  411. });
  412. }
  413. QString YTrack::_coverUrl()
  414. {
  415. if (!_py.has("cover_uri")) return "";
  416. auto a = "http://" + _py.get("cover_uri").to<QString>();
  417. a.remove(a.length() - 2, 2);
  418. a += "m" + Settings::ym_coverQuality();
  419. return a;
  420. }
  421. YArtist::YArtist(object impl, QObject* parent) : QObject(parent)
  422. {
  423. this->impl = impl;
  424. _id = impl.get("id").to<int>();
  425. }
  426. YArtist::YArtist()
  427. {
  428. }
  429. YArtist::YArtist(const YArtist& copy) : QObject(nullptr), impl(copy.impl), _id(copy._id)
  430. {
  431. }
  432. YArtist& YArtist::operator=(const YArtist& copy)
  433. {
  434. impl = copy.impl;
  435. _id = copy._id;
  436. return *this;
  437. }
  438. int YArtist::id()
  439. {
  440. return _id;
  441. }
  442. QString YArtist::name()
  443. {
  444. return impl.get("name").to<QString>();
  445. }
  446. QString YArtist::coverPath()
  447. {
  448. return Settings::ym_artistCoverPath(id());
  449. }
  450. QString YArtist::metadataPath()
  451. {
  452. return Settings::ym_artistMetadataPath(id());
  453. }
  454. QJsonObject YArtist::jsonMetadata()
  455. {
  456. QJsonObject info;
  457. info["id"] = id();
  458. info["name"] = name();
  459. info["cover"] = coverPath();
  460. return info;
  461. }
  462. QString YArtist::stringMetadata()
  463. {
  464. auto json = QJsonDocument(jsonMetadata()).toJson(QJsonDocument::Compact);
  465. return json.data();
  466. }
  467. void YArtist::saveMetadata()
  468. {
  469. File(metadataPath()).writeAll(jsonMetadata());
  470. }
  471. bool YArtist::saveCover(int quality)
  472. {
  473. bool successed;
  474. QString size = QString::number(quality) + "x" + QString::number(quality);
  475. repeat_if_error([this, size]() {
  476. impl.call("download_og_image", std::initializer_list<object>{coverPath(), size});
  477. }, [&successed](bool success) {
  478. successed = success;
  479. }, Settings::ym_repeatsIfError());
  480. return successed;
  481. }
  482. YPlaylist::YPlaylist(py::object impl, QObject* parent) : QObject(parent), impl(impl)
  483. {
  484. }
  485. YPlaylist::YPlaylist()
  486. {
  487. }
  488. QString YPlaylist::name()
  489. {
  490. return impl.get("title").to<QString>();
  491. }
  492. QUrl YPlaylist::cover()
  493. {
  494. try {
  495. auto a = "http://" + impl.get("cover").get("uri").to<QString>();
  496. return QUrl(a.replace("%%", "m" + Settings::ym_coverQuality()));
  497. } catch (py::error& e) {
  498. return QUrl("qrc:resources/player/no-cover.svg");
  499. }
  500. }
  501. refPlaylist YPlaylist::toPlaylist()
  502. {
  503. DPlaylist* res = new DPlaylist(this);
  504. auto a = impl.call("fetch_tracks");
  505. for (auto&& p : a) {
  506. if (!p.has("id")) continue;
  507. res->add(refTrack(new YTrack(p.get("id").to<int>(), YClient::instance)));
  508. }
  509. return refPlaylist(res);
  510. }
  511. bool YPlaylist::setName(QString name)
  512. {
  513. Q_UNUSED(name)
  514. // TODO
  515. return false;
  516. }
  517. bool YPlaylist::setCover(QUrl cover)
  518. {
  519. Q_UNUSED(cover)
  520. // TODO
  521. return false;
  522. }
  523. YLikedTracks::YLikedTracks(QObject* parent) : YPlaylist(py::none, parent)
  524. {
  525. }
  526. YLikedTracks* YLikedTracks::instance = new YLikedTracks;
  527. YLikedTracks* YLikedTracks::qmlInstance(QQmlEngine*, QJSEngine*)
  528. {
  529. return instance;
  530. }
  531. QString YLikedTracks::name()
  532. {
  533. return tr("Favorites");
  534. }
  535. QUrl YLikedTracks::cover()
  536. {
  537. return QUrl("qrc:/resources/covers/like.png");
  538. }
  539. refPlaylist YLikedTracks::toPlaylist()
  540. {
  541. DPlaylist* res = new DPlaylist(this);
  542. try {
  543. if (!YClient::instance->initialized()) throw std::runtime_error(tr("Yandex music api is not initialized").toStdString());
  544. auto a = YClient::instance->me.call("users_likes_tracks").get("tracks_ids");
  545. for (auto&& p : a) {
  546. if (!p.contains(":")) continue;
  547. res->add(refTrack(new YTrack(p.call("split", ":")[0].to<int>(), YClient::instance)));
  548. }
  549. } catch (std::exception& e) {
  550. Messages::error(tr("Failed to load Yandex.Music user liked tracks"), e.what());
  551. }
  552. return refPlaylist(res);
  553. }
  554. YClient::~YClient()
  555. {
  556. if (instance == this) instance = nullptr;
  557. }
  558. YClient::YClient(QObject *parent) : QObject(parent)
  559. {
  560. instance = this;
  561. }
  562. YClient* YClient::instance = new YClient;
  563. YClient* YClient::qmlInstance(QQmlEngine*, QJSEngine*)
  564. {
  565. return instance;
  566. }
  567. bool YClient::initialized()
  568. {
  569. return _initialized;
  570. }
  571. refTrack YClient::track(qint64 id)
  572. {
  573. return refTrack(new YTrack(id, this));
  574. }
  575. bool YClient::isLoggined()
  576. {
  577. return loggined;
  578. }
  579. void YClient::init()
  580. {
  581. if (_initialized) return;
  582. try {
  583. ym = module("yandex_music", true);
  584. ym_request = ym/"utils"/"request";
  585. ym.get("Client").set("notice_displayed", true);
  586. _initialized = true;
  587. emit initializedChanged(true);
  588. } catch (py::error& e) {
  589. Messages::error(tr("Failed to initialize yandex music client"), e.what());
  590. emit initializedChanged(false);
  591. }
  592. }
  593. QString YClient::token(QString login, QString password)
  594. {
  595. if (!initialized()) return "";
  596. return ym.call("generate_token_by_username_and_password", {login, password}).to<QString>();
  597. }
  598. bool YClient::login(QString token)
  599. {
  600. if (!initialized()) return false;
  601. loggined = false;
  602. repeat_if_error([this, token]() {
  603. me = ym.call("Client", token);
  604. }, [this](bool success) {
  605. loggined = success;
  606. }, Settings::ym_repeatsIfError());
  607. return loggined;
  608. }
  609. void YClient::login(QString token, const QJSValue& callback)
  610. {
  611. do_async<bool>(this, callback, &YClient::login, token);
  612. }
  613. bool YClient::loginViaProxy(QString token, QString proxy)
  614. {
  615. if (!initialized()) return false;
  616. loggined = false;
  617. repeat_if_error([this, token, proxy]() {
  618. std::map<std::string, object> kwargs;
  619. kwargs["proxy_url"] = proxy;
  620. object req = ym_request.call("Request", std::initializer_list<object>{}, kwargs);
  621. kwargs.clear();
  622. kwargs["request"] = req;
  623. me = ym.call("Client", token, kwargs);
  624. }, [this](bool success) {
  625. loggined = success;
  626. }, Settings::ym_repeatsIfError());
  627. return loggined;
  628. }
  629. void YClient::loginViaProxy(QString token, QString proxy, const QJSValue& callback)
  630. {
  631. do_async<bool>(this, callback, &YClient::loginViaProxy, token, proxy);
  632. }
  633. QVector<object> YClient::fetchTracks(qint64 id)
  634. {
  635. if (!initialized()) return {};
  636. QVector<py::object> tracks;
  637. repeat_if_error([this, id, &tracks]() {
  638. tracks = me.call("tracks", std::vector<object>{id}).to<QVector<py::object>>();
  639. }, [](bool) {}, Settings::ym_repeatsIfError());
  640. return tracks;
  641. }
  642. YLikedTracks* YClient::likedTracks()
  643. {
  644. return YLikedTracks::instance;
  645. }
  646. YPlaylist* YClient::playlist(int id)
  647. {
  648. if (id == 3) return likedTracks();
  649. if (!initialized()) return nullptr;
  650. try {
  651. return new YPlaylist(me.call("playlists_list", me.get("me").get("account").get("uid").to<QString>() + ":" + QString::number(id))[0]);
  652. } catch (py::error& e) {
  653. Messages::error(tr("Failed to load Yandex.Music playlist (id: %1)").arg(id), e.what());
  654. }
  655. return nullptr;
  656. }
  657. Playlist* YClient::oneTrack(qint64 id)
  658. {
  659. DPlaylist* res = new DPlaylist(this);
  660. if (!initialized()) return res;
  661. res->add(track(id));
  662. return res;
  663. }
  664. YPlaylist* YClient::userDailyPlaylist()
  665. {
  666. if (!initialized()) return nullptr;
  667. try {
  668. auto ppb = me.call("landing", std::vector<object>{"personalplaylists"}).get("blocks")[0];
  669. return new YPlaylist(ppb.get("entities")[0].get("data").get("data"));
  670. } catch (py::error& e) {
  671. Messages::error(tr("Failed to load Yandex.Music daily playlist"), e.what());
  672. }
  673. return nullptr;
  674. }
  675. Playlist* YClient::userTrack(int id)
  676. {
  677. DPlaylist* res = new DPlaylist(this);
  678. res->add(refTrack(new UserTrack(id)));
  679. return res;
  680. }
  681. Playlist* YClient::downloadsPlaylist()
  682. {
  683. DPlaylist* res = new DPlaylist(this);
  684. if (!initialized()) return res;
  685. QDir recoredDir(Settings::ym_savePath());
  686. QStringList allFiles = recoredDir.entryList(QDir::Files, QDir::SortFlag::Name);
  687. for (auto s : allFiles) {
  688. if (!s.endsWith(".json")) continue;
  689. s.chop(5);
  690. res->add(track(s.toInt()));
  691. }
  692. recoredDir = QDir("user");
  693. allFiles = recoredDir.entryList(QDir::Files, QDir::SortFlag::Name);
  694. for (auto s : qAsConst(allFiles)) {
  695. if (!s.endsWith(".json")) continue;
  696. s.chop(5);
  697. res->add(refTrack(new UserTrack(s.toInt())));
  698. }
  699. return res;
  700. }
  701. YPlaylistsModel* YClient::homePlaylistsModel()
  702. {
  703. auto res = new YPlaylistsModel(this);
  704. if (!initialized()) return res;
  705. res->playlists.append(likedTracks());
  706. try {
  707. for (auto&& p : me.call("landing", std::vector<object>{"personalplaylists"}).get("blocks")[0].get("entities")) {
  708. try {
  709. res->playlists.append(new YPlaylist(p.get("data").get("data")));
  710. } catch (py::error& e) {
  711. Messages::error(tr("Failed to load one of Yandex.Music smart playlists"), e.what());
  712. }
  713. }
  714. } catch (py::error& e) {
  715. Messages::error(tr("Failed to load Yandex.Music smart playlists"), e.what());
  716. }
  717. return res;
  718. }
  719. void YClient::playPlaylist(YPlaylist* playlist)
  720. {
  721. if (playlist == nullptr) return;
  722. AudioPlayer::instance->play(playlist->toPlaylist());
  723. }
  724. void YClient::addUserTrack(QString media, QString cover, QString title, QString artists, QString extra)
  725. {
  726. UserTrack().setup(media, cover, title, artists, extra);
  727. }
  728. YPlaylistsModel::YPlaylistsModel(QObject* parent) : QAbstractListModel(parent)
  729. {
  730. }
  731. int YPlaylistsModel::rowCount(const QModelIndex&) const
  732. {
  733. return playlists.length();
  734. }
  735. QVariant YPlaylistsModel::data(const QModelIndex& index, int) const
  736. {
  737. if (index.row() >= playlists.length()) return QVariant::Invalid;
  738. QVariant res;
  739. res.setValue(playlists[index.row()]);
  740. return res;
  741. }
  742. QHash<int, QByteArray> YPlaylistsModel::roleNames() const
  743. {
  744. static QHash<int, QByteArray>* pHash = nullptr;
  745. if (!pHash) {
  746. pHash = new QHash<int, QByteArray>;
  747. (*pHash)[Qt::UserRole + 1] = "element";
  748. }
  749. return *pHash;
  750. }