ntphuc149 commited on
Commit
84687c0
·
verified ·
1 Parent(s): db78387

Update src/services/horoscopeService.js

Browse files
Files changed (1) hide show
  1. 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 rawUrl = `https://raw.githubusercontent.com/preview4r/AstroLens-Horoscope-Data-Storage/main/data/${currentDate}/${fileName}`;
32
-
33
- const headers = {};
34
-
35
- const githubToken = window?.huggingface?.variables?.REACT_APP_GITHUB_TOKEN;
36
-
37
- if (githubToken) {
38
- headers['Authorization'] = `token ${githubToken}`;
 
 
39
  }
40
-
41
- console.log('=== TOKEN DEBUG FOR STATIC SPACE ===');
42
- console.log('Huggingface object exists:', !!window.huggingface);
43
- console.log('Variables object exists:', !!window?.huggingface?.variables);
44
- console.log('Token exists:', !!githubToken);
45
- console.log('Token preview:', githubToken ? `${githubToken.substring(0, 8)}...` : 'None');
46
-
47
- console.log('Fetching from raw URL:', rawUrl);
48
-
49
- const response = await fetch(rawUrl, {
50
- headers,
51
- method: 'GET'
52
- });
53
-
54
- console.log('Response status:', response.status);
55
-
56
  if (!response.ok) {
57
- throw new Error(`Failed to fetch: ${response.status} - ${response.statusText}`);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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('Error fetching horoscope data:', 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
  };