77 lines
2.9 KiB
JavaScript
77 lines
2.9 KiB
JavaScript
//import { notifyNewListing } from './telegramBot';
|
|
|
|
// import Listing from '../../server/db/models/Listing';
|
|
|
|
export default async function scrapeHabitaclia(puppeteer) {
|
|
console.log("Starting Habitaclia scraping process");
|
|
|
|
const url = 'https://idealista7.p.rapidapi.com/listhomes?order=relevance&operation=sale&locationId=0-EU-ES-28-07-001-079&locationName=Madrid&numPage=1&maxItems=40&location=es&locale=en';
|
|
const options = {
|
|
method: 'GET',
|
|
headers: {
|
|
'x-rapidapi-key': '080e9362e4mshdbcf5473f82adb9p196205jsne6a02c4e6381',
|
|
'x-rapidapi-host': 'idealista7.p.rapidapi.com'
|
|
}
|
|
};
|
|
|
|
try {
|
|
const response = await fetch(url, options);
|
|
const result = await response.text();
|
|
console.log(result);
|
|
} catch (error) {
|
|
console.error(error);
|
|
}
|
|
// const browser = await puppeteer.launch({
|
|
// headless: true,
|
|
// args: [
|
|
// '--no-sandbox' ,
|
|
// '--disable-setuid-sandbox'
|
|
// ]
|
|
// });
|
|
// const page = await browser.newPage();
|
|
|
|
// console.log(puppeteer, browser);
|
|
try {
|
|
// await page.goto('https://www.habitaclia.com/viviendas-granada_ciudad.htm', {
|
|
// waitUntil: 'networkidle2',
|
|
// });
|
|
// console.log('Navigated to Habitaclia');
|
|
// const content = await page.content();
|
|
// console.log(content);
|
|
// await page.screenshot({ path: 'habitaclia_state.png', fullPage: true })
|
|
// const listings = await page.evaluate(() => {
|
|
// const results = [];
|
|
// document.querySelectorAll('.list-item').forEach((element) => {
|
|
// const title = element.querySelector('.list-item-title')?.innerText.trim();
|
|
// const priceText = element.querySelector('.list-item-price')?.innerText.trim();
|
|
// const price = parseFloat(priceText.replace(/[^\d]/g, ''));
|
|
// const location = element.querySelector('.list-item-location')?.innerText.trim();
|
|
// const description = element.querySelector('.list-item-description')?.innerText.trim();
|
|
// const url = element.querySelector('.list-item-title a')?.href;
|
|
// if (title && price && location && description && url) {
|
|
// results.push({ title, price, location, description, url });
|
|
// }
|
|
// });
|
|
// return results;
|
|
// });
|
|
// console.log(`Found ${listings.length} listings`);
|
|
// for (const listingData of listings) {
|
|
// try {
|
|
// const listing = new Listing(listingData);
|
|
// //await listing.save();
|
|
// console.log(`Saved listing to database: ${listing.title}`);
|
|
// //await notifyNewListing(listing);
|
|
// console.log(`Sent notification for listing: ${listing.title}`);
|
|
// } catch (innerError) {
|
|
// console.error(`Error processing listing: ${innerError.message}`);
|
|
// }
|
|
// }
|
|
} catch (error) {
|
|
console.error("Error scraping Habitaclia:", error.message);
|
|
throw new Error("Failed to scrape Habitaclia");
|
|
} finally {
|
|
await browser.close();
|
|
console.log("Finished Habitaclia scraping process");
|
|
}
|
|
}
|