-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
81 lines (69 loc) · 1.92 KB
/
index.ts
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
import * as trc from 'typed-rest-client';
import * as http from 'typed-rest-client/HttpClient';
import * as fs from 'fs';
import * as os from 'os';
interface BingImage {
startdate: string;
fullstartdate: string;
enddate: string;
url: string;
urlbase: string;
copyright: string;
copyrightlink: string;
title: string;
quiz: string;
wp: boolean;
hsh: string;
drk: number;
top: number;
bot: number;
hs: [];
}
interface BingTooltip {
loading: string;
previous: string;
next: string;
walle: string;
walls: string;
}
interface Bing {
images: BingImage[];
tooltips: BingTooltip[];
}
let index : number = 0;
if (process.argv.length >= 3){
index = Number(process.argv[2]);
}
const bingBaseUrl = "http://www.bing.com";
const dataUrl = `/HPImageArchive.aspx?format=js&idx=${index}&n=1`;
const userAgent = "";
const filename = "bing-wallpaper.jpeg";
async function getWallpaperUrl(): Promise<string> {
let client = new trc.RestClient(userAgent, bingBaseUrl);
let result: trc.IRestResponse<Bing> = await client.get<Bing>(dataUrl);
if (!result.result || result.result.images.length < 1) {
throw "No results";
}
return bingBaseUrl + result.result.images[0].url;
}
function getWallpaper() {
getWallpaperUrl().then((url: string) => {
let client = new http.HttpClient(userAgent);
let response = client.get(url);
response.then((data) => {
let fileStream = fs.createWriteStream(filename);
data.message.on('data', (chunk: Buffer) => {
fileStream.write(chunk);
}).on('end', () => {
fileStream.end();
fs.copyFileSync(filename, `${os.homedir()}/Pictures/bing-wallpaper.jpeg`);
});
});
});
}
fs.access(filename, fs.constants.F_OK, (error) => {
if (!error) {
fs.unlinkSync(filename);
}
getWallpaper();
});