################################################################################
# Copyright (c) 2021 JagTheFriend
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
################################################################################

import os
import re
from datetime import timedelta
from googleapiclient.discovery import build

api_key = os.environ.get('yt_token')

youtube = build('youtube', 'v3', developerKey=api_key)

hours_pattern = re.compile(r'(\d+)H')
minutes_pattern = re.compile(r'(\d+)M')
seconds_pattern = re.compile(r'(\d+)S')


def main(playlist_id: str) -> dict:
    """
    This function returns the time it would take to
    watch an entire playlist, from the start to end
    :param playlist_id: This a unique id given to each playlist
    :return: Dictionary
    """
    total_seconds = 0
    next_page_token = None
    while True:
        pl_request = youtube.playlistItems().list(
            part='contentDetails',
            playlistId=playlist_id,
            maxResults=50,
            pageToken=next_page_token
        )

        try:
            pl_response = pl_request.execute()
        except Exception:
            return {"output": f'Invalid id'}

        vid_ids = []
        for item in pl_response['items']:
            vid_ids.append(item['contentDetails']['videoId'])

        vid_request = youtube.videos().list(
            part="contentDetails",
            id=','.join(vid_ids)
        )

        vid_response = vid_request.execute()

        for item in vid_response['items']:
            duration = item['contentDetails']['duration']

            hours = hours_pattern.search(duration)
            minutes = minutes_pattern.search(duration)
            seconds = seconds_pattern.search(duration)

            hours = int(hours.group(1)) if hours else 0
            minutes = int(minutes.group(1)) if minutes else 0
            seconds = int(seconds.group(1)) if seconds else 0

            video_seconds = timedelta(
                hours=hours,
                minutes=minutes,
                seconds=seconds
            ).total_seconds()

            total_seconds += video_seconds

        next_page_token = pl_response.get('nextPageToken')

        if not next_page_token:
            break

    total_seconds = int(total_seconds)

    minutes, seconds = divmod(total_seconds, 60)
    hours, minutes = divmod(minutes, 60)
    return {"output": f'{hours} hour(s):{minutes} minute(s):{seconds} second(s)'}

bd4c1c5f826f09eac1a2c165464cbbdd36cc5c8e
<mask token>

<mask token>


def main(playlist_id: str) ->dict:
    """
    This function returns the time it would take to
    watch an entire playlist, from the start to end
    :param playlist_id: This a unique id given to each playlist
    :return: Dictionary
    """
    total_seconds = 0
    next_page_token = None
    while True:
        pl_request = youtube.playlistItems().list(part='contentDetails',
            playlistId=playlist_id, maxResults=50, pageToken=next_page_token)
        try:
            pl_response = pl_request.execute()
        except Exception:
            return {'output': f'Invalid id'}
        vid_ids = []
        for item in pl_response['items']:
            vid_ids.append(item['contentDetails']['videoId'])
        vid_request = youtube.videos().list(part='contentDetails', id=','.
            join(vid_ids))
        vid_response = vid_request.execute()
        for item in vid_response['items']:
            duration = item['contentDetails']['duration']
            hours = hours_pattern.search(duration)
            minutes = minutes_pattern.search(duration)
            seconds = seconds_pattern.search(duration)
            hours = int(hours.group(1)) if hours else 0
            minutes = int(minutes.group(1)) if minutes else 0
            seconds = int(seconds.group(1)) if seconds else 0
            video_seconds = timedelta(hours=hours, minutes=minutes, seconds
                =seconds).total_seconds()
            total_seconds += video_seconds
        next_page_token = pl_response.get('nextPageToken')
        if not next_page_token:
            break
    total_seconds = int(total_seconds)
    minutes, seconds = divmod(total_seconds, 60)
    hours, minutes = divmod(minutes, 60)
    return {'output':
        f'{hours} hour(s):{minutes} minute(s):{seconds} second(s)'}

<mask token>
api_key = os.environ.get('yt_token')
youtube = build('youtube', 'v3', developerKey=api_key)
hours_pattern = re.compile('(\\d+)H')
minutes_pattern = re.compile('(\\d+)M')
seconds_pattern = re.compile('(\\d+)S')


def main(playlist_id: str) ->dict:
    """
    This function returns the time it would take to
    watch an entire playlist, from the start to end
    :param playlist_id: This a unique id given to each playlist
    :return: Dictionary
    """
    total_seconds = 0
    next_page_token = None
    while True:
        pl_request = youtube.playlistItems().list(part='contentDetails',
            playlistId=playlist_id, maxResults=50, pageToken=next_page_token)
        try:
            pl_response = pl_request.execute()
        except Exception:
            return {'output': f'Invalid id'}
        vid_ids = []
        for item in pl_response['items']:
            vid_ids.append(item['contentDetails']['videoId'])
        vid_request = youtube.videos().list(part='contentDetails', id=','.
            join(vid_ids))
        vid_response = vid_request.execute()
        for item in vid_response['items']:
            duration = item['contentDetails']['duration']
            hours = hours_pattern.search(duration)
            minutes = minutes_pattern.search(duration)
            seconds = seconds_pattern.search(duration)
            hours = int(hours.group(1)) if hours else 0
            minutes = int(minutes.group(1)) if minutes else 0
            seconds = int(seconds.group(1)) if seconds else 0
            video_seconds = timedelta(hours=hours, minutes=minutes, seconds
                =seconds).total_seconds()
            total_seconds += video_seconds
        next_page_token = pl_response.get('nextPageToken')
        if not next_page_token:
            break
    total_seconds = int(total_seconds)
    minutes, seconds = divmod(total_seconds, 60)
    hours, minutes = divmod(minutes, 60)
    return {'output':
        f'{hours} hour(s):{minutes} minute(s):{seconds} second(s)'}

import os
import re
from datetime import timedelta
from googleapiclient.discovery import build
api_key = os.environ.get('yt_token')
youtube = build('youtube', 'v3', developerKey=api_key)
hours_pattern = re.compile('(\\d+)H')
minutes_pattern = re.compile('(\\d+)M')
seconds_pattern = re.compile('(\\d+)S')


def main(playlist_id: str) ->dict:
    """
    This function returns the time it would take to
    watch an entire playlist, from the start to end
    :param playlist_id: This a unique id given to each playlist
    :return: Dictionary
    """
    total_seconds = 0
    next_page_token = None
    while True:
        pl_request = youtube.playlistItems().list(part='contentDetails',
            playlistId=playlist_id, maxResults=50, pageToken=next_page_token)
        try:
            pl_response = pl_request.execute()
        except Exception:
            return {'output': f'Invalid id'}
        vid_ids = []
        for item in pl_response['items']:
            vid_ids.append(item['contentDetails']['videoId'])
        vid_request = youtube.videos().list(part='contentDetails', id=','.
            join(vid_ids))
        vid_response = vid_request.execute()
        for item in vid_response['items']:
            duration = item['contentDetails']['duration']
            hours = hours_pattern.search(duration)
            minutes = minutes_pattern.search(duration)
            seconds = seconds_pattern.search(duration)
            hours = int(hours.group(1)) if hours else 0
            minutes = int(minutes.group(1)) if minutes else 0
            seconds = int(seconds.group(1)) if seconds else 0
            video_seconds = timedelta(hours=hours, minutes=minutes, seconds
                =seconds).total_seconds()
            total_seconds += video_seconds
        next_page_token = pl_response.get('nextPageToken')
        if not next_page_token:
            break
    total_seconds = int(total_seconds)
    minutes, seconds = divmod(total_seconds, 60)
    hours, minutes = divmod(minutes, 60)
    return {'output':
        f'{hours} hour(s):{minutes} minute(s):{seconds} second(s)'}

################################################################################
# Copyright (c) 2021 JagTheFriend
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
################################################################################

import os
import re
from datetime import timedelta
from googleapiclient.discovery import build

api_key = os.environ.get('yt_token')

youtube = build('youtube', 'v3', developerKey=api_key)

hours_pattern = re.compile(r'(\d+)H')
minutes_pattern = re.compile(r'(\d+)M')
seconds_pattern = re.compile(r'(\d+)S')


def main(playlist_id: str) -> dict:
    """
    This function returns the time it would take to
    watch an entire playlist, from the start to end
    :param playlist_id: This a unique id given to each playlist
    :return: Dictionary
    """
    total_seconds = 0
    next_page_token = None
    while True:
        pl_request = youtube.playlistItems().list(
            part='contentDetails',
            playlistId=playlist_id,
            maxResults=50,
            pageToken=next_page_token
        )

        try:
            pl_response = pl_request.execute()
        except Exception:
            return {"output": f'Invalid id'}

        vid_ids = []
        for item in pl_response['items']:
            vid_ids.append(item['contentDetails']['videoId'])

        vid_request = youtube.videos().list(
            part="contentDetails",
            id=','.join(vid_ids)
        )

        vid_response = vid_request.execute()

        for item in vid_response['items']:
            duration = item['contentDetails']['duration']

            hours = hours_pattern.search(duration)
            minutes = minutes_pattern.search(duration)
            seconds = seconds_pattern.search(duration)

            hours = int(hours.group(1)) if hours else 0
            minutes = int(minutes.group(1)) if minutes else 0
            seconds = int(seconds.group(1)) if seconds else 0

            video_seconds = timedelta(
                hours=hours,
                minutes=minutes,
                seconds=seconds
            ).total_seconds()

            total_seconds += video_seconds

        next_page_token = pl_response.get('nextPageToken')

        if not next_page_token:
            break

    total_seconds = int(total_seconds)

    minutes, seconds = divmod(total_seconds, 60)
    hours, minutes = divmod(minutes, 60)
    return {"output": f'{hours} hour(s):{minutes} minute(s):{seconds} second(s)'}
