您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

nrj.cpp 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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/nrj/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;
  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::onSetupFinished()
  36. {
  37. QNetworkReply* reply = (QNetworkReply*)sender();
  38. YAML::Node root = YAML::Load(reply->readAll().constData());
  39. apiBase = QUrl(root["player_api"].as<std::string>().c_str());
  40. QUrlQuery query;
  41. query.addQueryItem("id_radio", root["id_webradio"].as<std::string>().c_str());
  42. apiBase.setQuery(query);
  43. coverBase = QUrl(root["radio_pics"].as<std::string>().c_str());
  44. YAML::Node webradios = root["webradios"];
  45. QString logosRootUrl = root["radio_pics"].as<std::string>().c_str();
  46. logosRootUrl += "80x80/";
  47. for(unsigned i = 0;i < webradios.size(); ++i)
  48. {
  49. YAML::Node node = webradios[i];
  50. Radio* radio = new Radio(this);
  51. radios.append(radio);
  52. radio->setApiBase(apiBase);
  53. radio->setCoverBase(coverBase);
  54. radio->setNetworkManager(mgr);
  55. radio->setId(node["id"].as<int>());
  56. radio->setName(QString::fromUtf8(node["name"].as<std::string>().c_str()));
  57. radio->setClaim(QString::fromUtf8(node["claim"].as<std::string>().c_str()));
  58. radio->setTextColor(QColor("#" + QString(node["color"].as<std::string>().c_str())));
  59. radio->setLogoUrl(QUrl(logosRootUrl + node["logo"].as<std::string>().c_str()));
  60. radio->setStreamUrl(QUrl(node["url_128k_mp3"].as<std::string>().c_str()));
  61. }
  62. emit setupFinished();
  63. }
  64. void NRJ::onSetupError(QNetworkReply::NetworkError)
  65. {
  66. if(tries < 3)
  67. {
  68. ++tries;
  69. setupPlayer();
  70. }
  71. else
  72. {
  73. tries = 0;
  74. emit setupError();
  75. }
  76. }
  77. void NRJ::onGetAllCurFinished()
  78. {
  79. QNetworkReply* reply = (QNetworkReply*)sender();
  80. YAML::Node itms = YAML::Load(reply->readAll().constData())["itms"];
  81. for(unsigned i = 0;i < itms.size(); ++i)
  82. {
  83. YAML::Node itm = itms[i];
  84. int id = itm["id_plst"].as<int>();
  85. for(int it = 0;it < radios.size(); ++it)
  86. {
  87. Radio* r = radios.at(it);
  88. if(r->getId() == id)
  89. {
  90. it = radios.size();
  91. Song* song = new Song(r);
  92. song->setDuration(qMax((int)(itm["t_dur"].as<int>() - (1000 * (QDateTime::currentDateTime().toTime_t() - itm["ts"].as<int>()))), 0));
  93. song->setArtist(itm["art"].as<std::string>().c_str());
  94. song->setTitle(itm["tit"].as<std::string>().c_str());
  95. //qDebug()<<(QDateTime::currentDateTime().toTime_t() - itm["ts"].as<int>());
  96. //song->setStartTime(itm["ts"].as<int>() * 1000);
  97. QUrl url = coverBase;
  98. url.setPath(url.path() + itm["cov"].as<std::string>().c_str());
  99. song->setCoverUrl(url);
  100. r->setCurrentSong(song);
  101. }
  102. }
  103. }
  104. }
  105. void NRJ::onGetAllCurError(QNetworkReply::NetworkError)
  106. {
  107. if(tries < 3)
  108. {
  109. ++tries;
  110. getAllCur();
  111. }
  112. else
  113. {
  114. tries = 0;
  115. emit getAllCurError();
  116. }
  117. }