Spaces:
Running
Running
Update src/services/horoscopeService.js
Browse files- src/services/horoscopeService.js +46 -32
src/services/horoscopeService.js
CHANGED
|
@@ -23,46 +23,60 @@ const decodeBase64UTF8 = (base64String) => {
|
|
| 23 |
}
|
| 24 |
};
|
| 25 |
|
| 26 |
-
// Thay đổi cách lấy token cho Static Space
|
| 27 |
export const fetchHoroscopeData = async (category, period) => {
|
| 28 |
try {
|
| 29 |
const currentDate = getCurrentDate();
|
| 30 |
const fileName = `${category}_${period}.json`;
|
| 31 |
-
const
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
|
|
|
|
|
|
| 39 |
}
|
| 40 |
-
|
| 41 |
-
console.log(
|
| 42 |
-
console.log(
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
method: 'GET'
|
| 52 |
-
});
|
| 53 |
-
|
| 54 |
-
console.log('Response status:', response.status);
|
| 55 |
-
|
| 56 |
if (!response.ok) {
|
| 57 |
-
throw new Error(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
}
|
| 59 |
-
|
| 60 |
-
const data = await response.json();
|
| 61 |
-
console.log('Successfully loaded data from raw URL:', data);
|
| 62 |
-
return data;
|
| 63 |
-
|
| 64 |
} catch (error) {
|
| 65 |
-
console.error(
|
| 66 |
return null;
|
| 67 |
}
|
| 68 |
};
|
|
|
|
| 23 |
}
|
| 24 |
};
|
| 25 |
|
|
|
|
| 26 |
export const fetchHoroscopeData = async (category, period) => {
|
| 27 |
try {
|
| 28 |
const currentDate = getCurrentDate();
|
| 29 |
const fileName = `${category}_${period}.json`;
|
| 30 |
+
const url = `https://api.github.com/repos/ntphuc149/AstroLens-Horoscope-Data-Storage/contents/data/${currentDate}/${fileName}`;
|
| 31 |
+
|
| 32 |
+
// Chuẩn bị headers với token
|
| 33 |
+
const headers = {
|
| 34 |
+
Accept: "application/vnd.github.v3+json",
|
| 35 |
+
};
|
| 36 |
+
|
| 37 |
+
// Thêm Authorization header nếu có token
|
| 38 |
+
if (process.env.REACT_APP_GITHUB_TOKEN) {
|
| 39 |
+
headers["Authorization"] = `token ${process.env.REACT_APP_GITHUB_TOKEN}`;
|
| 40 |
}
|
| 41 |
+
|
| 42 |
+
console.log("Fetching from URL:", url);
|
| 43 |
+
console.log(
|
| 44 |
+
"Using token:",
|
| 45 |
+
process.env.REACT_APP_GITHUB_TOKEN ? "Yes" : "No"
|
| 46 |
+
);
|
| 47 |
+
|
| 48 |
+
const response = await fetch(url, { headers });
|
| 49 |
+
|
| 50 |
+
console.log("Response status:", response.status);
|
| 51 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
if (!response.ok) {
|
| 53 |
+
throw new Error(
|
| 54 |
+
`GitHub API error: ${response.status} - ${response.statusText}`
|
| 55 |
+
);
|
| 56 |
+
}
|
| 57 |
+
|
| 58 |
+
const githubResponse = await response.json();
|
| 59 |
+
|
| 60 |
+
// GitHub API trả về content dạng base64, cần decode UTF-8 đúng cách
|
| 61 |
+
if (githubResponse.content) {
|
| 62 |
+
// Loại bỏ line breaks trong base64 content
|
| 63 |
+
const cleanBase64 = githubResponse.content.replace(/\n/g, "");
|
| 64 |
+
|
| 65 |
+
// Decode base64 với UTF-8 support
|
| 66 |
+
const decodedContent = decodeBase64UTF8(cleanBase64);
|
| 67 |
+
|
| 68 |
+
if (!decodedContent) {
|
| 69 |
+
throw new Error("Failed to decode base64 content");
|
| 70 |
+
}
|
| 71 |
+
|
| 72 |
+
const data = JSON.parse(decodedContent);
|
| 73 |
+
console.log("Successfully loaded data:", data);
|
| 74 |
+
return data;
|
| 75 |
+
} else {
|
| 76 |
+
throw new Error("No content in GitHub response");
|
| 77 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
} catch (error) {
|
| 79 |
+
console.error("Error fetching horoscope data:", error);
|
| 80 |
return null;
|
| 81 |
}
|
| 82 |
};
|