import re


def youtube_duration_to_seconds(value):
    """Convert youtube (ISO 8601) duration to seconds.

    https://en.wikipedia.org/wiki/ISO_8601#Durations
    https://regex101.com/r/ALmmSS/1

    """
    iso8601 = r"P(?:(\d+)Y)?(?:(\d+)M)?(?:(\d+)W)?(?:(\d+)D)?T?(?:(\d+)H)?(?:(\d+)M)?(?:(\d+)S)?"
    match = re.match(iso8601, value)
    if match is None:
        return None

    group_names = ['years', 'months', 'weeks', 'days', 'hours', 'minutes', 'seconds']
    d = dict()
    for name, group in zip(group_names, match.groups(default=0)):
        d[name] = int(group)

    return int(
        d['years']*365*24*60*60 +
        d['months']*30*24*60*60 +
        d['weeks']*7*24*60*60 +
        d['days']*24*60*60 +
        d['hours']*60*60 +
        d['minutes']*60 +
        d['seconds']
    )