-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
130 lines (117 loc) · 3.65 KB
/
Copy pathindex.js
File metadata and controls
130 lines (117 loc) · 3.65 KB
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
const fs = require('fs');
const path = require('path');
const https = require('https');
const { app, Menu, Tray, net, shell } = require('electron');
const { exec } = require('child_process');
const { menubar } = require('menubar');
const iconPath = path.join(__dirname, 'assets', 'images', 'icon.png');
const fileDir = path.join(app.getPath('userData'), 'gallery27');
const urlG27 = 'http://api.punkscape.xyz/gallery27/scapes/latest';
const urlPSLatest = 'https://punkscape.xyz/gallery27/now/';
const interval = 60 * 1000; // milliseconds
var filePath = null;
let tray = null;
let mb = null;
var timer = null;
app.whenReady().then(() => {
initialize();
});
const initialize = function() {
if (!fs.existsSync(fileDir)) {
fs.mkdirSync(fileDir);
}
tray = new Tray(iconPath);
const contextMenu = Menu.buildFromTemplate([
{ id: 0, label: 'Live Updates', type: 'radio', click: itemClicked},
{ id: 1, label: 'Off', type: 'radio', click: itemClicked, checked: true},
{ type: 'separator'},
{ id: 2, label: 'View on punkscape.xyz', type: 'normal', click: itemClicked},
{ type: 'separator'},
{ id: 3, label: 'Quit', type: 'normal', click: itemClicked}
]);
tray.setContextMenu(contextMenu);
mb = menubar({tray});
mb.on('ready', () => {
console.log('ready');
tray.removeAllListeners();
});
};
const itemClicked = function(menuItem, browserWindow, event) {
console.log(menuItem['label'], menuItem['id']);
if (menuItem['id']==0) {
loadLatestScape();
clearInterval(timer);
timer = setInterval(loop, interval);
} else if (menuItem['id']==1) {
clearInterval(timer);
} else if (menuItem['id']==2) {
shell.openExternal(urlPSLatest);
} else if (menuItem['id']==3) {
app.quit();
}
}
const loop = function() {
console.log('Loop');
loadLatestScape();
}
const loadLatestScape = function() {
const request = net.request(urlG27);
request.on('response', (response) => {
console.log(`STATUS: ${response.statusCode}`);
response.on('data', (data) => {
let json = JSON.parse(data);
let imageURL = json['image'];
console.log(`Loading image from URL ${imageURL}`);
saveImage(imageURL, setBackground);
});
});
request.on('error', function(error) {
console.log('error: ' + error.message);
});
request.end();
};
const saveImage = function(imageURL, onDownload) {
let request = https.get(imageURL, (response) => {
removeAllFiles();
filePath = getNewFilePath();
let file = fs.createWriteStream(filePath);
response.pipe(file);
console.log(`Saved filed to ${filePath}.`);
file.on('finish', function() {
file.close();
console.log('Downloaded file.');
onDownload();
});
});
request.on('error', function(error) {
console.log('error: ' + error.message);
});
request.end();
};
const getNewFilePath = function() {
let d = new Date();
let time = d.getTime();
return path.join(fileDir, time + '.png');;
};
const removeAllFiles = function() {
console.log(`read directory ${fileDir}`);
files = fs.readdirSync(fileDir);
for (const file of files) {
fs.unlink(path.join(fileDir, file), err => {
if (err) throw err;
});
}
};
const setBackground = function() {
let script = "/usr/bin/osascript<<END\ntell application \"System Events\" to tell every desktop to set picture to \"" + filePath + "\"\nEND"
console.log(`Setting background from ${filePath}...`);
exec(script,
function (error, stdout, stderr) {
console.log('Set background.');
if (stdout) console.log('stdout: ' + stdout);
if (stderr) console.log('stderr: ' + stderr);
if (error !== null) {
console.log('exec error: ' + error);
}
});
};