-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithubSave.js
More file actions
67 lines (55 loc) · 1.7 KB
/
githubSave.js
File metadata and controls
67 lines (55 loc) · 1.7 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
const Github = require('github-api');
const path = require('path');
const request = require('request');
var lastData = "";
var github = new Github({
token: process.env.OAUTH_TOKEN || require('./config/githubOauth.js').token,
auth: "oauth"
});
function UpdateLast() {
request({
url: "https://raw.githubusercontent.com/tumblenet/tn-phone-website/master/_data/phones.json",
json: true,
followAllRedirects: true
}, function (error, response, body) {
if (!error && response.statusCode === 200) {
lastData = body;
}
});
}
var repo = github.getRepo("tumblenet", "tnphone.tumblenet.ga");
UpdateLast();
var options = {
author: {name: 'HerokuSave', email: 'api@tumblenet.ga'},
committer: {name: 'TN Phones', email: 'admin@tumblenet.ga'},
encode: true // Whether to base64 encode the file. (default: true)
}
function LoadPhones(cb) {
request({
url: "https://raw.githubusercontent.com/tumblenet/tnphone.tumblenet.ga/master/_data/phones.json",
json: true,
followAllRedirects: true
}, function (error, response, body) {
if (!error && response.statusCode === 200) {
cb(body);
}
});
}
function SavePhones(data, cb) {
UpdateLast();
var callback = cb || function (err) {
console.log(err);
}
if (JSON.stringify(lastData, null, '\t') == JSON.stringify(data, null, '\t')) {
callback("phones is already up to date")
} else {
repo.writeFile('master', '_data/phones.json', JSON.stringify(data, null, '\t'), 'Update Phones feed', options,
function(error,request,result) {
callback(error ? "There was an error in updateing phones data" : "phones data was updated");
})
}
}
module.exports = {
SavePhones:SavePhones,
LoadPhones:LoadPhones
};