-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathengine.html
159 lines (130 loc) · 6.32 KB
/
engine.html
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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script>
// Include version 1 of the engine base library.
document.write('<link rel="import" href="' + window.Alteryx.LibDir + '1/lib/alteryx/engine/includes.html">');
</script>
<!-- Axios, a much better Promise based HTTP client for the browser and node.js -->
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<script>
const globes = {
baseUrl: '',
apiKey: '',
sections: [],
fieldMap: [
{name:'api_section',type:'V_WString',size:500},
{name:'article_section',type:'V_WString',size:500},
{name:'article_subsection',type:'V_WString',size:1000},
{name:'title',type:'V_WString',size:1000},
{name:'abstract',type:'V_WString',size:1000},
{name:'url',type:'V_WString',size:1000},
{name:'short_url',type:'V_WString',size:1000},
{name:'byline',type:'V_WString',size:1000},
{name:'updated',type:'V_WString',size:1000},
{name:'created',type:'V_WString',size:1000},
{name:'published',type:'V_WString',size:1000},
{name:'image_url',type:'V_WString',size:1000},
{name:'image_caption',type:'V_WString',size:1000},
{name:'image_copyright',type:'V_WString',size:1000}
]
}
function fetchArticles() {
// go time, make some API calls
Alteryx.Engine.SendMessage.RecordInfo("Articles", { "Field": globes.fieldMap });
var promises = [];
globes.sections.forEach(function(s){
getUrl = globes.baseUrl + '/svc/topstories/v2/' + s + '.json?api-key=' + globes.apiKey;
promises.push(axios.get(getUrl))
});
var articles = [];
axios.all(promises).then(function(results) {
results.forEach(function(res) {
Alteryx.Engine.SendMessage.Info('Downloaded ' + res.data.num_results + ' articles from the "' + res.data.section + '" section.');
for (r = 0; r < res.data.results.length; r++) {
apiSection = res.data.section
a = res.data.results[r]
imgUrl = a.multimedia[0] ? a.multimedia[0].url : '-'
imgCaption = a.multimedia[0] ? a.multimedia[0].caption : '-'
imgCopyright = a.multimedia[0] ? a.multimedia[0].copyright : '-'
articles.push([
apiSection,
a.section,
a.subsection,
a.title,
a.abstract,
a.url,
a.short_url,
a.byline,
a.updated_date,
a.created_date,
a.published_date,
imgUrl,
imgCaption,
imgCopyright
]);
}
})
Alteryx.Engine.SendMessage.PushRecords("Articles", articles);
Alteryx.Engine.SendMessage.Complete();
})
.catch(function (error) {
Alteryx.Engine.SendMessage.Error('Uh oh! Error in API callback...');
Alteryx.Engine.SendMessage.Error('This is usually the result of API call limits being exceeded (limits are 5 per second and/or 1000 per day) Try reducing the number of selected sections in the UI.');
Alteryx.Engine.SendMessage.Error('Error details are below. For other errors, please submit an issue at https://github.com/alteryx-vue/nyt-connector/issues');
Alteryx.Engine.SendMessage.Error('Error Message: ' + JSON.stringify(error.response.data.message));
Alteryx.Engine.SendMessage.Error('Response Headers: ' + JSON.stringify(error.response.headers));
Alteryx.Engine.SendMessage.Complete();
});
}
Alteryx.Plugin.DefineConnections = function()
{
return {
IncomingConnections: [],
OutgoingConnections: [{
name: "Articles"
}]
};
};
Alteryx.Plugin.PI_Init = function(config)
{
if (config.Configuration.connected > 0 && config.Configuration.apiKey == config.Configuration.lastKey){
Alteryx.Engine.SendMessage.Info('API Key valid, successful connection to ' + config.Configuration.baseUrl);
globes.baseUrl = config.Configuration.baseUrl;
globes.apiKey = config.Configuration.apiKey;
globes.sections = config.Configuration.selections;
} else if (config.Configuration.connects > 0 ) {
Alteryx.Engine.SendMessage.Error('Connection to ' + config.Configuration.baseUrl + ' failed! Please check your API Key..');
} else {
Alteryx.Engine.SendMessage.Warning('Not connected, don\'t expect any results...');
}
};
Alteryx.Plugin.II_Init = function(metaInfo){
};
Alteryx.Plugin.II_PushRecords = function(data){
};
Alteryx.Plugin.II_AllClosed = function()
{
Alteryx.Engine.SendMessage.CloseOutput(Alteryx.Plugin.DefineConnections().OutgoingConnections[0].name);
Alteryx.Engine.SendMessage.Complete();
};
Alteryx.Plugin.PI_PushAllRecords = function(recordLimit)
{
if (recordLimit.RecordLimit > 0) {
fetchArticles();
} else {
// config update only, just push field map
Alteryx.Engine.SendMessage.RecordInfo("Articles", { "Field": globes.fieldMap });
Alteryx.Engine.SendMessage.Complete();
}
};
Alteryx.Plugin.PI_Close = function()
{
Alteryx.Engine.SendMessage.PI_Close();
};
</script>
</head>
<body>
</body>
</html>