-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
executable file
·80 lines (72 loc) · 2.7 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
'use strict';
const fetch = require('node-fetch');
const urllib = require('url');
const path = require('path');
const fs = require('fs');
const pathExists = require('path-exists');
const pify = require('pify');
const wallpaper = require('wallpaper');
const Listr = require('listr');
const DEST = path.resolve(process.env.HOME, 'unsplash-so-fancy');
let location;
let filename;
let imageExists;
let response;
let imagePath;
module.exports = url => {
const tasks = new Listr([
{
title: 'Preparing',
task: () => {
return new Listr([
{
title: 'Creating image folder if not exist',
task: () => pify(fs.mkdir)(DEST).catch(() => {})
},
{
title: 'Getting image filename',
task: () => fetch(url, { redirect: 'manual' }).then(res => {
if (res.status !== 302) {
throw Error('No redirect - not sure what the image ID is. Bug seph');
}
location = urllib.resolve('https://source.unsplash.com/', res.headers.get('location'));
if (!location) throw Error('No location header');
filename = path.parse(urllib.parse(location).pathname).base;
})
}
], { concurrent: true });
}
},
{
title: 'Checking if image already exists',
task: () => Promise.all([pathExists(`${filename}.jpeg`), pathExists(`${filename}.png`)])
.then(exist => imageExists = exist[0] || exist[1])
},
{
title: 'Downloading image',
task: () => fetch(location).then(res => response = res),
skip: () => imageExists
},
{
title: 'Copying image',
task: () => {
const contenttype = response.headers.get('content-type');
const ext = contenttype.indexOf('jpeg') !== -1 ? 'jpeg' : 'png'; // bleh.
imagePath = path.resolve(DEST, `${filename}.${ext}`);
response.body.pipe(fs.createWriteStream(imagePath));
return new Promise((resolve, reject) => {
response.body.once('end', () => resolve());
response.body.once('error', () => reject());
})
},
skip: () => imageExists
},
{
title: 'Setting wallpaper',
task: () => wallpaper.set(imagePath)
}
]);
tasks.run().catch(err => {
console.error(err);
});
};