1"""Apify Actor to scrape TikTok video count from sound URLs."""
2
3from apify import Actor
4from playwright.async_api import async_playwright
5
6async def main() -> None:
7 async with Actor:
8 actor_input = await Actor.get_input() or {}
9 start_urls = actor_input.get('start_urls', [])
10
11 if not start_urls:
12 Actor.log.error('No URLs provided in start_urls!')
13 await Actor.exit()
14
15
16 target_url = start_urls[0].get('url') if start_urls else None
17 if not target_url:
18 Actor.log.error('First URL is invalid!')
19 await Actor.exit()
20
21 async with async_playwright() as playwright:
22 browser = await playwright.chromium.launch(
23 headless=Actor.config.headless,
24 args=[
25 '--disable-gpu',
26 '--user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36'
27 ],
28 )
29 context = await browser.new_context(viewport={'width': 1920, 'height': 1080})
30 page = None
31
32 try:
33 page = await context.new_page()
34 await page.goto(target_url, wait_until='domcontentloaded', timeout=60000)
35
36
37 video_count_element = await page.wait_for_selector(
38 'h2[data-e2e="music-video-count"] strong',
39 timeout=15000
40 )
41
42 video_count = await video_count_element.text_content()
43 cleaned_count = video_count.replace(' videos', '').strip()
44
45 await Actor.push_data({
46 'url': target_url,
47 'video_count': cleaned_count
48 })
49
50 except Exception as e:
51 await Actor.push_data({
52 'url': target_url,
53 'error': f'Failed to extract video count: {str(e)}'
54 })
55
56 finally:
57 if page:
58 await page.close()
59 await context.close()
60 await browser.close()
61 await Actor.exit()