-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
executable file
·110 lines (87 loc) · 3.03 KB
/
index.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
#! /usr/bin/env node
'use strict';
/**
* @description
* A tool that delivers therapy pugs to the directory from which it is run.
*
* @example
* pugbalm 5 //downloads 5 pugs.
*/
var BPromise = require('bluebird'),
wreck = BPromise.promisifyAll(require('wreck')),
fs = BPromise.promisifyAll(require('fs')),
querystring = require('querystring'),
_ = require('lodash'),
statusStream = process.stderr,
baseUrl = 'http://api.giphy.com/v1/gifs/search',
apiKey = 'dc6zaTOxFJmzC',
downloadLocation = process.cwd(),
params = {
q: 'pugs',
limit: Number(process.argv[2]),
offset: Date.now() % 800, // because there about 800 results in the query
fmt: 'json',
api_key: apiKey
};
// Validate cli input
if (!params.limit) {
throw new Error('You don\'t ask for therapy with a value not representative of actual therapy. SHAME');
}
if (params.limit < 0) {
throw new Error('Try a positive number for PUG therapy.');
}
/**
* @jsdoc function
* @name downloadImage
* @param {string} path
* The URL for the image to be downloaded.
* @param {string} id
* The ID of the image to be downloaded. Will be used to make the filename.
*
* @description
* Uses HTTP to download the image from the path provided and save it to the current working directory.
*/
function downloadImage(path, id) {
var destination = downloadLocation + '/' + id + '.gif';
return wreck.requestAsync('GET', path, {agent: false, timeout: 30000})
.then(function (response) {
var writeStream = fs.createWriteStream(destination);
return new BPromise(function (resolve, reject) {
writeStream.on('finish', function () {
resolve();
});
writeStream.on('error', function (err) {
reject(err);
});
response.pipe(writeStream);
});
});
};
/**
* @jsdoc function
* @name getListOfImages
* @param {string} baseUrl
* The URL for the Giphy API.
* @param {object} params
* An object containing configuration information for the API request.
* @description
* Uses HTTP to get an array of objects that contain information related to a given gif.
*/
function getListOfImages(baseUrl, params) {
var imagesDownloaded = 0;
return wreck.getAsync(baseUrl + '?' + querystring.stringify(params), {json: true})
.spread(function (response, payload) {
return payload.data;
})
.map(function (element) {
return downloadImage(element.images.original.url, element.id).then(function () {
statusStream.write('.');
imagesDownloaded++;
});
}, {concurrency: 10}).then(function () {
statusStream.write('therapy complete\n');
statusStream.write(imagesDownloaded + ' pugs were delivered to ' + downloadLocation + '.\n');
statusStream.write('Powered by GIPHY. http://giphy.com/search/' + encodeURIComponent(params.q) + '\n');
});
};
getListOfImages(baseUrl, params);