# Buggy code
def _real_extract(self, url):
    video_id = self._match_id(url)
    webpage = self._download_webpage(url, video_id)
    matches = re.search(r"data-(prog|clip)id=\'([0-9]+)\'", webpage)
    if matches:
        video_type, video_id = matches.groups()
        if video_type == "prog":
            video_type = "program"
    else:
        senate_isvp_url = SenateISVPIE._search_iframe_url(webpage)
        if senate_isvp_url:
            title = self._og_search_title(webpage)
            surl = smuggle_url(senate_isvp_url, {"force_title": title})
            return self.url_result(surl, "SenateISVP", video_id, title)

    def get_text_attr(d, attr):
        return d.get(attr, {}).get("#text")

    data = self._download_json(
        "http://www.c-span.org/assets/player/ajax-player.php?os=android&html5=%s&id=%s"
        % (video_type, video_id),
        video_id,
    )["video"]
    if data["@status"] != "Success":
        raise ExtractorError(
            "%s said: %s" % (self.IE_NAME, get_text_attr(data, "error")), expected=True
        )

    doc = self._download_xml(
        "http://www.c-span.org/common/services/flashXml.php?%sid=%s"
        % (video_type, video_id),
        video_id,
    )

    description = self._html_search_meta("description", webpage)

    title = find_xpath_attr(doc, ".//string", "name", "title").text
    thumbnail = find_xpath_attr(doc, ".//string", "name", "poster").text

    files = data["files"]
    capfile = get_text_attr(data, "capfile")

    entries = []
    for partnum, f in enumerate(files):
        formats = []
        for quality in f["qualities"]:
            formats.append(
                {
                    "format_id": "%s-%sp"
                    % (
                        get_text_attr(quality, "bitrate"),
                        get_text_attr(quality, "height"),
                    ),
                    "url": unescapeHTML(get_text_attr(quality, "file")),
                    "height": int_or_none(get_text_attr(quality, "height")),
                    "tbr": int_or_none(get_text_attr(quality, "bitrate")),
                }
            )
        self._sort_formats(formats)
        entries.append(
            {
                "id": "%s_%d" % (video_id, partnum + 1),
                "title": (
                    title if len(files) == 1 else "%s part %d" % (title, partnum + 1)
                ),
                "formats": formats,
                "description": description,
                "thumbnail": thumbnail,
                "duration": int_or_none(get_text_attr(f, "length")),
                "subtitles": {
                    "en": [{"url": capfile, "ext": determine_ext(capfile, "dfxp")}],
                }
                if capfile
                else None,
            }
        )

    if len(entries) == 1:
        entry = dict(entries[0])
        entry["id"] = "c" + video_id if video_type == "clip" else video_id
        return entry
    else:
        return {
            "_type": "playlist",
            "entries": entries,
            "title": title,
            "id": "c" + video_id if video_type == "clip" else video_id,
        }


# Traceback (UnboundLocalError)
~ $ youtube-dl -v http://www.c-span.org/video/?319979-1/european-security-ukraine
[debug] System config: []
[debug] User config: []
[debug] Command-line args: ['-v', 'http://www.c-span.org/video/?319979-1/european-security-ukraine']
[debug] Encodings: locale UTF-8, fs utf-8, out UTF-8, pref UTF-8
[debug] youtube-dl version 2015.12.23
[debug] Python version 3.2.3 - Linux-3.13.0-68-generic-i686-with-LinuxMint-13-maya
[debug] exe versions: ffmpeg 2.8.3-dpkg, ffprobe 2.8.3-dpkg, rtmpdump 2.4
[debug] Proxy map: {}
[CSpan] 319979: Downloading webpage
Traceback (most recent call last):
File "/usr/local/bin/youtube-dl", line 11, in <module>
sys.exit(main())
File "/usr/local/lib/python3.2/dist-packages/youtube_dl/__init__.py", line 410, in main
_real_main(argv)
File "/usr/local/lib/python3.2/dist-packages/youtube_dl/__init__.py", line 400, in _real_main
retcode = ydl.download(all_urls)
File "/usr/local/lib/python3.2/dist-packages/youtube_dl/YoutubeDL.py", line 1677, in download
url, force_generic_extractor=self.params.get('force_generic_extractor', False))
File "/usr/local/lib/python3.2/dist-packages/youtube_dl/YoutubeDL.py", line 665, in extract_info
ie_result = ie.extract(url)
File "/usr/local/lib/python3.2/dist-packages/youtube_dl/extractor/common.py", line 291, in extract
return self._real_extract(url)
File "/usr/local/lib/python3.2/dist-packages/youtube_dl/extractor/cspan.py", line 78, in _real_extract
'http://www.c-span.org/assets/player/ajax-player.php?os=android&amp;html5=%s&amp;id=%s' % (video_type, video_id),
UnboundLocalError: local variable 'video_type' referenced before assignment

# Fixed code
def _real_extract(self, url):
    video_id = self._match_id(url)
    video_type = None
    webpage = self._download_webpage(url, video_id)
    matches = re.search(r"data-(prog|clip)id=\'([0-9]+)\'", webpage)
    if matches:
        video_type, video_id = matches.groups()
        if video_type == "prog":
            video_type = "program"
    else:
        senate_isvp_url = SenateISVPIE._search_iframe_url(webpage)
        if senate_isvp_url:
            title = self._og_search_title(webpage)
            surl = smuggle_url(senate_isvp_url, {"force_title": title})
            return self.url_result(surl, "SenateISVP", video_id, title)
    if video_type is None or video_id is None:
        raise ExtractorError("unable to find video id and type")

    def get_text_attr(d, attr):
        return d.get(attr, {}).get("#text")

    data = self._download_json(
        "http://www.c-span.org/assets/player/ajax-player.php?os=android&html5=%s&id=%s"
        % (video_type, video_id),
        video_id,
    )["video"]
    if data["@status"] != "Success":
        raise ExtractorError(
            "%s said: %s" % (self.IE_NAME, get_text_attr(data, "error")), expected=True
        )

    doc = self._download_xml(
        "http://www.c-span.org/common/services/flashXml.php?%sid=%s"
        % (video_type, video_id),
        video_id,
    )

    description = self._html_search_meta("description", webpage)

    title = find_xpath_attr(doc, ".//string", "name", "title").text
    thumbnail = find_xpath_attr(doc, ".//string", "name", "poster").text

    files = data["files"]
    capfile = get_text_attr(data, "capfile")

    entries = []
    for partnum, f in enumerate(files):
        formats = []
        for quality in f["qualities"]:
            formats.append(
                {
                    "format_id": "%s-%sp"
                    % (
                        get_text_attr(quality, "bitrate"),
                        get_text_attr(quality, "height"),
                    ),
                    "url": unescapeHTML(get_text_attr(quality, "file")),
                    "height": int_or_none(get_text_attr(quality, "height")),
                    "tbr": int_or_none(get_text_attr(quality, "bitrate")),
                }
            )
        self._sort_formats(formats)
        entries.append(
            {
                "id": "%s_%d" % (video_id, partnum + 1),
                "title": (
                    title if len(files) == 1 else "%s part %d" % (title, partnum + 1)
                ),
                "formats": formats,
                "description": description,
                "thumbnail": thumbnail,
                "duration": int_or_none(get_text_attr(f, "length")),
                "subtitles": {
                    "en": [{"url": capfile, "ext": determine_ext(capfile, "dfxp")}],
                }
                if capfile
                else None,
            }
        )

    if len(entries) == 1:
        entry = dict(entries[0])
        entry["id"] = "c" + video_id if video_type == "clip" else video_id
        return entry
    else:
        return {
            "_type": "playlist",
            "entries": entries,
            "title": title,
            "id": "c" + video_id if video_type == "clip" else video_id,
        }