Skip to content

Commit 7a27d7c

Browse files
committed
Add music URL extraction functionality to x402_agent_demo
- Introduced extract_music_url function to parse and retrieve SoundCloud links from HTML/text content. - Updated main function to attempt music URL extraction from payment result body, displaying it if found. - Enhanced overall readability and maintainability of the code by organizing new functionality logically within the existing structure.
1 parent 7889ffa commit 7a27d7c

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

examples/x402_agent_demo.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,25 @@ def decode_receipt(header_value: str) -> Dict[str, Any]:
133133
return payload.model_dump()
134134

135135

136+
def extract_music_url(content: str) -> Optional[str]:
137+
"""Extract SoundCloud (or other) music link from HTML/text."""
138+
if not content:
139+
return None
140+
cleaned = html.unescape(content)
141+
# Look for SoundCloud embed first
142+
match = re.search(r"https?://w\.soundcloud\.com/player/\?[^\s\"'<>]+", cleaned)
143+
if match:
144+
return match.group(0)
145+
match = re.search(r"https?://soundcloud\.com/[^\s\"'<>]+", cleaned)
146+
if match:
147+
return match.group(0)
148+
# Fallback: first http(s) link
149+
match = re.search(r"https?://[^\s\"'<>]+", cleaned)
150+
if match:
151+
return match.group(0)
152+
return None
153+
154+
136155
def parse_tool_output(raw: str) -> Any:
137156
segment: Optional[str] = None
138157
if "Output:" in raw:
@@ -294,13 +313,21 @@ async def main() -> None:
294313
http_probe_result = extract_tool_payload(messages, "http_probe")
295314
payment_result = extract_tool_payload(messages, "x402_paywalled_request")
296315
assistant_summary = extract_last_assistant(messages)
316+
music_url: Optional[str] = None
297317

298318
if not assistant_summary and payment_result:
299319
body = payment_result.get("body")
300320
if isinstance(body, dict):
301321
assistant_summary = summarise_text(json.dumps(body, ensure_ascii=False))
302322
elif isinstance(body, str):
303323
assistant_summary = summarise_text(body)
324+
music_url = extract_music_url(body)
325+
326+
# Attempt music URL extraction even if assistant summary already exists.
327+
if music_url is None and payment_result:
328+
body = payment_result.get("body")
329+
if isinstance(body, str):
330+
music_url = extract_music_url(body)
304331

305332
if http_probe_result:
306333
preview_body = http_probe_result.get("body")
@@ -328,6 +355,9 @@ async def main() -> None:
328355
if assistant_summary:
329356
rprint("\n[bold green]Agent Final Summary[/]")
330357
rprint(assistant_summary)
358+
if music_url:
359+
rprint("\n[bold green]Music URL[/]")
360+
rprint(music_url)
331361

332362
if payment_header:
333363
rprint("\n[bold blue]Signed X-PAYMENT Header[/]")

0 commit comments

Comments
 (0)