/* <?xml version="1.0" encoding="UTF-8"?> <methodCall> <methodName>method</methodName> <params> <param><value><string>user</string></value></param> <param><value><string>challenge</string></value></param> <param><value><string>auth</string></value></param> <param><value><string>artist</string></value></param> <param><value><string>title</string></value></param> </params> </methodCall> */ bool CLastFmManager::CallXmlRpc(const CStdString& action, const CStdString& artist, const CStdString& title) { CStdString strUserName = g_guiSettings.GetString("scrobbler.lastfmusername"); CStdString strPassword = g_guiSettings.GetString("scrobbler.lastfmpass"); if (strUserName.IsEmpty() || strPassword.IsEmpty()) { CLog::Log(LOGERROR, "Last.fm CallXmlRpc no username or password set."); return false; } if (artist.IsEmpty()) { CLog::Log(LOGERROR, "Last.fm CallXmlRpc no artistname provided."); return false; } if (title.IsEmpty()) { CLog::Log(LOGERROR, "Last.fm CallXmlRpc no tracktitle provided."); return false; } char ti[20]; time_t rawtime; time ( &rawtime ); struct tm *now = gmtime(&rawtime); strftime(ti, sizeof(ti), "%Y-%m-%d %H:%M:%S", now); CStdString strChallenge = ti; CStdString strAuth(strPassword); strAuth.ToLower(); strAuth.append(strChallenge); CreateMD5Hash(strAuth, strAuth); //create request xml TiXmlDocument doc; TiXmlDeclaration * decl = new TiXmlDeclaration( "1.0", "UTF-8", "" ); doc.LinkEndChild( decl ); TiXmlElement * elMethodCall = new TiXmlElement( "methodCall" ); doc.LinkEndChild( elMethodCall ); TiXmlElement * elMethodName = new TiXmlElement( "methodName" ); elMethodCall->LinkEndChild( elMethodName ); TiXmlText * txtAction = new TiXmlText( action ); elMethodName->LinkEndChild( txtAction ); TiXmlElement * elParams = new TiXmlElement( "params" ); elMethodCall->LinkEndChild( elParams ); TiXmlElement * elParam = new TiXmlElement( "param" ); elParams->LinkEndChild( elParam ); TiXmlElement * elValue = new TiXmlElement( "value" ); elParam->LinkEndChild( elValue ); TiXmlElement * elString = new TiXmlElement( "string" ); elValue->LinkEndChild( elString ); TiXmlText * txtParam = new TiXmlText( strUserName ); elString->LinkEndChild( txtParam ); elParam = new TiXmlElement( "param" ); elParams->LinkEndChild( elParam ); elValue = new TiXmlElement( "value" ); elParam->LinkEndChild( elValue ); elString = new TiXmlElement( "string" ); elValue->LinkEndChild( elString ); txtParam = new TiXmlText( strChallenge ); elString->LinkEndChild( txtParam ); elParam = new TiXmlElement( "param" ); elParams->LinkEndChild( elParam ); elValue = new TiXmlElement( "value" ); elParam->LinkEndChild( elValue ); elString = new TiXmlElement( "string" ); elValue->LinkEndChild( elString ); txtParam = new TiXmlText( strAuth ); elString->LinkEndChild( txtParam ); elParam = new TiXmlElement( "param" ); elParams->LinkEndChild( elParam ); elValue = new TiXmlElement( "value" ); elParam->LinkEndChild( elValue ); elString = new TiXmlElement( "string" ); elValue->LinkEndChild( elString ); txtParam = new TiXmlText( artist ); elString->LinkEndChild( txtParam ); elParam = new TiXmlElement( "param" ); elParams->LinkEndChild( elParam ); elValue = new TiXmlElement( "value" ); elParam->LinkEndChild( elValue ); elString = new TiXmlElement( "string" ); elValue->LinkEndChild( elString ); txtParam = new TiXmlText( title ); elString->LinkEndChild( txtParam ); CStdString strBody; strBody << doc; CFileCurl http; CStdString html; CStdString url = "http://ws.audioscrobbler.com/1.0/rw/xmlrpc.php"; http.SetMimeType("text/xml"); if (!http.Post(url, strBody, html)) { CLog::Log(LOGERROR, "Last.fm action %s failed.", action.c_str()); return false; } if (html.Find("fault") >= 0) { CLog::Log(LOGERROR, "Last.fm return failed response: %s", html.c_str()); return false; } return true; }
bool CDAVDirectory::GetDirectory(const CStdString& strPath, CFileItemList &items) { CFileCurl dav; CURL url(strPath); CStdString strRequest = "PROPFIND"; dav.SetCustomRequest(strRequest); dav.SetMimeType("text/xml; charset=\"utf-8\""); dav.SetRequestHeader("depth", 1); dav.SetPostData( "<?xml version=\"1.0\" encoding=\"utf-8\" ?>" " <D:propfind xmlns:D=\"DAV:\">" " <D:prop>" " <D:resourcetype/>" " <D:getcontentlength/>" " <D:getlastmodified/>" " <D:creationdate/>" " <D:displayname/>" " </D:prop>" " </D:propfind>"); if (!dav.Open(url)) { CLog::Log(LOGERROR, "%s - Unable to get dav directory (%s)", __FUNCTION__, strPath.c_str()); return false; } char buffer[MAX_PATH + 1024]; CStdString strResponse; CStdString strHeader; while (dav.ReadString(buffer, sizeof(buffer))) { if (strstr(buffer, "<D:response") != NULL) { // The header should contain the xml version/utf encoding line // followed by the <multistatus> tag if (strHeader.IsEmpty()) strHeader = strResponse; strResponse = strHeader; } strResponse.append(buffer, strlen(buffer)); if (strstr(buffer, "</D:response") != NULL) { // Close the multistatus tag from the header if (strHeader.Find("<D:multistatus")) strResponse+="</D:multistatus>\n"; TiXmlDocument davResponse; if (!davResponse.Parse(strResponse)) { CLog::Log(LOGERROR, "%s - Unable to process dav directory (%s)", __FUNCTION__, strPath.c_str()); dav.Close(); return false; } TiXmlNode *pChild; // Iterate over all responses for (pChild = davResponse.RootElement()->FirstChild(); pChild != 0; pChild = pChild->NextSibling()) { if (ValueWithoutNamespace(pChild, "response")) { CFileItem item; ParseResponse(pChild->ToElement(), item); CURL url2(strPath); CURL url3(item.m_strPath); URIUtils::AddFileToFolder(url2.GetWithoutFilename(), url3.GetFileName(), item.m_strPath); if (item.GetLabel().IsEmpty()) { CStdString name(item.m_strPath); URIUtils::RemoveSlashAtEnd(name); CURL::Decode(name); item.SetLabel(URIUtils::GetFileName(name)); } if (item.m_bIsFolder) URIUtils::AddSlashAtEnd(item.m_strPath); // Add back protocol options if (!url2.GetProtocolOptions().IsEmpty()) item.m_strPath += "|" + url2.GetProtocolOptions(); if (!item.m_strPath.Equals(strPath)) { CFileItemPtr pItem(new CFileItem(item)); items.Add(pItem); } } } strResponse.clear(); } } dav.Close(); return true; }