-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathoctodex.js
More file actions
73 lines (56 loc) · 1.59 KB
/
octodex.js
File metadata and controls
73 lines (56 loc) · 1.59 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
// -*- coding: utf-8 -*-
// 9KB
'use strict'
const https = require('https')
const fs = require('fs')
const path = require('path')
const __forEach = Array.prototype.forEach
var options = {
hostname: 'octodex.github.com',
port: 443,
path: '/',
method: 'GET'
}
function writeToFile(content) {
const newContent = `window.octodex=${content}`
const fullpath = path.resolve(__dirname, 'static', 'octodex-data.js')
fs.readFile(fullpath, (err, oldContent) => {
if (err ||
Buffer.compare(oldContent,Buffer.from(newContent)) === 0) {
// different: update the file
fs.writeFile(fullpath, newContent, () => {
console.log(`[Octodex] Has written to ${fullpath}.`);
})
} else {
// same: no changes
console.log('[Octodex] There is no changes. Dropped.')
}
})
}
const req = https.request(options, (res) => {
let chunks = ''
res.on('data', (chunk) => {
chunks += chunk
})
res.on('end', () => {
console.log('[Octodex] Getting from remote done.');
let pics = []
__forEach.call(chunks.match(/<img height="424" width="424".*?>/g), function(i) {
let img = i.match(/data-src="(.*?)" alt="(.*?)"/)
pics.push({
'f': img[1].split('/').pop(), // filename: xxx.png
't': img[2] // title (caption)
})
})
__forEach.call(chunks.match(/<p class="number">.*?<\/p>/g), function(n, index) {
let no = n.match(/(\d+)/)
pics[index].no = no[1]
})
writeToFile(JSON.stringify(pics))
})
})
console.log("[Octodex] Start...");
req.end()
req.on('error', (err) => {
console.error('[Octodex]', err)
})