/
app.js
196 lines (174 loc) · 4.3 KB
/
app.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
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
const Gfycat = require("gfycat-sdk");
var gfycat = new Gfycat({
clientId: "2_7kmTI8",
clientSecret:
"9X4E1UlJMRmVUdCrqzeeZNkNCeud2oWIGHdX_PzwP2EgM7CiXrpMSgQ_kBuOd-Yc"
});
var express = require("express");
var http = require("http");
const https = require("https");
const assert = require("assert");
var hostname = "0.0.0.0";
var port = process.env.PORT || 8080;
var app = express();
var router = express.Router();
app.use("/", router); //attach security router to everything that goes to /map
app.use(express.static(__dirname + "/public")); //allow access to the .html files in public
app.listen(port, hostname, function() {
console.log(`Server running at http://${hostname}:${port}/`);
});
gfycat.authenticate((err, data) => {
console.log("Your app is now authenticated", data);
});
function errorHappened(err) {
console.log("ERROR!");
console.log(err);
}
router
.route("/getdata")
.all(function(req, res, next) {
next();
})
.get(function(req, res, next) {
var query = req.headers.query;
if (req.headers.count) {
gfycount = req.headers.count;
} else {
gfycount = 5;
}
options = {
search_text: query,
count: gfycount,
first: 0
};
gfycat
.search(options)
.then(data => {
res.json(data);
})
.catch(err => {
errorHappened(err);
});
});
router
.route("/gfyinfo")
.all(function(req, res, next) {
next();
})
.get(function(req, res, next) {
var url = req.headers.url;
console.log("url " + url);
gfycat
.getGifDetails({ gfyId: url })
.then(data => {
console.log("gfycats", data);
res.json(data);
})
.catch(err => {
errorHappened(err);
});
});
router
.route("/trending")
.all(function(req, res, next) {
next();
})
.get(function(req, res, next) {
if (req.headers.count) {
count = req.headers.count;
} else {
count = 1;
}
gfycat
.trendingGifs({ count: count })
.then(data => {
res.json(data.gfycats[count - 1]);
})
.catch(err => {
errorHappened(err);
});
});
router
.route("/videourl")
.all(function(req, res, next) {
next();
})
.get(function(req, res, next) {
var url = req.headers.url;
options = {
host: "api.gfycat.com",
path: "/v1/gfycats/fetch/remoteurlinfo",
headers: { url: url }
};
https.get(options, function(http_res) {
// initialize the container for our data
var data = "";
// this event fires many times, each time collecting another piece of the response
http_res.on("data", function(chunk) {
// append this chunk to our growing `data` var
data += chunk;
});
// this event fires *one* time, after all the `data` events/chunks have been gathered
http_res.on("end", function() {
// you can use res.send instead of console.log to output via express
res.end(data); //If I do res.json, it doesn't work
});
});
});
router
.route("/uploadurl")
.all(function(req, res, next) {
next();
})
.post(function(req, res, next) {
var url = req.headers.url;
var duration = req.headers.duration;
var start = req.headers.start;
//hardcoded for testing
options = {
fetchUrl: url,
title: "gifNote",
private: false,
cut: { duration: duration, start: start }
};
gfycat
.upload(options)
.then(data => {
res.json(data);
})
.catch(err => {
errorHappened(err);
});
});
router
.route("/checkstatus")
.all(function(req, res, next) {
next();
})
.get(function(req, res, next) {
var id = req.headers.id;
gfycat
.checkUploadStatus(id)
.then(data => {
res.json(data);
})
.catch(err => {
errorHappened(err);
});
});
router
.route("/gfycats")
.all(function(name) {
next();
})
.get(function(req, res, next) {
var name = req.headers.name;
gfycat
.getGyfcat(name)
.then(data => {
res.json(data);
})
.catch(err => {
errorHappened(err);
});
});