From 18ec7a6ac5d350e587eed3147f5eea98e230f29a Mon Sep 17 00:00:00 2001 From: Victor Azevedo <34415964+victrme@users.noreply.github.com> Date: Sun, 27 Oct 2024 06:13:23 +0100 Subject: [PATCH] Fetch 10-day and today weather.com together --- src/providers/weathercom.ts | 47 +++++++++++++++++++------------------ 1 file changed, 24 insertions(+), 23 deletions(-) diff --git a/src/providers/weathercom.ts b/src/providers/weathercom.ts index e0d4035..1ca3e01 100644 --- a/src/providers/weathercom.ts +++ b/src/providers/weathercom.ts @@ -20,30 +20,23 @@ function transformToJson(html: string): unknown { return { meta: { - url: 'https://accuweather.com' + encodeURI($('.header-city-link').attr('href') ?? ''), + url: 'https://weather.com' + encodeURI($('.header-city-link').attr('href') ?? ''), }, now: { - icon: $('.cur-con-weather-card .weather-icon')?.attr('data-src') ?? '', + icon: $('.CurrentConditions--wxIcon--BOjPq')?.attr('skycode') ?? '', temp: $('.CurrentConditions--tempValue--zUBSz')?.text(), - feels: $('.cur-con-weather-card .real-feel')?.text(), - description: $('.cur-con-weather-card .phrase')?.text(), + feels: $('.TodayDetailsCard--feelsLikeTempValue--8WgHV')?.text(), + description: $('.CurrentConditions--phraseValue---VS-k')?.text(), }, sun: { - rise: $('.sunrise-sunset__times-value:nth(0)')?.text(), - set: $('.sunrise-sunset__times-value:nth(1)')?.text(), + rise: $('.TwcSunChart--sunriseDateItem--Os-KL')?.text(), + set: $('.TwcSunChart--sunsetDateItem--y9wq2')?.text(), }, - hourly: new Array(12).fill('').map((_, i) => ({ - time: $(`.hourly-list__list__item-time:nth(${i})`)?.text(), - temp: $(`.hourly-list__list__item-temp:nth(${i})`)?.text(), - rain: $(`.hourly-list__list__item-precip:nth(${i})`)?.text(), - })), daily: new Array(10).fill('').map((_, i) => ({ - time: $(`.daily-list-item:nth(${i}) .date p:last-child`)?.text(), - high: $(`.daily-list-item:nth(${i}) .temp-hi`)?.text(), - low: $(`.daily-list-item:nth(${i}) .temp-lo`)?.text(), - day: $(`.daily-list-item:nth(${i}) .phrase p:first-child`)?.text(), - night: $(`.daily-list-item:nth(${i}) .phrase p:last-child`)?.text(), - rain: $(`.daily-list-item:nth(${i}) .precip`)?.text(), + description: $(`.DailyForecast--CardContent--y6e2w .DailyContent--narrative--jqi6P:nth(${i})`)?.text(), + high: $(`.DailyForecast--CardContent--y6e2w .DetailsSummary--highTempValue--VHKaO:nth(${i})`)?.text(), + low: $(`.DailyForecast--CardContent--y6e2w .DetailsSummary--lowTempValue--ogrzb:nth(${i})`)?.text(), + rain: $(`.DailyForecast--CardContent--y6e2w .DetailsSummary--precip--YXw9t:nth(${i})`)?.text(), })), } } @@ -78,14 +71,22 @@ async function fetchPageContent(params: QueryParams): Promise { const data = Object.values(json.dal.getSunV3LocationSearchUrlConfig)[0].data const placeId = data.location.placeId[0] - const firefoxAndroid = 'Mozilla/5.0 (Android 14; Mobile; rv:109.0) Gecko/124.0 Firefox/124.0' - const path = `https://weather.com/weather/today/l/${placeId}?unit=${params.unit}` - const htmlResp = await fetch(path, { headers: { 'User-Agent': firefoxAndroid } }) - let html = await htmlResp.text() + const headers = { + 'User-Agent': 'Mozilla/5.0 (Android 14; Mobile; rv:109.0) Gecko/124.0 Firefox/124.0', + } + + const responses = await Promise.all([ + fetch(`https://weather.com/${params.lang}/weather/today/l/${placeId}?unit=${params.unit}`, { headers }), + fetch(`https://weather.com/${params.lang}/weather/tenday/l/${placeId}?unit=${params.unit}`, { headers }), + ]) + + let today = await responses[0].text() + let tenday = await responses[1].text() - html = html.slice(html.indexOf(''), html.indexOf('')) + today = today.slice(today.indexOf(''), today.indexOf('')) + tenday = tenday.slice(tenday.indexOf(''), tenday.indexOf('')) - return html + return today + tenday } interface WeatherComLocationSearch {