Transform URL based on M3U profile settings
(original_url, m3u_profile)
| 360 | return None |
| 361 | |
| 362 | def _transform_url(original_url, m3u_profile): |
| 363 | """Transform URL based on M3U profile settings""" |
| 364 | try: |
| 365 | import regex |
| 366 | |
| 367 | if not original_url: |
| 368 | return None |
| 369 | |
| 370 | search_pattern = m3u_profile.search_pattern |
| 371 | replace_pattern = m3u_profile.replace_pattern |
| 372 | # Convert JS-style backreferences in replace: $<name> -> \g<name>, $1 -> \1 |
| 373 | safe_replace_pattern = regex.sub(r'\$<([^>]+)>', r'\\g<\1>', replace_pattern) |
| 374 | safe_replace_pattern = regex.sub(r'\$(\d+)', r'\\\1', safe_replace_pattern) |
| 375 | |
| 376 | if search_pattern and replace_pattern: |
| 377 | # regex module accepts JS-style (?<name>...) named groups natively |
| 378 | transformed_url = regex.sub(search_pattern, safe_replace_pattern, original_url) |
| 379 | return transformed_url |
| 380 | |
| 381 | return original_url |
| 382 | |
| 383 | except Exception as e: |
| 384 | logger.error(f"Error transforming URL: {e}") |
| 385 | return original_url |
| 386 | |
| 387 | @api_view(["GET"]) |
| 388 | @authentication_classes([JWTAuthentication, ApiKeyAuthentication, QueryParamJWTAuthentication]) |
no outgoing calls
no test coverage detected