File tree Expand file tree Collapse file tree 1 file changed +9
-0
lines changed Expand file tree Collapse file tree 1 file changed +9
-0
lines changed Original file line number Diff line number Diff line change
1
+ // Hug Router
2
+ // This handles all calls to the API
3
+
1
4
const router = require ( 'express' ) . Router ( ) ;
2
5
const axios = require ( 'axios' ) . default ;
3
6
const xmlParser = require ( 'xml2json' ) ;
4
7
8
+ // This is how we choose which GIF to return
5
9
function getRandomArbitrary ( max ) {
6
10
return Math . round ( Math . random ( ) * max ) ;
7
11
}
@@ -10,16 +14,21 @@ router.route('/').get((req, res) => {
10
14
11
15
var options = { method : 'GET' , url : 'http://hugcdn.nyc3.digitaloceanspaces.com/' } ;
12
16
17
+ // Make the API request
13
18
axios . request ( options ) . then ( function ( response ) {
19
+ // Convert the XML response to a JS array
14
20
const fullParsed = JSON . parse ( xmlParser . toJson ( response . data ) )
15
21
22
+ // Pick which GIF to return based off of the array
16
23
const hug = fullParsed . ListBucketResult . Contents [ getRandomArbitrary ( fullParsed . ListBucketResult . Contents . length ) ]
17
24
const stagedResponse = {
18
25
url : `https://hug.cdn.bigbrother.group/${ hug . Key } ` ,
19
26
filename : hug . Key ,
20
27
size : hug . Size
21
28
}
29
+ // Let us know that a hug has been sent out, which is very useful for knowning the activity level of the API.
22
30
console . log ( `A hug has been sent out to ${ res . req . ip } ` ) ;
31
+ // Send back the gif and related information
23
32
res . status ( 200 ) . json ( stagedResponse ) ;
24
33
25
34
} ) . catch ( function ( error ) {
You can’t perform that action at this time.
0 commit comments