Skip to content

Commit

Permalink
adding weather
Browse files Browse the repository at this point in the history
  • Loading branch information
t0mCS committed Aug 15, 2024
1 parent 3f49d09 commit 0d81502
Show file tree
Hide file tree
Showing 8 changed files with 696 additions and 277 deletions.
103 changes: 103 additions & 0 deletions src/main/Scrapers/Google/weather.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
const {
customConsoleLog,
wait,
waitForElement,
bigStepper,
} = require('../../preloadFunctions');
const { ipcRenderer } = require('electron');

async function exportWeather(company, name, runID) {
await wait(2);

const weatherData = {
current: {},
forecast: [],
};

bigStepper(runID);

// Extract current weather data
const currentWeather = document.querySelector('#wob_wc');
if (currentWeather) {
weatherData.current = {
temperature: currentWeather.querySelector('#wob_tm').textContent + '°C',
condition: currentWeather.querySelector('#wob_dc').textContent,
precipitation: currentWeather.querySelector('#wob_pp').textContent,
humidity: currentWeather.querySelector('#wob_hm').textContent,
wind: currentWeather.querySelector('#wob_ws').textContent,
};
}

// Extract forecast data
const forecastElements = document.querySelectorAll('.wob_df');
forecastElements.forEach((element) => {
const day = element.querySelector('.Z1VzSb').getAttribute('aria-label');
const maxTemp = element.querySelector('.gNCp2e .wob_t').textContent + '°C';
const minTemp = element.querySelector('.QrNVmd .wob_t').textContent + '°C';
const condition = element.querySelector('img').getAttribute('alt');

weatherData.forecast.push({
day,
maxTemp,
minTemp,
condition,
});
});

customConsoleLog(
'Weather data collected:',
JSON.stringify(weatherData, null, 2),
);

bigStepper(runID);
ipcRenderer.send('handle-export', company, name, weatherData, runID);

return;
}

async function navigateAndSearch() {
const searchInput = await waitForElement('input[name="q"]', 'Search input');
searchInput.value = 'my current weather';
searchInput.form.submit();
await wait(2);
}

async function extractWeatherData(weatherData) {
const temperatureElement = await waitForElement('#wob_tm', 'Temperature');
const locationElement = await waitForElement('#wob_loc', 'Location');
const conditionElement = await waitForElement('#wob_dc', 'Condition');

weatherData.temperature = temperatureElement
? temperatureElement.textContent + '°C'
: 'N/A';
weatherData.location = locationElement ? locationElement.textContent : 'N/A';
weatherData.condition = conditionElement
? conditionElement.textContent
: 'N/A';

// Extract forecast
const forecastElements = await waitForElement(
'.wob_df',
'Forecast days',
true,
);
weatherData.forecast = [];

if (forecastElements && forecastElements.length > 0) {
for (const element of forecastElements) {
const day = element.querySelector('.wob_d').textContent;
const maxTemp = element.querySelector('.wob_t').children[0].textContent;
const minTemp = element.querySelector('.wob_t').children[2].textContent;
const condition = element.querySelector('img').getAttribute('alt');

weatherData.forecast.push({
day,
maxTemp: maxTemp + '°C',
minTemp: minTemp + '°C',
condition,
});
}
}
}

module.exports = exportWeather;
52 changes: 52 additions & 0 deletions src/main/Scrapers/Google/weather.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# Google Weather Scraper

This module is responsible for scraping weather data from Google search results.

## Functionality

The `exportWeather()` function in the `weather.js` file performs the following tasks:

1. Navigates to Google and searches for "my current weather".
2. Extracts current weather information (temperature, location, condition).
3. Extracts weather forecast for the next few days.
4. Stores the collected data in an object.
5. Returns the collected weather data.

## Implementation

The weather export functionality is implemented in the `weather.js` file and integrated into the main application through `preloadWebview.js`. Here's an overview of the implementation:

1. The `exportWeather()` function is defined in `weather.js` and exported as a module.
2. In `preloadWebview.js`, the `exportWeather` function is imported and called when the 'export-website' event is received for Google Weather.
3. The export process is initiated through an IPC message from the main process.
4. After the export is complete, the collected weather data is sent back to the main process using the 'handle-export' IPC message.

## Data Extraction

The module extracts the following information:

1. Current Weather:

- Temperature
- Location
- Weather condition

2. Forecast (for each day):
- Day
- Maximum temperature
- Minimum temperature
- Weather condition

## Platform-specific Considerations

1. Element Selection: The module uses specific CSS selectors to target weather information elements, which may need updating if Google's HTML structure changes.
2. Location Accuracy: The "my current weather" search relies on Google's ability to determine the user's location, which may not always be accurate.
3. Units: The current implementation assumes temperatures are in Celsius. This may need to be adjusted based on the user's location or preferences.

## Future Improvements

1. **User Location Input**: Allow users to input a specific location for weather information.
2. **Unit Conversion**: Add the ability to switch between Celsius and Fahrenheit.
3. **Extended Forecast**: Implement functionality to extract a longer-term forecast if available.
4. **Error Handling**: Improve the module's robustness by adding more thorough error handling and recovery mechanisms.
5. **Additional Weather Data**: Extract more detailed weather information such as humidity, wind speed, and precipitation probability if available.
Loading

0 comments on commit 0d81502

Please sign in to comment.