-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
92 lines (81 loc) · 1.99 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
81
82
83
84
85
86
87
88
89
90
91
92
const { rwClient, octokit } = require("./twitterClient.js");
const RaySo = require("rayso.js");
const CronJob = require("cron").CronJob;
const fetch = require("node-fetch");
let lastDate = null;
let commit_message = "";
const commit = async () => {
try {
console.log("Finding new commits....");
const result = await octokit.request("GET /repos/{owner}/{repo}/commits", {
owner: "zvizzct",
repo: "Codewars",
});
const { data } = result;
processData(data[0]);
} catch (e) {
console.error(e);
}
};
const getData = async (url) => {
const response = await fetch(url);
return response.json();
};
const processData = async (data) => {
const { commit, url } = data;
const { message } = commit;
commit_message = message;
const { files } = await getData(url);
const { raw_url } = files[0];
let newDate = new Date(commit.committer.date);
if (checkDate(newDate)) {
lastDate = new Date(newDate);
console.log("Commit found!");
getCode(raw_url);
}
};
const getCode = async (raw_url) => {
fetch(raw_url).then(function (response) {
response.text().then(function (text) {
storedText = text;
codeToImg(storedText);
});
});
};
const checkDate = (newDate) => {
if (newDate > lastDate) return true;
console.log("No updates found");
return false;
};
const codeToImg = async (code) => {
const raySo = new RaySo({
title: commit_message,
padding: 32,
language: "javascript",
localPreview: true,
browserPath: "/usr/bin/google-chrome-stable",
});
raySo
.cook(code)
.then((response) => {
tweet();
})
.catch((err) => {
console.error(err);
});
};
const tweet = async () => {
try {
const mediaId = await rwClient.v1.uploadMedia("example.png");
await rwClient.v1.tweet(
`${commit_message} #100DaysOfCode #Codewars #javascript`,
{ media_ids: mediaId }
);
} catch (e) {
console.error(e);
}
};
const job = new CronJob("0 */12 * * *", () => {
commit();
});
job.start();