MCPcopy Create free account
hub / github.com/nlweb-ai/NLWeb / extract_duration

Function extract_duration

AskAgent/python/data_loading/rss2schema.py:68–117  ·  view source on GitHub ↗

Extract and normalize duration from various formats. Args: duration_str: Duration string from feed Returns: Normalized duration string or None

(duration_str: str)

Source from the content-addressed store, hash-verified

66 return url
67
68def extract_duration(duration_str: str) -> str | None:
69 """
70 Extract and normalize duration from various formats.
71
72 Args:
73 duration_str: Duration string from feed
74
75 Returns:
76 Normalized duration string or None
77 """
78 if not duration_str:
79 return None
80
81 duration_str = duration_str.strip()
82
83 # If already in ISO format like PT1H30M15S
84 if duration_str.startswith('PT'):
85 return duration_str
86
87 # Handle common formats
88
89 # Format: HH:MM:SS
90 if re.match(r'^\d+:\d+:\d+$', duration_str):
91 hours, minutes, seconds = map(int, duration_str.split(':'))
92 return f"PT{hours}H{minutes}M{seconds}S"
93
94 # Format: MM:SS
95 if re.match(r'^\d+:\d+$', duration_str):
96 minutes, seconds = map(int, duration_str.split(':'))
97 return f"PT{minutes}M{seconds}S"
98
99 # Format: seconds (as number)
100 if re.match(r'^\d+$', duration_str):
101 seconds = int(duration_str)
102 hours = seconds // 3600
103 minutes = (seconds % 3600) // 60
104 seconds = seconds % 60
105
106 result = "PT"
107 if hours > 0:
108 result += f"{hours}H"
109 if minutes > 0:
110 result += f"{minutes}M"
111 if seconds > 0 or (hours == 0 and minutes == 0):
112 result += f"{seconds}S"
113
114 return result
115
116 # Return as-is if we can't parse it
117 return duration_str
118
119def extract_guid(item: ET.Element) -> str | None:
120 """

Callers 1

parse_rss_2_0Function · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected