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
|
import QtQuick 2.0
QtObject {
id: rootObject
property var __doc: null
property bool inProgress: __doc != null
signal findResult(var result)
signal loadResult(var result)
/* Find feed by keywords.
*/
function findFeeds(searchKeywords) {
abort(true)
__doc = new XMLHttpRequest()
var doc = __doc
doc.onreadystatechange = function() {
if (doc.readyState === XMLHttpRequest.DONE) {
var resObj
if (doc.status == 200) {
resObj = JSON.parse(doc.responseText)
} else { // Error
resObj = {"responseDetails" : doc.statusText,
"responseStatus" : doc.status}
}
__doc = null
findResult(resObj)
}
}
var searchKeywordsEncoded = encodeURIComponent(searchKeywords)
var baseUrl = "https://ajax.googleapis.com/ajax/services/feed/find?v=1.0&q="
var finalRequest = baseUrl + searchKeywordsEncoded
doc.open("GET", finalRequest, true);
doc.send();
}
/* Load feed by URL.
*/
function loadFeed(feedUrl, num) {
abort(true)
if (num)
num = Math.min(num, 100)
else num = 50
__doc = new XMLHttpRequest()
var doc = __doc
doc.onreadystatechange = function() {
if (doc.readyState === XMLHttpRequest.DONE) {
var resObj
if (doc.status == 200) {
resObj = JSON.parse(doc.responseText)
} else { // Error
resObj = {"responseDetails" : doc.statusText,
"responseStatus" : doc.status}
}
__doc = null
loadResult(resObj)
}
}
var encodedUrl = encodeURIComponent(feedUrl)
var baseUrl = "https://ajax.googleapis.com/ajax/services/feed/load?v=1.0&q="
var finalRequest = baseUrl + encodedUrl
/* Number of articles to download.
*/
finalRequest += "&num=" + num
// Error messages are always on English :(
// finalRequest += "&hl=ru"
/* Add some optional params.
* May be usable:
* hl - host language, for example "hl=ru", default en.
* num - number of entries, for example "num=50", default 4, maximum 100.
* output - format of output, for example "output=json", may be xml, json_xml, json.
*/
doc.open("GET", finalRequest, true);
doc.send();
}
/* Param "isAbortOnly" used to preserve
* property "__doc" in not null state.
* inProgress binded to it so we can avoid
* additional recalculations.
*/
function abort(isAbortOnly) {
if (__doc != null) {
__doc.abort()
if (!isAbortOnly)
__doc = null
}
}
/* Return true if some kind of errors detected.
* TODO DEMO
*/
function checkForErrors(result) {
if (result.responseStatus == 200 || // HTTP OK
result.responseStatus == 0) { // ABORTED
return false
}
return true
}
} // QtObject googleFeedApi
|