From 195638bdb044f4f97251788ad6890e2fd3ccc384 Mon Sep 17 00:00:00 2001 From: Itai Klapholtz Date: Thu, 1 Feb 2024 13:09:15 +0200 Subject: [PATCH] WIP --- main.js | 2 - main.ts | 90 ------------------------------------------ search-console.js | 1 + utils/notifications.js | 2 +- 4 files changed, 2 insertions(+), 93 deletions(-) delete mode 100644 main.ts diff --git a/main.js b/main.js index 402b136..9865a03 100644 --- a/main.js +++ b/main.js @@ -11,8 +11,6 @@ const STRATEGY = 'MOBILE'; const senderEmail = 'EXAMPLE@email.com'; const recipients = [senderEmail, senderEmail]; -const googleSheetUrl = 'https://...'; -const mailSubject = 'Lighthouse Scores'; function fetchLighthouseMetrics(url) { try { diff --git a/main.ts b/main.ts deleted file mode 100644 index d00282b..0000000 --- a/main.ts +++ /dev/null @@ -1,90 +0,0 @@ -const subject: string = 'Lighthouse Scores'; -const pages: string[] = [ - 'http://www.yahoo.com' -]; - -async function serializeObject(obj: Record): Promise { - const str: string[] = []; - for (const p in obj) { - if (obj.hasOwnProperty(p)) { - str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p].toString())); - } - } - return str.join("&"); -} - -async function fetchLighthouseMetrics(url: string): Promise<{ - seoScore?: number; - accessibilityScore?: number; - performanceScore?: number; - bestPracticesScore?: number; -}> { - try { - // Define the strategy here - const STRATEGY = "MOBILE"; - - // Make the API request - const response = await fetch(`https://www.googleapis.com/pagespeedonline/v5/runPagespeed?url=${encodeURIComponent(url)}&strategy=${STRATEGY}&category=BEST_PRACTICES&category=ACCESSIBILITY&category=SEO&category=PERFORMANCE`); - - if (!response.ok) { - throw new Error(`HTTP error! Status: ${response.status}`); - } - - const data = await response.json(); - - const lighthouseMetrics: { - seoScore?: number; - accessibilityScore?: number; - performanceScore?: number; - bestPracticesScore?: number; - } = { - seoScore: data.lighthouseResult.categories.seo?.score, - accessibilityScore: data.lighthouseResult.categories.accessibility?.score, - performanceScore: data.lighthouseResult.categories.performance?.score, - bestPracticesScore: data.lighthouseResult.categories['best-practices']?.score, - }; - - console.log(`For url ${url}, metrics: ${JSON.stringify(lighthouseMetrics)}`); - - return lighthouseMetrics; - } catch (error) { - console.error('Error on fetchLighthouseMetrics: ' + error.toString()); - throw error; - } -} - -function appendToExcel(url: string, row: { - seoScore?: number; - accessibilityScore?: number; - performanceScore?: number; - bestPracticesScore?: number; -}) { - const todayDate: string = new Date().toJSON().slice(0, 10).replace(/-/g, '/'); - const sheet = SpreadsheetApp.openByUrl(url).getSheetByName('Sheet1'); - const data: any[] = [todayDate, row.seoScore, row.accessibilityScore, row.performanceScore, row.bestPracticesScore]; - sheet.appendRow(data); -} - -async function main() { - const results: Record = {}; - - for (const p of pages) { - const res = await fetchLighthouseMetrics(p); - results[p] = res; - } - - let mailBody: string = `Strategy: ${STRATEGY}\nEnv: Production\n`; - Object.entries(results).forEach(([lp, res]) => { - mailBody += `\n\n${lp}: ${JSON.stringify(res)}`; - }); - - recipients.forEach(recipient => sendEmail(recipient, subject, mailBody)); -} - -// Call the main function to start the execution -main(); diff --git a/search-console.js b/search-console.js index f01ff0a..3f6e02a 100644 --- a/search-console.js +++ b/search-console.js @@ -1,2 +1,3 @@ import { sendEmail } from './utils'; +// TODO \ No newline at end of file diff --git a/utils/notifications.js b/utils/notifications.js index c348ba1..7845928 100644 --- a/utils/notifications.js +++ b/utils/notifications.js @@ -12,7 +12,7 @@ export function sendEmail(recipient, subject, body) { } } -function sendTelegramMessage(message) { +export function sendTelegramMessage(message) { const text = encodeURIComponent(message); const url = `${TELEGRAM_API}?chat_id=${CHAT_ID}&text=${text}`; const response = UrlFetchApp.fetch(url, { muteHttpExceptions: true });