YouTube Id Parser & Playlist Handler functions

Thought I'd post some sample code in case others run into issues trying to parse the YouTube id from numerous url formats, or with parsing the YouTube video details from a playlist url.

from re import findall

DEFAULT_HEADERS = {“User-Agent”: “Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:25.0) Gecko/20100101 Firefox/25.0”,
                   “Accept”:     “application/json”}
TYPE_VIDEO      = 100000
TYPE_PLAYLIST   = 100001

def id_from_url(url):
    “”"Supply a YoutTube url and retrieve it’s Id. Supported formats are as follows:

(random tags will automatically be removed)

    Playlist URL Types:
    -------------------
        https://www.youtube.com/p/19B0B5CCFEF1C0C9?hl=en_US
        https://www.youtube.com/playlist?list=PL19B0B5CCFEF1C0C9
        https://www.youtube.com/view_play_list?p=19B0B5CCFEF1C0C9
        https://www.youtube.com/watch?v=1XP6Rpy7KCw&list=19B0B5CCFEF1C0C9

    Video URL Types:
    ----------------
        https://www.youtube.com/v/XGSy3_Czz8k
        https://www.youtube.com/e/XGSy3_Czz8k
        https://www.youtube.com/embed/XGSy3_Czz8k
        https://www.youtube.com/watch?v=XGSy3_Czz8k
        https://www.youtube.com/get_video_info?video_id=XGSy3_Czz8k
    “”"

    regexs = [(“PL”, “p=”, “&”), (“PL”, “list=”, “&”),
              (“PL”, “p/”, “?”), ("", “v/”, “?”),
              ("", “e/”, “?”),   ("", “embed/”, “?”),
              ("", “v=”, “&”),   ("", “video_id=”, “&”)]
    for regex in regexs:
        y_id  = None
        media = TYPE_VIDEO
        if regex[1] in url:
            if url.endswith("/"):
                url = url[:-1]
            y_id = findall("%s(.*?)%s" % regex[1:], url + regex[2])[0]
            if y_id.startswith(“PL”):
                y_id = y_id[2:]
            if regex[0] == “PL”:
                media = TYPE_PLAYLIST
            y_id = (regex[0] + y_id, media)
            break
    return y_id

def videos_from_playlist(y_id, indexed=True, plex=False, fallback_icon="", headers=DEFAULT_HEADERS):
    “”“Supply a YouTube playlist id, return dictionary of playlist video data or Plex ObjectContainer
    
    y_id:                   The YouTube playlist id
    indexed:                If True (default), the index position of the video
                            will be inserted at the beginning of the title
                            (eg: “(1/5) My Title”) If False, return “My Title”
    plex:                   If False (default), function will return a dictionary
                            of video data. Otherwise supply a valid Plex ObjectContainer
                            and the video data will be added into VideoClipObject’s and
                            appended to your ObjectContainer
    fallback_icon:          If you are supplying an ObjectContainer then you MUST specify
                            a fallback icon in the event that there are none available
    “””
    
    if plex:
        playlist = JSON.ObjectFromURL(“https://gdata.youtube.com/feeds/api/playlists/%s?v=2.1&alt=jsonc” % (y_id), headers=DEFAULT_HEADERS)
    else:
        from urllib2 import Request, urlopen
        from json import load
        c = urlopen(Request(“https://gdata.youtube.com/feeds/api/playlists/%s?v=2.1&alt=jsonc” % (y_id), headers=DEFAULT_HEADERS))
        playlist = load©
        c.close()

    play_items =
    for index, item in enumerate(playlist[“data”][“items”]):
        title   = item[“video”][“title”]
        url     = “http://www.youtube.com/watch?v=%s&list=%s” % (item[“video”][“id”], y_id),
        summary = item[“video”][“description”]
        date    = item[“video”][“uploaded”]
        if indexed:
            title = “(%s/%s) %s” % (index+1, len(playlist[“data”][“items”]), title)
        try:
            # test if hq thumb exists, if not use default quality… yuck…
            if plex:
                HTTP.Request(item[“video”][“thumbnail”][“hqDefault”])
            else:
                urlopen(item[“video”][“thumbnail”][“hqDefault”]).close()
            thumb = item[“video”][“thumbnail”][“hqDefault”]
        except Exception, error:
            if “404” in str(error):
                thumb = item[“video”][“thumbnail”][“sqDefault”]
        if plex:
            plex.add(VideoClipObject(
                title   = title,
                url     = url,
                thumb   = Resource.ContentsOfURLWithFallback(url=thumb, fallback=fallback_icon),
                summary = summary,
                originally_available_at = Datetime.ParseDate(date).date()
                ))
            
        else:
            play_items.append({“title”:   title,
                               “url”:     url,
                               “thumb”:   thumb,
                               “summary”: summary,
                               “originally_available_at”: date})
    if plex:
        return plex
    return play_items

y_id, y_type = id_from_url(“https://www.youtube.com/playlist?list=PL19B0B5CCFEF1C0C9”)
print videos_from_playlist(y_id, plex=False)

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.