-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.mjs
More file actions
59 lines (51 loc) · 1.62 KB
/
main.mjs
File metadata and controls
59 lines (51 loc) · 1.62 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
import fetch from 'node-fetch';
import { Octokit } from "@octokit/rest";
const octokit = new Octokit({
auth: process.env.GH_TOKEN,
});
const apiKey = process.env.NASA_TOKEN;
fetch(`https://api.nasa.gov/EPIC/api/natural?api_key=${apiKey}`)
.then((res) => res.json())
.then(async(d) => {
const year = d[0].identifier.slice(0, 4);
const month = d[0].identifier.slice(4, 6);
const day = d[0].identifier.slice(6, 8);
const image = `https://api.nasa.gov/EPIC/archive/natural/${year}/${month}/${day}/png/${d[0].image}.png?api_key=${apiKey}`
const readme = await octokit.rest.repos.getReadme({
owner: "tinvv",
repo: "Earth",
});
const content = Buffer.from(
readme.data.content,
"base64"
).toString();
const data = content.split("\n");
const start_line = data.indexOf("<!-- Earth Image Update -->");
const end_line = data.indexOf("<!-- /Earth Image Update -->");
if (start_line === -1 && end_line === -1) {
console.log("Error: Could not find start/end line syntax");
}
data.splice(
start_line + 1,
end_line - start_line - 1,
`# Updated at
${day}/${month}/${year} <br>
# Info
${d[0].caption} <br>
 `
)
const modify = data.join("\n");
octokit.rest.repos.createOrUpdateFileContents({
owner: "mulforma",
repo: "Earth",
path: "README.md",
branch: "main",
message: "Update README.md",
sha: readme.data.sha,
content: Buffer.from(modify).toString("base64"),
committer: {
name: "Tin's bot",
},
});
})