-
Notifications
You must be signed in to change notification settings - Fork 0
/
flickr-webhook.js
150 lines (128 loc) · 3.94 KB
/
flickr-webhook.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
/* Webhook from IFTTT when new Flickr photo with tag #blog
create github file and post
*/
const githubapi = require('github'),
async = require('async'),
https = require('https');
exports.handler = function(event, context, callback) {
const { caption, url, image, key } = JSON.parse(event.body);
const { IG_GIT_USER: user, IG_GIT_TOKEN: token, IG_GIT_REPO: repo, IG_SECRET_KEY } = process.env;
if (key !== IG_SECRET_KEY) return callback(null, { statusCode: 401, body: 'Incorrect key supplied' });
if (!image || !caption || !url) return callback(null, { statusCode: 400, body: 'Params not supplied' });
const time = Date.now();
const date = new Date();
const github = new githubapi({ version: '3.0.0' });
github.authenticate({
type: 'token',
username: user,
token
});
async.waterfall([
function scrape_image_from_flickr(callback) {
console.log("Flickr url: " + url);
console.log("Flickr image: " + image);
console.log("Flickr title: " + caption);
// const imageURL = 'https://scontent.cdninstagram.com/hphotos-xaf1/t51.2885-15/s1080x1080/e15/' + imageSplit[imageSplit.length - 1];
let imageData = '';
https.get(image, (resp) => {
resp.setEncoding('base64');
resp.on('data', (data) => { imageData += data});
resp.on('end', () => callback(null, imageData));
}).on('error', (e) => new Error(`Error scraping image: ${e.message}`));
},
function upload_image_blob(image, callback) {
github.gitdata.createBlob({
owner: user,
repo: repo,
content: image,
encoding: 'base64'
}, function(err, data) {
if (err) return new Error(err);
callback(null, data.data.sha);
});
},
function get_branch_reference(image, callback){
github.gitdata.getReference({
owner: user,
user: user,
repo: repo,
ref: 'heads/master'
}, function(err, data){
if (err) return new Error(err);
callback(null, { image: image, commit: data.data.object.sha});
});
},
// Create a tree ready to commit
function create_tree(result, callback){
const content = `---
title: Flickr - ${date.toString()}
categories:
- photo
- flickr
date: ${date.toISOString().slice(0,-14)}
images:
- /images/flickr/${time}.jpg
originalURL: ${url}
syndication:
- ${url}
---
{{< picture src="/images/flickr/${time}" type="jpg" alt="" caption="${caption}" >}}
<!--more-->
`;
const files = [{
path: `static/images/flickr/${time}.jpg`,
mode: '100644',
type: 'blob',
sha: result.image
}, {
path: `content/photos/${time}.md`,
mode: '100644',
type: 'blob',
content: content
}];
github.gitdata.createTree({
owner: user,
user: user,
repo: repo,
tree: files,
base_tree: result.commit
}, function(err, data){
if (err) return new Error(err);
result.tree = data.data.sha;
callback(null, result);
});
},
function commit_the_files(result, callback){
github.gitdata.createCommit({
owner: user,
user: user,
repo: repo,
message: `New Flickr image: ${date.toString()}`,
tree: result.tree,
parents: [result.commit]
}, function(err, data){
if (err) return new Error(err);
result.new = data.data.sha;
callback(null, result);
});
},
/*
function update_git_reference(result, callback){
github.gitdata.updateReference({
owner: user,
user: user,
repo: repo,
ref: 'heads/master',
sha: result.new,
force: true
}, function(err, data){
if (err) return new Error(err);
callback(null);
});
}
*/
], function (err, result) {
if (err) return callback(null, { statusCode: 400, body: err.message });
else return callback(null, { statusCode: 200, body: 'Image imported' });
});
};