Пример #1
0
	void PendingSimilarArtists::handleReplyFinished ()
	{
		auto reply = qobject_cast<QNetworkReply*> (sender ());
		reply->deleteLater ();

		const auto& similar = lastfm::Artist::getSimilar (reply);
		if (similar.isEmpty ())
		{
			emit ready ();
			return;
		}

		auto begin = similar.begin ();
		auto end = similar.end ();
		const int distance = std::distance (begin, end);
		if (distance > NumGet_)
			std::advance (begin, distance - NumGet_);

		InfosWaiting_ = std::distance (begin, end);

		for (auto i = begin; i != end; ++i)
		{
			auto infoReply = lastfm::Artist (i.value ()).getInfo ();
			infoReply->setProperty ("Similarity", i.key ());
			connect (infoReply,
					SIGNAL (finished ()),
					this,
					SLOT (handleInfoReplyFinished ()));
			connect (infoReply,
					SIGNAL (error (QNetworkReply::NetworkError)),
					this,
					SLOT (handleInfoReplyError ()));
		}
	}
	void PendingSimilarArtists::handleReplyFinished ()
	{
		auto reply = qobject_cast<QNetworkReply*> (sender ());
		reply->deleteLater ();

		QDomDocument doc;
		if (!doc.setContent (reply->readAll ()))
		{
			emit ready ();
			return;
		}

		const auto& artistElems = doc.elementsByTagName ("artist");
		if (artistElems.isEmpty ())
		{
			emit ready ();
			return;
		}

		QList<QPair<QString, double>> similar;
		for (int i = 0, size = artistElems.size (); i < size; ++i)
		{
			const auto& elem = artistElems.at (i).toElement ();
			similar << qMakePair (elem.firstChildElement ("name").text (),
						elem.firstChildElement ("match").text ().toDouble ());
		}

		auto begin = similar.begin ();
		auto end = similar.end ();
		const int distance = std::distance (begin, end);
		if (distance > NumGet_)
			std::advance (begin, distance - NumGet_);

		InfosWaiting_ = std::distance (begin, end);

		for (auto i = begin; i != end; ++i)
		{
			QMap<QString, QString> params;
			params ["artist"] = i->first;
			AddLanguageParam (params);
			auto infoReply = Request ("artist.getInfo", NAM_, params);

			infoReply->setProperty ("Similarity", i->second);
			connect (infoReply,
					SIGNAL (finished ()),
					this,
					SLOT (handleInfoReplyFinished ()));
			connect (infoReply,
					SIGNAL (error (QNetworkReply::NetworkError)),
					this,
					SLOT (handleInfoReplyError ()));
		}
	}
	void PendingRecommendedArtists::handleReplyFinished ()
	{
		auto reply = qobject_cast<QNetworkReply*> (sender ());
		reply->deleteLater ();

		QDomDocument doc;
		if (!doc.setContent (reply->readAll ()))
		{
			qWarning () << Q_FUNC_INFO
					<< "unable to parse reply";
			emit error ();
			return;
		}

		auto artistElem = doc.documentElement ()
				.firstChildElement ("recommendations")
				.firstChildElement ("artist");
		auto elemGuard = [&artistElem] (void*) { artistElem = artistElem.nextSiblingElement ("artist"); };
		while (!artistElem.isNull ())
		{
			std::shared_ptr<void> guard (static_cast<void*> (0), elemGuard);

			const auto& name = artistElem.firstChildElement ("name").text ();
			if (name.isEmpty ())
				continue;

			QStringList similarTo;
			auto similarElem = artistElem.firstChildElement ("context").firstChildElement ("artist");
			while (!similarElem.isNull ())
			{
				similarTo << similarElem.firstChildElement ("name").text ();
				similarElem = similarElem.nextSiblingElement ("artist");
			}

			++InfosWaiting_;

			QMap<QString, QString> params;
			params ["artist"] = name;
			AddLanguageParam (params);
			auto infoReply = Request ("artist.getInfo", NAM_, params);

			infoReply->setProperty ("SimilarTo", similarTo);
			connect (infoReply,
					SIGNAL (finished ()),
					this,
					SLOT (handleInfoReplyFinished ()));
			connect (infoReply,
					SIGNAL (error (QNetworkReply::NetworkError)),
					this,
					SLOT (handleInfoReplyError ()));
		}
	}