-
Notifications
You must be signed in to change notification settings - Fork 5
/
newsapi.gs
286 lines (214 loc) · 7.83 KB
/
newsapi.gs
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
var CLIENT_ID = '714070609501-r9f0gu0p8ih9b3lgepql9sgukv0mr2mk.apps.googleusercontent.com';
var CLIENT_SECRET = 'sNxhhQwCfgv4QWRdv0e4pA1R';
var BLOG_ID = '2402205635238914485';
var NEWS_API_KEY = "1cb3ca22268a477b9d2a2f65c940ecfa";
/*
* Based on the following work by others;
* https://mashe.hawksey.info/2015/10/setting-up-oauth2-access-with-google-apps-script-blogger-api-example/
* https://github.com/googlesamples/apps-script-oauth2
* https://developers.google.com/blogger/docs/3.0/getting_started
* https://github.com/benlcollins/apps_script_apis
* https://www.bettercloud.com/monitor/the-academy/use-apps-script-push-live-news-headlines-slack-google-sheets/
**/
// add custom menu
function onOpen() {
var ui = SpreadsheetApp.getUi();
ui.createMenu('Custom Blogger Menu')
.addItem('Get Blogger Posts','getBloggerPosts')
.addItem('Publish post on Blogger','postToBlogger')
.addToUi();
}
// configure the service
function getBloggerService_() {
return OAuth2.createService('Blogger')
.setAuthorizationBaseUrl('https://accounts.google.com/o/oauth2/auth')
.setTokenUrl('https://accounts.google.com/o/oauth2/token')
.setClientId(CLIENT_ID)
.setClientSecret(CLIENT_SECRET)
.setCallbackFunction('authCallback')
.setPropertyStore(PropertiesService.getUserProperties())
.setScope('https://www.googleapis.com/auth/blogger'); // this is blogger scope
}
// Logs the redict URI to register
function logRedirectUri() {
var service = getBloggerService_();
Logger.log(service.getRedirectUri());
}
// handle the callback
function authCallback(request) {
var bloggerService = getBloggerService_();
var isAuthorized = bloggerService.handleCallback(request);
if (isAuthorized) {
return HtmlService.createHtmlOutput('Success! You can close this tab.');
} else {
return HtmlService.createHtmlOutput('Denied. You can close this tab');
}
}
// Step 1: call the blogger API and get list of blogger sites associated with this google account
function getBloggerSites() {
var service = getBloggerService_();
if (service.hasAccess()) {
Logger.log("App has access.");
var api = "https://www.googleapis.com/blogger/v3/users/self/blogs";
var headers = {
"Authorization": "Bearer " + getBloggerService_().getAccessToken()
};
var options = {
"headers": headers,
"method" : "GET",
"muteHttpExceptions": true
};
var response = UrlFetchApp.fetch(api, options);
var json = JSON.parse(response.getContentText());
var bloggerIds = [];
for (var i in json.items) {
Logger.log("%s %s", json.items[i].name, json.items[i].url);
bloggerIds.push(json.items[i].id);
}
Logger.log(bloggerIds);
//return bloggerIds;
}
else {
Logger.log("App has no access yet.");
// this was the step I was missing originally
// open this url to gain authorization from blogger
var authorizationUrl = service.getAuthorizationUrl();
Logger.log('Open the following URL and re-run the script: %s',
authorizationUrl);
}
}
// Step 2: get list of all posts for specific blogger site
function getBloggerPosts() {
var service = getBloggerService_();
var blogId = BLOG_ID; // the id for my blogger site Data Chops
var api = 'https://www.googleapis.com/blogger/v3/blogs' + blogId + '/posts';
var headers = {
"Authorization": "Bearer " + getBloggerService_().getAccessToken()
};
var options = {
"headers": headers,
"method" : "GET",
"muteHttpExceptions": true
};
var response = UrlFetchApp.fetch(api, options);
var json = JSON.parse(response.getContentText());
var posts = json["items"];
Logger.log(posts.length); // result is 8, which matches the number of blog posts at http://datachops.blogspot.com/
//Logger.log(posts[0]);
var postsArray = [];
for (var i = 0; i < posts.length; i++) {
var authorName = posts[i]["author"]["displayName"];
var authorImage = '=image("https:' + posts[i]["author"]["image"]["url"] + '",4,60,60)';
var publishedDate = posts[i]["published"];
var publishedUrl = posts[i]["url"];
var title = posts[i]["title"];
var content = posts[i]["content"];
postsArray.push([publishedDate,title,publishedUrl,authorName,authorImage,content]);
}
Logger.log(postsArray);
outputToSheet(postsArray);
}
// print out results to sheet
function outputToSheet(post) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var publishedPosts = ss.getSheetByName('Published Posts');
publishedPosts.getRange(4,1,publishedPosts.getLastRow(),4).clearContent();
var outputRange = publishedPosts.getRange(4,1,post.length,4).setValues(post);
/*
// only need this snippet of code when i'm including thumbnail author images
for (var i = 0; i < post.length; i++) {
publishedPosts.setRowHeight(i + 4,65);
}
*/
}
// posting blog post from google sheet to blogger
// need to get the content from the sheet into suitable json format
// then post to blogger
function postToBlogger(publicationName,publicationKey,channel,max,suppressImages) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var toPublish = ss.getSheetByName('To Publish');
var data = ss.getDataRange().getValues();
data.splice(parseInt(0), 1); // remove the first header row
for (var i in data) {
var publicationKey = data[i][1];
var newsUrl = "https://newsapi.org/v1/articles?source="+publicationKey+"&sortBy=top&apiKey="+NEWS_API_KEY;
var newsResponse = UrlFetchApp.fetch(newsUrl);
var newsObj = JSON.parse(newsResponse);
var status = newsObj.status;
var articles = newsObj.articles;
for (var a=0; a<articles.length; a++) {
if (a > (max-1)) { break;}
//if (suppressImages) { var urlImage = articles[a].urlToImage; } else {var urlImage ="";}
var attObj = {
"fallback": publicationName+" [powered by News API]",
"color": "#36a64f",
"pretext": publicationName+" [powered by News API]",
"author_name": articles[a].author,
"title": articles[a].title,
"title_link": articles[a].url,
"ImgUrl": articles[a].urlToImage,
"fields": [
{
"title": articles[a].title,
"value": articles[a].urlToImage,
"image_url": articles[a].urlToImage,
"short": false
}
],
"image_url": articles[a].urlToImage,
"footer": "Published at: "+articles[a].publishedAt
};
var kind ='#blogger#post';
// var kind = toPublish.getRange(4,1).getValue();
// var blogId = toPublish.getRange(4,2).getValue();
var blogId = BLOG_ID
var title = articles[a].title
var content = articles[a].description
var image = articles[a].urlToImage
var imgbody = "<img src=\""+image+"\">"+content+""
var body = JSON.stringify({
'kind': kind,
'blog': {
'id': blogId
},
'title': title,
'content': imgbody,
"images": [
{
"url": image
}
],
});
}
Logger.log(body);
var service = getBloggerService_();
if (service.hasAccess()) {
var api = 'https://www.googleapis.com/blogger/v3/blogs/' + blogId + '/posts/';
var headers = {
'Authorization': 'Bearer ' + getBloggerService_().getAccessToken()
};
var options = {
'headers': headers,
'method' : 'post',
'contentType': 'application/json',
'payload': body,
'muteHttpExceptions': false
};
try {
var response = UrlFetchApp.fetch(api, options);
var responseCode = response.getResponseCode();
Logger.log(responseCode);
var json = JSON.parse(response.getContentText());
Logger.log(json);
}
catch(err) {
Logger.log(err); // error with url fetch call
}
}
else {
var authorizationUrl = service.getAuthorizationUrl();
Logger.log('Open the following URL and re-run the script: %s',
authorizationUrl);
}
}
}