diff --git a/app/Utils/UpdateGithubReadme.ts b/app/Utils/UpdateGithubReadme.ts index 111dc19..fde53ec 100644 --- a/app/Utils/UpdateGithubReadme.ts +++ b/app/Utils/UpdateGithubReadme.ts @@ -8,35 +8,97 @@ import { fetchWeeklyStatistics, } from 'App/Utils/StatsUtils' import { Stats } from 'App/Types/IStats' +import { getCurrentPlayingFromCache } from 'App/Utils/SongUtils' +import { GithubReason, GithubRequest } from 'App/Types/IGithub' -export async function UpdateGithubReadme(): Promise { +export async function updateGithubReadmeStats(): Promise { const daily_stats = await fetchDailyStatistics() const weekly_stats = await fetchWeeklyStatistics() const monthly_stats = await fetchMonthlyStatistics() const total_stats = await fetchStatistics() - const response = await axios.get<{ content: string; sha: string }>(`https://api.github.com/repos/${Env.get('GITHUB_USERNAME')}/${Env.get('GITHUB_USERNAME')}/readme`, { - headers: { - authorization: `Bearer ${Env.get('GITHUB_TOKEN')}`, - }, - }) + const readme = await getReadmeContent() - if (response.status === 200) { - const content = Buffer.from(response.data.content, 'base64').toString() - const old_table = content.split('')[1].split('')[0] - const new_table = ` + if (readme === null) + return + + const old_table = readme.content.split('')[1].split('')[0] + const new_table = ` | Title | Daily | Weekly | Monthly | Total | | :------------------------------------------ | ----------: | ----------: | -----------: | -----------: | | :hourglass_flowing_sand: Hours Spent Coding | **${getTotalHours(daily_stats)}** | **${getTotalHours(weekly_stats)}** | **${getTotalHours(monthly_stats)}** | **${getTotalHours(total_stats)}** | | :computer: Terminal Commands | **${daily_stats.commands_ran}** | **${weekly_stats.commands_ran}** | **${monthly_stats.commands_ran}** | **${total_stats.commands_ran}** | | :hammer: Docker Builds | **${daily_stats.builds_ran}** | **${weekly_stats.builds_ran}** | **${monthly_stats.builds_ran}** | **${total_stats.builds_ran}** |\n` - const new_content = content.replace(old_table, new_table) - const update = await axios.put(`https://api.github.com/repos/${Env.get('GITHUB_USERNAME')}/${Env.get('GITHUB_USERNAME')}/contents/README.md`, + await updateReadmeContent( + { + content: readme.content.replace(old_table, new_table), + sha: readme.sha, + }, + { + reason: '📊 Updated Statistics - Athena', + error: 'Error while updating statistics', + }) +} + +export async function updateGithubReadmeSpotify(): Promise { + const current_song = await getCurrentPlayingFromCache() + + const readme = await getReadmeContent() + + if (readme === null) + return + + let old_song + let new_song + + if (current_song.is_playing) { + old_song = readme.content.split('')[1].split('')[0] + new_song = ` +Spotify Cover Image\n + +${current_song.name} by ${current_song.author}
+Listening from ${current_song.device_type}\n` + } + else { + old_song = readme.content.split('')[1].split('')[0] + new_song = '\nCurrently not listening to anything with Spotify\n' + } + + if (readme.content.replace(old_song, new_song) !== readme.content) { + await updateReadmeContent( { - message: '📊 Updated Statistics - Athena', - content: Buffer.from(new_content, 'utf8').toString('base64'), - sha: response.data.sha, + content: readme.content.replace(old_song, new_song), + sha: readme.sha, + }, + { + reason: '🎵 Updated Song - Athena', + error: 'Error while updating song', + }) + } +} + +async function getReadmeContent(): Promise { + const response = await axios.get(`https://api.github.com/repos/${Env.get('GITHUB_USERNAME')}/${Env.get('GITHUB_USERNAME')}/readme`, { + headers: { + authorization: `Bearer ${Env.get('GITHUB_TOKEN')}`, + }, + }) + return response.status === 200 + ? { + content: Buffer.from(response.data.content, 'base64').toString(), + sha: response.data.sha, + } + : null +} + +async function updateReadmeContent(new_readme: GithubRequest, reason: GithubReason): Promise { + try { + await axios.put(`https://api.github.com/repos/${Env.get('GITHUB_USERNAME')}/${Env.get('GITHUB_USERNAME')}/contents/README.md`, + { + message: reason.reason, + content: Buffer.from(new_readme.content, 'utf8').toString('base64'), + sha: new_readme.sha, author: { name: 'Athena - API Automation', email: 'api@arthurdanjou.fr', @@ -47,8 +109,9 @@ export async function UpdateGithubReadme(): Promise { authorization: `Bearer ${Env.get('GITHUB_TOKEN')}`, }, }) - if (update.status !== 200) - Logger.error('Error while updating statistics') + } + catch (error) { + Logger.error(reason.error) } }