-
Notifications
You must be signed in to change notification settings - Fork 3
/
fetch.js
executable file
·130 lines (116 loc) · 3.02 KB
/
fetch.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
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
fs = require("fs");
const https = require("https");
process = require("process");
require("dotenv").config();
const GITHUB_TOKEN = process.env.REACT_APP_GITHUB_TOKEN;
const GITHUB_USERNAME = process.env.GITHUB_USERNAME;
const USE_GITHUB_DATA = process.env.USE_GITHUB_DATA;
const MEDIUM_USERNAME = process.env.MEDIUM_USERNAME;
const ERR = {
noUserName:
"Github Username was found to be undefined. Please set all relevant environment variables.",
requestFailed:
"The request to GitHub didn't succeed. Check if GitHub token in your .env file is correct.",
requestFailedMedium:
"The request to Medium didn't succeed. Check if Medium username in your .env file is correct."
};
if (USE_GITHUB_DATA === "true") {
if (GITHUB_USERNAME === undefined) {
throw new Error(ERR.noUserName);
}
console.log(`Fetching profile data for ${GITHUB_USERNAME}`);
var data = JSON.stringify({
query: `
{
user(login:"${GITHUB_USERNAME}") {
name
bio
avatarUrl
location
pinnedItems(first: 6, types: [REPOSITORY]) {
totalCount
edges {
node {
... on Repository {
name
description
forkCount
stargazers {
totalCount
}
url
id
diskUsage
primaryLanguage {
name
color
}
}
}
}
}
}
}
`
});
const default_options = {
hostname: "api.github.com",
path: "/graphql",
port: 443,
method: "POST",
headers: {
Authorization: `Bearer ${GITHUB_TOKEN}`,
"User-Agent": "Node"
}
};
const req = https.request(default_options, res => {
let data = "";
console.log(`statusCode: ${res.statusCode}`);
if (res.statusCode !== 200) {
throw new Error(ERR.requestFailed);
}
res.on("data", d => {
data += d;
});
res.on("end", () => {
fs.writeFile("./public/profile.json", data, function (err) {
if (err) return console.log(err);
console.log("saved file to public/profile.json");
});
});
});
req.on("error", error => {
throw error;
});
req.write(data);
req.end();
}
if (MEDIUM_USERNAME !== undefined) {
console.log(`Fetching Medium blogs data for ${MEDIUM_USERNAME}`);
const options = {
hostname: "api.rss2json.com",
path: `/v1/api.json?rss_url=https://medium.com/feed/@${MEDIUM_USERNAME}`,
port: 443,
method: "GET"
};
const req = https.request(options, res => {
let mediumData = "";
console.log(`statusCode: ${res.statusCode}`);
if (res.statusCode !== 200) {
throw new Error(ERR.requestMediumFailed);
}
res.on("data", d => {
mediumData += d;
});
res.on("end", () => {
fs.writeFile("./public/blogs.json", mediumData, function (err) {
if (err) return console.log(err);
console.log("saved file to public/blogs.json");
});
});
});
req.on("error", error => {
throw error;
});
req.end();
}