from __future__ import unicode_literals
import re
from .common import InfoExtractor
from ..compat import compat_urlparse
from ..utils import (
    parse_duration,
    parse_iso8601,
    str_to_int,
)
class FourTubeBaseIE(InfoExtractor):
    def _real_extract(self, url):
        mobj = re.match(self._VALID_URL, url)
        kind, video_id, display_id = mobj.group('kind', 'id', 'display_id')
        if kind == 'm' or not display_id:
            url = self._URL_TEMPLATE % video_id
        webpage = self._download_webpage(url, video_id)
        title = self._html_search_meta('name', webpage)
        timestamp = parse_iso8601(self._html_search_meta(
            'uploadDate', webpage))
        thumbnail = self._html_search_meta('thumbnailUrl', webpage)
        uploader_id = self._html_search_regex(
            r'',
            webpage, 'uploader id', fatal=False)
        uploader = self._html_search_regex(
            r'',
            webpage, 'uploader', fatal=False)
        categories_html = self._search_regex(
            r'(?s)>\s*Categories / Tags\s*.*?',
            webpage, 'categories', fatal=False)
        categories = None
        if categories_html:
            categories = [
                c.strip() for c in re.findall(
                    r'(?s)(.*?)', categories_html)]
        view_count = str_to_int(self._search_regex(
            r']+itemprop="interactionCount"[^>]+content="UserPlays:([0-9,]+)">',
            webpage, 'view count', default=None))
        like_count = str_to_int(self._search_regex(
            r']+itemprop="interactionCount"[^>]+content="UserLikes:([0-9,]+)">',
            webpage, 'like count', default=None))
        duration = parse_duration(self._html_search_meta('duration', webpage))
        media_id = self._search_regex(
            r'