forked from fishpond-haiku/Haiku-Radio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
StationFinderRadioNetwork.cpp
134 lines (125 loc) · 4.91 KB
/
StationFinderRadioNetwork.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/*
* File: StationFinderRadioNetwork.cpp
* Author: user
*
* Created on 9. Oktober 2015, 22:51
*/
#include "StationFinderRadioNetwork.h"
#include <HttpRequest.h>
#include <HttpResult.h>
#include <DataIO.h>
#include <libxml/parser.h>
#include <libxml/tree.h>
#include "HttpUtils.h"
#include "StationFinderListenLive.h"
IconLookup::IconLookup(Station* station, BUrl iconUrl)
: fStation(station),
fIconUrl(iconUrl)
{ }
StationFinderRadioNetwork::StationFinderRadioNetwork()
: StationFinderService(),
fIconLookupThread(0),
fIconLookupList()
{
serviceName.SetTo("Radio Network");
serviceHomePage.SetUrlString("http://www.radio-browser.info");
RegisterSearchCapability("Keyword");
}
StationFinderRadioNetwork::~StationFinderRadioNetwork() {
fIconLookupList.MakeEmpty(true);
}
StationFinderService*
StationFinderRadioNetwork::Instantiate() {
return new StationFinderRadioNetwork();
}
void
StationFinderRadioNetwork::RegisterSelf() {
Register("Radio Network", &StationFinderRadioNetwork::Instantiate);
}
BObjectList<Station>*
StationFinderRadioNetwork::FindBy(int capabilityIndex, const char* searchFor,
BLooper* resultUpdateTarget) {
if (fIconLookupThread)
suspend_thread(fIconLookupThread);
fIconLookupList.MakeEmpty(true);
fIconLookupNotify = resultUpdateTarget;
BObjectList<Station>* result = new BObjectList<Station>();
BString urlString(baseUrl);
searchFor = BUrl::UrlEncode(searchFor, true, true);
urlString << searchFor;
BUrl url(urlString);
BMallocIO* data = HttpUtils::GetAll(url);
if (data) {
data->Seek(0, SEEK_SET);
const char* xmlProlog = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>";
xmlParserCtxt* parserCtxt = xmlCreatePushParserCtxt(NULL, NULL, xmlProlog, strlen(xmlProlog), "noname.xml");
xmlParseChunk(parserCtxt, ((const char*)data->Buffer()), data->BufferLength(), 1);
xmlDocPtr doc = parserCtxt->myDoc;
xmlFreeParserCtxt(parserCtxt);
for (xmlNode* nStation = doc->last->children; nStation; nStation = nStation->next) {
if (strcmp((char*)nStation->name, "station") == 0) {
Station* station = new Station("unknown");
for (xmlAttr* prop = nStation->properties; prop; prop = prop->next) {
if (strcmp((char*)prop->name, "name") == 0) {
station->SetName((char*)XML_GET_CONTENT(prop->children));
} else if (strcmp((char*)prop->name, "url") == 0) {
BString sourceString((char*)XML_GET_CONTENT(prop->children));
station->SetSource(BUrl(sourceString));
} else if (strcmp((char*)prop->name, "favicon") == 0) {
BUrl iconUrl((char*)XML_GET_CONTENT(prop->children));
fIconLookupList.AddItem(new IconLookup(station, iconUrl));
} else if (strcmp((char*)prop->name, "homepage") == 0) {
station->SetStation(((char*)XML_GET_CONTENT(prop->children)));
} else if (strcmp((char*)prop->name, "tags") == 0) {
station->SetGenre((char*)XML_GET_CONTENT(prop->children));
} else if (strcmp((char*)prop->name, "country") == 0) {
station->SetCountry((char*)XML_GET_CONTENT(prop->children));
} else if (strcmp((char*)prop->name, "bitrate") == 0) {
BString kbitRateStr((char*)XML_GET_CONTENT(prop->children));
int32 kbitRate = atoi(kbitRateStr);
station->SetBitRate(kbitRate * 1000);
}
}
if (station->Source().Path().EndsWith(".pls") ||
station->Source().Path().EndsWith(".m3u") ||
station->Source().Path().EndsWith(".m3u8"))
station->RetrieveStreamUrl();
else
station->SetStreamUrl((const BUrl)station->Source());
result->AddItem(station);
}
}
delete data;
if (!fIconLookupList.IsEmpty()) {
if (!fIconLookupThread)
fIconLookupThread = spawn_thread(&IconLookupFunc, "iconlookup", B_NORMAL_PRIORITY, this);
resume_thread(fIconLookupThread);
}
} else {
delete result;
result = NULL;
}
return result;
}
int32
StationFinderRadioNetwork::IconLookupFunc(void* data) {
StationFinderRadioNetwork* _this = (StationFinderRadioNetwork*)data;
while (!_this->fIconLookupList.IsEmpty()) {
IconLookup* item = _this->fIconLookupList.FirstItem();
BBitmap* logo = _this->logo(item->fIconUrl);
if (logo) {
item->fStation->SetLogo(logo);
BMessage* notification = new BMessage(MSG_UPDATE_STATION);
notification->AddPointer("station", item->fStation);
if (_this->fIconLookupNotify->LockLooper()) {
_this->fIconLookupNotify->PostMessage(notification);
_this->fIconLookupNotify->UnlockLooper();
}
}
_this->fIconLookupList.RemoveItem(item, true);
}
_this->fIconLookupThread = 0;
return B_OK;
}
const char*
StationFinderRadioNetwork::baseUrl = "http://www.radio-browser.info/webservice/xml/stations/";