-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfetch.js
More file actions
179 lines (155 loc) · 4.59 KB
/
Copy pathfetch.js
File metadata and controls
179 lines (155 loc) · 4.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
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
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();
}
// ── Football: fetch next Real Madrid scheduled match ──────────────────────────
const FOOTBALL_API_TOKEN = "8565ad2223864f5fa20dac2795660f83";
const FOOTBALL_TEAM_ID = 86;
console.log("Fetching next Real Madrid match from football-data.org...");
const footballOptions = {
hostname: "api.football-data.org",
path: `/v4/teams/${FOOTBALL_TEAM_ID}/matches?status=SCHEDULED`,
port: 443,
method: "GET",
headers: {
"X-Auth-Token": FOOTBALL_API_TOKEN
}
};
const footballReq = https.request(footballOptions, res => {
let footballData = "";
console.log(`football-data.org statusCode: ${res.statusCode}`);
res.on("data", d => {
footballData += d;
});
res.on("end", () => {
try {
const parsed = JSON.parse(footballData);
const matches = parsed.matches || [];
const nextMatch = matches.length > 0 ? matches[0] : null;
const output = JSON.stringify({ nextMatch });
fs.writeFile("./public/nextMatch.json", output, function (err) {
if (err) return console.log(err);
console.log("saved file to public/nextMatch.json");
});
} catch (e) {
console.log("Failed to parse football data:", e.message);
}
});
});
footballReq.on("error", error => {
console.log("Football API request failed:", error.message);
});
footballReq.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();
}