-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathqueries.js
162 lines (145 loc) · 5.37 KB
/
queries.js
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
var http = require('http');
var itunes = require('searchitunes');
var htmlparser = require('htmlparser2');
var makeRequest = function(options, onEnd) {
reqCallback = function(response) {
var str = '';
var links = [];
response.on('data', function (chunk) {
str += chunk;
});
response.on('end', function () {
onEnd(str);
});
}
var req = http.request(options, reqCallback);
req.end();
}
var findEpisodes = function(title, radioUrl, callback) {
var options = {
host: 'itunes.so-nik.com',
path: '/getfeed.php?terms=' + radioUrl,
method: 'POST'
};
console.log("making request");
makeRequest(options, function (str) {
console.log("made request");
var link = false;
var found = false;
// Find rss feed for podcast through helper website
var parser = new htmlparser.Parser({
onopentag: function(name, attribs){
if(name === "a") {
link = true;
}
},
ontext: function(text){
console.log("link: " + link);
console.log("ontext: " + text);
if (link && !found) {
console.log(text);
found = true;
var partition = text.split("://");
var host_path = partition[1].split("/");
var host = host_path.shift();
var options = {
host: host,
path: "/" + host_path.join("/"),
};
// Request and parse rss feed for podcast
makeRequest(options, function (str) {
var inItem = false;
var currElem = "";
var description = "";
var url = "";
var results = [];
var parser = new htmlparser.Parser({
onopentag: function(name, attribs) {
if(name === "item") {
inItem = true;
}
else if(inItem && name === "enclosure") {
url = attribs.url;
console.log(url);
} else {
currElem = name;
}
},
ontext: function(text) {
if (currElem === "description") {
description = text;
}
},
onclosetag: function(name) {
if(name === "item") {
if (inItem) {
results.push({'title': title, 'url': url,});
inItem = false;
}
} else if (name === "description") {
inDescription = false;
}
}
});
parser.write(str);
parser.end();
callback(null, results);
});
}
},
onclosetag: function(tagname){
link = false;
}
});
parser.write(str);
parser.end();
});
}
var findPodcasts = function(name, period, location, callback) {
var query = {
entity: 'podcast',
country: 'US',
term: [name, period, location].join("+"),
attribute: "descriptionTerm",
};
console.log("query: " + query['term']);
itunes(query, function(err, data) {
if (err) {
callback(err, null)
} else {
var full_podcasts = [];
var idxs = [];
var len = Math.min(1, data['results'].length);
console.log("calc: " + len);
for (i = 0; i < len; i++) {
idxs.push(data['results'].splice(Math.floor(Math.random()*data['results'].length), 1)[0]);
}
console.log(idxs.length);
for (i in idxs) {
var map = {
title: idxs[i]['trackName'],
artist: idxs[i]['artistName'],
radioUrl: idxs[i]['radioStationUrl'],
tags: idxs[i]['keywords'],
img: {
30: idxs[i]['artworkUrl30'],
60: idxs[i]['artworkUrl60'],
100: idxs[i]['artworkUrl100'],
600: idxs[i]['artworkUrl600']
}
};
console.log("calling findeps");
findEpisodes(map['title'], map['radioUrl'], function(err, results) {
map['results'] = results;
full_podcasts.push(map);
if (full_podcasts.length >= len) {
callback(null, map['results'])
}
});
}
}
});
}
module.exports = {
findPodcasts: findPodcasts,
};