HoroScope / src /services /horoscopeService.js
ntphuc149's picture
Update src/services/horoscopeService.js
a89840b verified
raw
history blame
4.95 kB
const GITHUB_BASE_URL =
"https://raw.githubusercontent.com/preview4r/AstroLens-Horoscope-Data-Storage/main";
const getCurrentDate = () => {
const today = new Date();
return today.toISOString().split("T")[0];
};
const decodeBase64UTF8 = (base64String) => {
try {
const binaryString = atob(base64String);
const bytes = new Uint8Array(binaryString.length);
for (let i = 0; i < binaryString.length; i++) {
bytes[i] = binaryString.charCodeAt(i);
}
const decoder = new TextDecoder("utf-8");
return decoder.decode(bytes);
} catch (error) {
console.error("Error decoding base64 UTF-8:", error);
return null;
}
};
export const fetchHoroscopeData = async (category, period) => {
try {
const currentDate = getCurrentDate();
const fileName = `${category}_${period}.json`;
const url = `https://api.github.com/repos/preview4r/AstroLens-Horoscope-Data-Storage/contents/data/${currentDate}/${fileName}`;
const headers = {
Accept: "application/vnd.github.v3+json",
};
if (window?.huggingface?.variables?.REACT_APP_GITHUB_TOKEN) {
headers["Authorization"] = `token ${window?.huggingface?.variables?.REACT_APP_GITHUB_TOKEN}`;
}
console.log("Fetching from URL:", url);
console.log(
"Using token:",
window?.huggingface?.variables?.REACT_APP_GITHUB_TOKEN ? "Yes" : "No"
);
const response = await fetch(url, { headers });
console.log("Response status:", response.status);
if (!response.ok) {
throw new Error(
`GitHub API error: ${response.status} - ${response.statusText}`
);
}
const githubResponse = await response.json();
if (githubResponse.content) {
const cleanBase64 = githubResponse.content.replace(/\n/g, "");
const decodedContent = decodeBase64UTF8(cleanBase64);
if (!decodedContent) {
throw new Error("Failed to decode base64 content");
}
const data = JSON.parse(decodedContent);
console.log("Successfully loaded data:", data);
return data;
} else {
throw new Error("No content in GitHub response");
}
} catch (error) {
console.error("Error fetching horoscope data:", error);
return null;
}
};
export const fallbackHoroscopeContent = {
overall: {
today:
"Today brings a harmonious blend of opportunities and challenges across all aspects of your life. Your natural instincts will guide you well.",
week: "This week promises balanced growth in multiple areas. Trust your intuition as you navigate new experiences.",
month:
"A month of significant personal development awaits. Embrace change and new perspectives with confidence.",
year: "This year marks a transformative period of growth, bringing profound insights and meaningful achievements.",
},
love: {
today:
"Love is in the air today! Your romantic side is highlighted, making it a perfect day for expressing your feelings.",
week: "This week brings romantic opportunities and deepening connections with your loved ones.",
month:
"A month of passionate encounters and meaningful relationships awaits you.",
year: "This year will be transformative for your love life, bringing lasting happiness.",
},
health: {
today:
"Your energy levels are high today. Focus on maintaining balance between work and rest.",
week: "Pay attention to your physical wellbeing this week. Small changes can make big differences.",
month:
"This month emphasizes the importance of mental and physical health harmony.",
year: "A year of wellness and vitality lies ahead. Make health your priority.",
},
relationship: {
today:
"Relationships flourish under today's cosmic energy. Communication is key to success.",
week: "Strengthen your bonds with family and friends through meaningful conversations.",
month:
"Social connections expand this month, bringing new friendships and opportunities.",
year: "Your social circle will grow significantly, enriching your life in unexpected ways.",
},
finance: {
today:
"Financial opportunities present themselves today. Stay alert for new possibilities.",
week: "This week favors careful financial planning and smart investments.",
month:
"Money matters require attention this month. Budget wisely for future security.",
year: "Financial growth and stability are highlighted throughout this year.",
},
fortune: {
today:
"Lucky energies surround you today. Trust your instincts and take calculated risks for unexpected rewards.",
week: "Fortune favors the bold this week. Positive surprises and serendipitous encounters await.",
month:
"A month of abundance and good fortune unfolds. Opportunities align perfectly with your goals.",
year: "This year brings remarkable luck and prosperity. Your efforts will be rewarded beyond expectations.",
},
};