MCPcopy Create free account
hub / github.com/apify/crawlee-python / _process_sitemap_item

Function _process_sitemap_item

src/crawlee/_utils/sitemap.py:229–284  ·  view source on GitHub ↗

Process a sitemap item and yield appropriate results.

(
    item: _SitemapItem,
    source: SitemapSource,
    depth: int,
    visited_sitemap_urls: set[str],
    sources: list[SitemapSource],
    *,
    emit_nested_sitemaps: bool,
    enqueue_strategy: EnqueueStrategy,
)

Source from the content-addressed store, hash-verified

227
228
229async def _process_sitemap_item(
230 item: _SitemapItem,
231 source: SitemapSource,
232 depth: int,
233 visited_sitemap_urls: set[str],
234 sources: list[SitemapSource],
235 *,
236 emit_nested_sitemaps: bool,
237 enqueue_strategy: EnqueueStrategy,
238) -> AsyncGenerator[SitemapUrl | NestedSitemap | None, None]:
239 """Process a sitemap item and yield appropriate results."""
240 item_copy = item.copy() # Work with a copy to avoid modifying the original
241
242 if 'type' not in item_copy:
243 return
244
245 item_type = item_copy.pop('type')
246
247 # Handle sitemap URL references (nested sitemaps)
248 if item_type == 'sitemap_url' and 'url' in item_copy:
249 sitemap_url = item_copy['url']
250 if sitemap_url and sitemap_url not in visited_sitemap_urls:
251 if parent_url := source.get('url'):
252 ok, reason = filter_url(target=sitemap_url, strategy=enqueue_strategy, origin=parent_url)
253 if not ok:
254 logger.warning(f'Skipping nested sitemap {sitemap_url!r} (parent {parent_url!r}): {reason}.')
255 return
256
257 # Add to processing queue
258 sources.append(SitemapSource(type='url', url=sitemap_url, depth=depth + 1))
259
260 # Output the nested sitemap reference if requested
261 if emit_nested_sitemaps:
262 yield NestedSitemap(loc=sitemap_url, origin_sitemap_url=parent_url)
263
264 # Handle individual URL entries
265 elif item_type == 'url' and 'loc' in item_copy:
266 # Determine the origin sitemap URL for tracking purposes
267 origin_url = _get_origin_url(source)
268
269 loc = item_copy['loc']
270 parent_url = source.get('url')
271 if parent_url and loc:
272 ok, reason = filter_url(target=loc, strategy=enqueue_strategy, origin=parent_url)
273 if not ok:
274 logger.warning(f'Skipping sitemap URL {loc!r} (parent {parent_url!r}): {reason}.')
275 return
276
277 # Create and yield the sitemap URL object
278 yield SitemapUrl(
279 loc=loc,
280 lastmod=item_copy.get('lastmod'),
281 changefreq=item_copy.get('changefreq'),
282 priority=item_copy.get('priority'),
283 origin_sitemap_url=origin_url,
284 )
285
286

Callers 2

_process_raw_sourceFunction · 0.85

Calls 5

filter_urlFunction · 0.90
SitemapSourceClass · 0.85
NestedSitemapClass · 0.85
_get_origin_urlFunction · 0.85
SitemapUrlClass · 0.85

Tested by

no test coverage detected