You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

nrj.cpp 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. #include "nrj.h"
  2. #include "yaml-cpp/yaml.h"
  3. #include <QDebug>
  4. #include <QColor>
  5. #include <QUrlQuery>
  6. NRJ::NRJ(QObject *parent) : QObject(parent)
  7. {
  8. mgr = new QNetworkAccessManager(this);
  9. tries = 0;
  10. }
  11. QList<Radio *> NRJ::getRadios()
  12. {
  13. return radios;
  14. }
  15. void NRJ::setupPlayer()
  16. {
  17. tries = 0;
  18. QNetworkReply* reply = mgr->get(QNetworkRequest(QUrl("http://players.nrjaudio.fm/live-metadata/player/" + radio + "/v1.0/player_setup.yml")));
  19. connect(reply, SIGNAL(finished()), this, SLOT(onSetupFinished()));
  20. connect(reply, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(onSetupError(QNetworkReply::NetworkError)));
  21. }
  22. void NRJ::getAllCur()
  23. {
  24. if(apiBase.isEmpty())
  25. return;
  26. tries = 0;
  27. QUrl url = apiBase;
  28. QUrlQuery query(apiBase.query());
  29. query.addQueryItem("act", "get_allcur");
  30. url.setQuery(query);
  31. QNetworkReply* reply = mgr->get(QNetworkRequest(url));
  32. connect(reply, SIGNAL(finished()), this, SLOT(onGetAllCurFinished()));
  33. connect(reply, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(onGetAllCurError(QNetworkReply::NetworkError)));
  34. }
  35. void NRJ::setRadio(QString radio)
  36. {
  37. this->radio = radio;
  38. }
  39. void NRJ::onSetupFinished()
  40. {
  41. QNetworkReply* reply = (QNetworkReply*)sender();
  42. YAML::Node root = YAML::Load(reply->readAll().constData());
  43. apiBase = QUrl(root["player_api"].as<std::string>().c_str());
  44. QUrlQuery query;
  45. query.addQueryItem("id_radio", root["id_webradio"].as<std::string>().c_str());
  46. apiBase.setQuery(query);
  47. coverBase = QUrl(root["radio_pics"].as<std::string>().c_str());
  48. YAML::Node webradios = root["webradios"];
  49. QString logosRootUrl = root["radio_pics"].as<std::string>().c_str();
  50. logosRootUrl += "80x80/";
  51. for(unsigned i = 0;i < webradios.size(); ++i)
  52. {
  53. YAML::Node node = webradios[i];
  54. Radio* radio = new Radio(this);
  55. radios.append(radio);
  56. radio->setApiBase(apiBase);
  57. radio->setCoverBase(coverBase);
  58. radio->setNetworkManager(mgr);
  59. radio->setId(node["id"].as<int>());
  60. radio->setName(QString::fromUtf8(node["name"].as<std::string>().c_str()));
  61. radio->setClaim(QString::fromUtf8(node["claim"].as<std::string>().c_str()));
  62. radio->setTextColor(QColor("#" + QString(node["color"].as<std::string>().c_str())));
  63. radio->setLogoUrl(QUrl(logosRootUrl + node["logo"].as<std::string>().c_str()));
  64. radio->setStreamUrl(QUrl(node["url_128k_mp3"].as<std::string>().c_str()));
  65. }
  66. emit setupFinished();
  67. }
  68. void NRJ::onSetupError(QNetworkReply::NetworkError)
  69. {
  70. if(tries < 3)
  71. {
  72. ++tries;
  73. setupPlayer();
  74. }
  75. else
  76. {
  77. tries = 0;
  78. emit setupError();
  79. }
  80. }
  81. void NRJ::onGetAllCurFinished()
  82. {
  83. QNetworkReply* reply = (QNetworkReply*)sender();
  84. QByteArray str = reply->readAll();
  85. YAML::Node itms = YAML::Load(str.constData())["itms"];
  86. for(unsigned i = 0;i < itms.size(); ++i)
  87. {
  88. YAML::Node itm = itms[i];
  89. int id = itm["id_plst"].as<int>();
  90. for(int it = 0;it < radios.size(); ++it)
  91. {
  92. Radio* r = radios.at(it);
  93. if(r->getId() == id)
  94. {
  95. it = radios.size();
  96. Song* song = new Song(r);
  97. song->setDuration(qMax((int)(itm["t_dur"].as<int>() - (1000 * (QDateTime::currentDateTime().toTime_t() - itm["ts"].as<int>()))), 0));
  98. song->setArtist(itm["art"].as<std::string>().c_str());
  99. song->setTitle(itm["tit"].as<std::string>().c_str());
  100. //qDebug()<<(QDateTime::currentDateTime().toTime_t() - itm["ts"].as<int>());
  101. //song->setStartTime(itm["ts"].as<int>() * 1000);
  102. QUrl url = coverBase;
  103. url.setPath(url.path() + itm["cov"].as<std::string>().c_str());
  104. song->setCoverUrl(url);
  105. r->setCurrentSong(song);
  106. }
  107. }
  108. }
  109. }
  110. void NRJ::onGetAllCurError(QNetworkReply::NetworkError)
  111. {
  112. if(tries < 3)
  113. {
  114. ++tries;
  115. getAllCur();
  116. }
  117. else
  118. {
  119. tries = 0;
  120. emit getAllCurError();
  121. }
  122. }