# coding: utf-8 from __future__ import unicode_literals from .common import InfoExtractor from ..utils import ( int_or_none, ) import datetime class KakaoIE(InfoExtractor): _VALID_URL = r'https?://tv.kakao.com/channel/(?P\d+)/cliplink/(?P\d+)' IE_NAME = 'kakao.com' _TESTS = [{ 'url': 'http://tv.kakao.com/channel/2671005/cliplink/301965083', 'md5': '702b2fbdeb51ad82f5c904e8c0766340', 'info_dict': { 'id': '301965083', 'ext': 'mp4', 'title': '乃木坂46 バナナマン 「3期生紹介コーナーが始動!顔高低差GPも!」 『乃木坂工事中』', 'uploader_id': 2671005, 'uploader': '그랑그랑이', } }, { 'url': 'http://tv.kakao.com/channel/2653210/cliplink/300103180', 'md5': 'a8917742069a4dd442516b86e7d66529', 'info_dict': { 'id': '300103180', 'ext': 'mp4', 'description': '러블리즈 - Destiny (나의 지구) (Lovelyz - Destiny)\r\n\r\n[쇼! 음악중심] 20160611, 507회', 'title': '러블리즈 - Destiny (나의 지구) (Lovelyz - Destiny)', 'uploader_id': 2653210, 'uploader': '쇼 음악중심', } }] def _real_extract(self, url): video_id = self._match_id(url) # Player URL, to be used in Referer header player_url = 'http://tv.kakao.com/embed/player/cliplink/' + video_id + \ '?service=kakao_tv&autoplay=1&profile=HIGH&wmode=transparent' player_header = {'Referer': player_url} # Request Impress, which contains video information impress = self._download_json( 'http://tv.kakao.com/api/v1/ft/cliplinks/%s/impress' % video_id, video_id, 'Downloading video info', query={ 'player': 'monet_html5', 'referer': url, 'uuid': '', 'service': 'kakao_tv', 'section': '', 'dteType': 'PC', 'fields': 'clipLink,clip,channel,hasPlusFriend,-service,-tagList' }, headers=player_header) clipLink = impress['clipLink'] # Now we request Raw, which contains infos about video files. tid = impress.get('tid', '') raw = self._download_json( 'http://tv.kakao.com/api/v1/ft/cliplinks/%s/raw' % video_id, video_id, 'Downloading video formats info', query={ 'player': 'monet_html5', 'referer': url, 'uuid': '', 'service': 'kakao_tv', 'section': '', 'tid': tid, 'profile': 'HIGH', 'dteType': 'PC', }, headers=player_header) formats = [] for fmt in raw['outputList']: profile_name = fmt['profile'] # The following request is called when user changes the video quality. # We simulate it here. fmt_url_json = self._download_json( 'http://tv.kakao.com/api/v1/ft/cliplinks/%s/raw/videolocation' % video_id, video_id, 'Downloading video URL for profile %s' % profile_name, query={ 'service': 'kakao_tv', 'section': '', 'tid': tid, 'profile': profile_name }, headers=player_header) fmt_url = fmt_url_json['url'] formats.append({ 'url': fmt_url, 'format_id': profile_name, 'width': int_or_none(fmt.get('width')), 'height': int_or_none(fmt.get('height')), 'format_note': fmt.get('label', None), 'filesize': int_or_none(fmt.get('filesize')) }) self._sort_formats(formats) clip = clipLink['clip'] # Parse thumbnails. top_thumbnail = clip.get('thumbnailUrl', None) thumbs = [] for thumb in clip.get('clipChapterThumbnailList', []): thumbs.append({ 'url': thumb['thumbnailUrl'], 'id': str(thumb['timeInSec']), 'preference': -1 if thumb['isDefault'] else 0 }) # Parse upload date. upload_date = None try: upload_date = datetime.datetime.strptime(clipLink['create_time'], '%Y-%m-%d %H:%M:%S') upload_date = upload_date.strftime('%Y%m%d') except (ValueError, KeyError): pass return { 'id': video_id, 'title': clip['title'], 'formats': formats, 'thumbnail': top_thumbnail, 'thumbnails': thumbs, 'description': clip.get('description'), 'uploader': clipLink['channel'].get('name'), 'upload_date': upload_date, 'uploader_id': clipLink.get('channelId'), 'duration': int_or_none(clip.get('duration')), 'view_count': int_or_none(clip.get('playCount')), 'like_count': int_or_none(clip.get('likeCount')), 'comment_count': int_or_none(clip.get('commentCount')), }