00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00032
00033 #ifdef HAVE_CONFIG_H
00034 #include "autoconfig.h"
00035 #endif
00036
00037 #if defined(WEBORAMA)
00038
00039 #include "weborama_content_handler.h"
00040 #include "online_service.h"
00041 #include "tools.h"
00042 #include "metadata_handler.h"
00043 #include "cds_objects.h"
00044 #include "config_manager.h"
00045
00046 using namespace zmm;
00047 using namespace mxml;
00048
00049 bool WeboramaContentHandler::setServiceContent(zmm::Ref<mxml::Element> service)
00050 {
00051 String temp;
00052
00053 if (service->getName() != "playlist")
00054 throw _Exception(_("Recieved invalid XML for Weborama service"));
00055
00056 if (service->getAttribute(_("version")) != "1")
00057 throw _Exception(_("Recieved unsupported playlist version from Weborama"
00058 " service"));
00059
00060 Ref<Element> trackList = service->getChildByName(_("trackList"));
00061 if (trackList == nil)
00062 throw _Exception(_("Received invalid XML for Weborama service: "
00063 "track list not found!"));
00064
00065
00066
00067
00068
00069
00070
00071 this->service_xml = trackList;
00072
00073 track_count = service_xml->childCount();
00074
00075 if (track_count == 0)
00076 return false;
00077
00078 current_track_index = 0;
00079
00080 return true;
00081 }
00082
00083 Ref<CdsObject> WeboramaContentHandler::getNextObject()
00084 {
00085 String temp;
00086 struct timespec ts;
00087
00088 while (current_track_index < track_count)
00089 {
00090 Ref<Node> n = service_xml->getChild(current_track_index);
00091
00092 current_track_index++;
00093
00094 if (n == nil)
00095 return nil;
00096
00097 if (n->getType() != mxml_node_element)
00098 continue;
00099
00100 Ref<Element> track = RefCast(n, Element);
00101 if (track->getName() != "track")
00102 continue;
00103
00104
00105 Ref<CdsItemExternalURL> item(new CdsItemExternalURL());
00106 Ref<CdsResource> resource(new CdsResource(CH_DEFAULT));
00107 item->addResource(resource);
00108
00109 temp = track->getChildText(_("title"));
00110 if (!string_ok(temp))
00111 item->setTitle(_("Unknown"));
00112 else
00113 item->setTitle(temp);
00114
00115 temp = track->getAttribute(_("mimeType"));
00116 if (string_ok(temp))
00117 {
00118 item->setMimeType(temp);
00119 resource->addAttribute(MetadataHandler::getResAttrName(R_PROTOCOLINFO), renderProtocolInfo(temp));
00120 }
00121 else
00122 {
00123 log_warning("Could not retrieve mime type for %s, skipping...\n",
00124 item->getTitle().c_str());
00125 continue;
00126 }
00127
00128 item->setAuxData(_(ONLINE_SERVICE_AUX_ID), String::from(OS_Weborama));
00129
00130 temp = track->getChildText(_("identifier"));
00131 if (!string_ok(temp))
00132 {
00133 log_warning("Failed to retrieve Weborama track ID for \"%s\", skipping...\n",
00134 item->getTitle().c_str());
00135 continue;
00136 }
00137
00138 temp = String(OnlineService::getStoragePrefix(OS_Weborama)) + temp;
00139 item->setServiceID(temp);
00140
00141 temp = track->getChildText(_("location"));
00142 if (string_ok(temp))
00143 item->setURL(temp);
00144 else
00145 {
00146 log_error("Could not get location for Weborama item %s, "
00147 "skipping.\n", item->getTitle().c_str());
00148 continue;
00149 }
00150
00152 item->setClass(_("object.item.audioItem.musicTrack"));
00153
00154 temp = track->getChildText(_("creator"));
00155 if (string_ok(temp))
00156 item->setMetadata(MetadataHandler::getMetaFieldName(M_ARTIST), temp);
00157
00158
00159 temp = track->getChildText(_("album"));
00160 if (string_ok(temp))
00161 item->setMetadata(MetadataHandler::getMetaFieldName(M_ALBUM), temp);
00162
00163 temp = track->getChildText(_("duration"));
00164 if (string_ok(temp))
00165 {
00166 int d = temp.toInt();
00167 if (d > 0)
00168 resource->addAttribute(MetadataHandler::getResAttrName(
00169 R_DURATION),
00170 secondsToHMS(d/1000));
00171 }
00172
00173 temp = track->getChildText(_("bitrate"));
00174 if (string_ok(temp))
00175 {
00176 int br = temp.toInt();
00177 br = br / 8;
00178 if (br > 0)
00179 resource->addAttribute(
00180 MetadataHandler::getResAttrName(R_BITRATE),
00181 String::from(br));
00182 }
00183
00184 temp = track->getChildText(_("frequency"));
00185 if (string_ok(temp))
00186 resource->addAttribute(
00187 MetadataHandler::getResAttrName(R_SAMPLEFREQUENCY),
00188 temp);
00189
00191
00192 Ref<Element> image = track->getChildByName(_("image"));
00193 if (image != nil)
00194 {
00195 temp = image->getAttribute(_("mimeType"));
00196 if (string_ok(temp))
00197 {
00198 Ref<CdsResource> albumart(new CdsResource(CH_EXTURL));
00199 albumart->addOption(_(RESOURCE_CONTENT_TYPE), _(ID3_ALBUM_ART));
00200 albumart->addAttribute(MetadataHandler::getResAttrName(
00201 R_PROTOCOLINFO), renderProtocolInfo(temp));
00202
00203 temp = image->getAttribute(_("size"));
00204 if (string_ok(temp))
00205 albumart->addAttribute(MetadataHandler::getResAttrName(
00206 R_RESOLUTION), temp + "x" + temp);
00207
00208 temp = image->getText();
00209 if (string_ok(temp))
00210 {
00211 albumart->addOption(_(RESOURCE_OPTION_URL), temp);
00212 albumart->addOption(_(RESOURCE_OPTION_PROXY_URL), _(FALSE));
00213 item->addResource(albumart);
00214 }
00215 }
00216 }
00217
00218
00219
00220 getTimespecNow(&ts);
00221 item->setAuxData(_(ONLINE_SERVICE_LAST_UPDATE),
00222 String::from(ts.tv_sec));
00223
00224 item->setFlag(OBJECT_FLAG_ONLINE_SERVICE);
00225 try
00226 {
00227 item->validate();
00228 return RefCast(item, CdsObject);
00229 }
00230 catch (Exception ex)
00231 {
00232 log_warning("Failed to validate newly created Weborama item: %s\n",
00233 ex.getMessage().c_str());
00234 continue;
00235 }
00236 }
00237 return nil;
00238 }
00239
00240 #endif//WEBORAMA