fix(HandelsRegister): Refined HandelsRegister functionality for better error handling and response capture.
This commit is contained in:
		| @@ -1,5 +1,12 @@ | |||||||
| # Changelog | # Changelog | ||||||
|  |  | ||||||
|  | ## 2025-01-03 - 1.3.1 - fix(HandelsRegister) | ||||||
|  | Refined HandelsRegister functionality for better error handling and response capture. | ||||||
|  |  | ||||||
|  | - Improved parsing logic in parseGermanRegistration function. | ||||||
|  | - Enhanced navigateToPage and clickFindButton methods with error messages for clarity. | ||||||
|  | - Implemented a new responseListener to handle and log HTTP responses correctly. | ||||||
|  |  | ||||||
| ## 2025-01-03 - 1.3.0 - feat(core) | ## 2025-01-03 - 1.3.0 - feat(core) | ||||||
| Enhanced data handling capabilities and improved company search functionalities. | Enhanced data handling capabilities and improved company search functionalities. | ||||||
|  |  | ||||||
|   | |||||||
| @@ -3,6 +3,6 @@ | |||||||
|  */ |  */ | ||||||
| export const commitinfo = { | export const commitinfo = { | ||||||
|   name: '@fin.cx/opendata', |   name: '@fin.cx/opendata', | ||||||
|   version: '1.3.0', |   version: '1.3.1', | ||||||
|   description: 'A TypeScript library for accessing, managing, and updating open business data, focused on German companies and integrating with MongoDB.' |   description: 'A TypeScript library for accessing, managing, and updating open business data, focused on German companies and integrating with MongoDB.' | ||||||
| } | } | ||||||
|   | |||||||
| @@ -114,7 +114,8 @@ export class HandelsRegister { | |||||||
|   private async parseGermanRegistration( |   private async parseGermanRegistration( | ||||||
|     input: string |     input: string | ||||||
|   ): Promise<BusinessRecord['data']['germanParsedRegistration']> { |   ): Promise<BusinessRecord['data']['germanParsedRegistration']> { | ||||||
|     const regex = /District court (\p{L}[\p{L}\s-]*?(?:\s*\([\p{L}\s-]+\))?)\s+(HRA|HRB|GnR|VR|PR|GsR)\s+(\d+)/u; |     const regex = | ||||||
|  |       /District court (\p{L}[\p{L}\s-]*?(?:\s*\([\p{L}\s-]+\))?)\s+(HRA|HRB|GnR|VR|PR|GsR)\s+(\d+)/u; | ||||||
|     const match = input.match(regex); |     const match = input.match(regex); | ||||||
|  |  | ||||||
|     if (match) { |     if (match) { | ||||||
| @@ -193,39 +194,101 @@ export class HandelsRegister { | |||||||
|     await page.waitForSelector('#form\\:schlagwoerter', { timeout: 5000 }); |     await page.waitForSelector('#form\\:schlagwoerter', { timeout: 5000 }); | ||||||
|  |  | ||||||
|     // 1) Type of Register: |     // 1) Type of Register: | ||||||
| // Open the dropdown to reveal options |     // Open the dropdown to reveal options | ||||||
| await page.waitForSelector('#form\\:registerArt_label'); |     await page.waitForSelector('#form\\:registerArt_label'); | ||||||
| await page.click('#form\\:registerArt_label'); // Open the dropdown |     await page.click('#form\\:registerArt_label'); // Open the dropdown | ||||||
|  |  | ||||||
| // Wait for the options and select the one matching companyArg.type |     // Wait for the options and select the one matching companyArg.type | ||||||
| await page.waitForSelector('#form\\:registerArt_items'); // Ensure dropdown options are loaded |     await page.waitForSelector('#form\\:registerArt_items'); // Ensure dropdown options are loaded | ||||||
| await page.evaluate((type) => { |     await page.evaluate((type) => { | ||||||
|   const options = Array.from(document.querySelectorAll('#form\\:registerArt_items li')); |       const options = Array.from(document.querySelectorAll('#form\\:registerArt_items li')); | ||||||
|   const targetOption = options.find((option) => option.textContent?.trim() === type); // Match type dynamically |       const targetOption = options.find((option) => option.textContent?.trim() === type); // Match type dynamically | ||||||
|   (targetOption as any)?.click(); |       (targetOption as any)?.click(); | ||||||
| }, companyArg.type); // Pass companyArg.type to the browser context |     }, companyArg.type); // Pass companyArg.type to the browser context | ||||||
|  |  | ||||||
| // 2) Register number: |     // 2) Register number: | ||||||
| // Fill in the register number |     // Fill in the register number | ||||||
| await page.waitForSelector('#form\\:registerNummer'); |     await page.waitForSelector('#form\\:registerNummer'); | ||||||
| await page.type('#form\\:registerNummer', companyArg.number); |     await page.type('#form\\:registerNummer', companyArg.number); | ||||||
|  |  | ||||||
| // 3) Register court: |     // 3) Register court: | ||||||
| // Open the dropdown for the register court |     // Open the dropdown for the register court | ||||||
| await page.waitForSelector('#form\\:registergericht_label'); |     await page.waitForSelector('#form\\:registergericht_label'); | ||||||
| await page.click('#form\\:registergericht_label'); // Open the dropdown |     await page.click('#form\\:registergericht_label'); // Open the dropdown | ||||||
|  |  | ||||||
| // Wait for the options and select the one matching companyArg.court |     // Wait for the options and select the one matching companyArg.court | ||||||
| await page.waitForSelector('#form\\:registergericht_items'); // Ensure dropdown options are loaded |     await page.waitForSelector('#form\\:registergericht_items'); // Ensure dropdown options are loaded | ||||||
| await page.evaluate((court) => { |     await page.evaluate((court) => { | ||||||
|   const options = Array.from(document.querySelectorAll('#form\\:registergericht_items li')); |       const options = Array.from(document.querySelectorAll('#form\\:registergericht_items li')); | ||||||
|   const targetOption = options.find((option) => option.textContent?.trim() === court); // Match court dynamically |       const targetOption = options.find((option) => option.textContent?.trim() === court); // Match court dynamically | ||||||
|   (targetOption as any)?.click(); |       (targetOption as any)?.click(); | ||||||
| }, companyArg.court); // Pass companyArg.court to the browser context |     }, companyArg.court); // Pass companyArg.court to the browser context | ||||||
|  |  | ||||||
|     await this.clickFindButton(page); |     await this.clickFindButton(page); | ||||||
|  |  | ||||||
|     const businessRecords = await this.waitForResults(page); |     const businessRecords = await this.waitForResults(page); | ||||||
|     console.log(businessRecords); |     console.log(businessRecords); | ||||||
|  |  | ||||||
|  |     // Define the response listener | ||||||
|  |     const responseListener = async ( | ||||||
|  |       response: plugins.smartbrowser.smartpuppeteer.puppeteer.HTTPResponse | ||||||
|  |     ) => { | ||||||
|  |       // Ignore preflight (OPTIONS) requests | ||||||
|  |       if (response.request().method() === 'OPTIONS') { | ||||||
|  |         console.log(`Ignoring preflight request: ${response.url()}`); | ||||||
|  |         return; | ||||||
|  |       } | ||||||
|  |  | ||||||
|  |       // Check for downloads (Content-Disposition header) | ||||||
|  |       const contentDisposition = response.headers()['content-disposition']; | ||||||
|  |  | ||||||
|  |       if (contentDisposition && contentDisposition.includes('attachment')) { | ||||||
|  |         console.log(`Download detected: ${response.url()}`); | ||||||
|  |         try { | ||||||
|  |           const buffer = await response.buffer(); | ||||||
|  |           console.log(`Downloaded file size: ${buffer.length} bytes`); | ||||||
|  |         } catch (error) { | ||||||
|  |           console.error('Error downloading file:', error); | ||||||
|  |         } | ||||||
|  |       } | ||||||
|  |     }; | ||||||
|  |     page.on('response', responseListener); | ||||||
|  |  | ||||||
|  |     // Click the element | ||||||
|  |     await page.evaluate(() => { | ||||||
|  |       // Locate the table body | ||||||
|  |       const tableBody = document.querySelector( | ||||||
|  |         '#ergebnissForm\\:selectedSuchErgebnisFormTable_data' | ||||||
|  |       ); | ||||||
|  |       if (!tableBody) { | ||||||
|  |         throw new Error('Table body not found'); | ||||||
|  |       } | ||||||
|  |  | ||||||
|  |       // Locate the first row | ||||||
|  |       const firstRow = tableBody.querySelector('tr:nth-child(1)'); | ||||||
|  |       if (!firstRow) { | ||||||
|  |         throw new Error('First row not found'); | ||||||
|  |       } | ||||||
|  |  | ||||||
|  |       // Locate the last cell in the first row | ||||||
|  |       const lastCell = firstRow.querySelector('td:last-child'); | ||||||
|  |       if (!lastCell) { | ||||||
|  |         throw new Error('Last cell not found in the first row'); | ||||||
|  |       } | ||||||
|  |  | ||||||
|  |       // Locate the last <a> element in the last cell | ||||||
|  |       const lastLink = lastCell.querySelector('a:last-of-type'); | ||||||
|  |       if (!lastLink) { | ||||||
|  |         throw new Error('Last link not found in the last cell'); | ||||||
|  |       } | ||||||
|  |  | ||||||
|  |       // Simulate a click on the last <a> element | ||||||
|  |       (lastLink as HTMLElement).click(); | ||||||
|  |     }); | ||||||
|  |  | ||||||
|  |     // Optional: Wait for some response or navigation triggered by the click | ||||||
|  |     await page.waitForTimeout(10000); | ||||||
|  |  | ||||||
|  |     page.off('response', responseListener); | ||||||
|   } |   } | ||||||
| } | } | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user