7 from .common
import InfoExtractor
13 compat_urllib_parse_urlparse
,
14 compat_urllib_request
,
21 class BlipTVIE(InfoExtractor
):
22 """Information extractor for blip.tv"""
24 _VALID_URL
= r
'^(?:https?://)?(?:\w+\.)?blip\.tv/((.+/)|(play/)|(api\.swf#))(.+)$'
25 _URL_EXT
= r
'^.*\.([a-z0-9]+)$'
28 def report_direct_download(self
, title
):
29 """Report information extraction."""
30 self
.to_screen(u
'%s: Direct download detected' % title
)
32 def _real_extract(self
, url
):
33 mobj
= re
.match(self
._VALID
_URL
, url
)
35 raise ExtractorError(u
'Invalid URL: %s' % url
)
37 # See https://github.com/rg3/youtube-dl/issues/857
38 api_mobj
= re
.match(r
'http://a\.blip\.tv/api\.swf#(?P<video_id>[\d\w]+)', url
)
39 if api_mobj
is not None:
40 url
= 'http://blip.tv/play/g_%s' % api_mobj
.group('video_id')
41 urlp
= compat_urllib_parse_urlparse(url
)
42 if urlp
.path
.startswith('/play/'):
43 request
= compat_urllib_request
.Request(url
)
44 response
= compat_urllib_request
.urlopen(request
)
45 redirecturl
= response
.geturl()
46 rurlp
= compat_urllib_parse_urlparse(redirecturl
)
47 file_id
= compat_parse_qs(rurlp
.fragment
)['file'][0].rpartition('/')[2]
48 url
= 'http://blip.tv/a/a-' + file_id
49 return self
._real
_extract
(url
)
56 json_url
= url
+ cchar
+ 'skin=json&version=2&no_wrap=1'
57 request
= compat_urllib_request
.Request(json_url
)
58 request
.add_header('User-Agent', 'iTunes/10.6.1')
59 self
.report_extraction(mobj
.group(1))
62 urlh
= compat_urllib_request
.urlopen(request
)
63 if urlh
.headers
.get('Content-Type', '').startswith('video/'): # Direct download
64 basename
= url
.split('/')[-1]
65 title
,ext
= os
.path
.splitext(basename
)
66 title
= title
.decode('UTF-8')
67 ext
= ext
.replace('.', '')
68 self
.report_direct_download(title
)
78 except (compat_urllib_error
.URLError
, compat_http_client
.HTTPException
, socket
.error
) as err
:
79 raise ExtractorError(u
'ERROR: unable to download video info webpage: %s' % compat_str(err
))
80 if info
is None: # Regular URL
82 json_code_bytes
= urlh
.read()
83 json_code
= json_code_bytes
.decode('utf-8')
84 except (compat_urllib_error
.URLError
, compat_http_client
.HTTPException
, socket
.error
) as err
:
85 raise ExtractorError(u
'Unable to read video info webpage: %s' % compat_str(err
))
88 json_data
= json
.loads(json_code
)
89 if 'Post' in json_data
:
90 data
= json_data
['Post']
94 upload_date
= datetime
.datetime
.strptime(data
['datestamp'], '%m-%d-%y %H:%M%p').strftime('%Y%m%d')
95 video_url
= data
['media']['url']
96 umobj
= re
.match(self
._URL
_EXT
, video_url
)
98 raise ValueError('Can not determine filename extension')
102 'id': data
['item_id'],
104 'uploader': data
['display_name'],
105 'upload_date': upload_date
,
106 'title': data
['title'],
108 'format': data
['media']['mimeType'],
109 'thumbnail': data
['thumbnailUrl'],
110 'description': data
['description'],
111 'player_url': data
['embedUrl'],
112 'user_agent': 'iTunes/10.6.1',
114 except (ValueError,KeyError) as err
:
115 raise ExtractorError(u
'Unable to parse video information: %s' % repr(err
))
120 class BlipTVUserIE(InfoExtractor
):
121 """Information Extractor for blip.tv users."""
123 _VALID_URL
= r
'(?:(?:(?:https?://)?(?:\w+\.)?blip\.tv/)|bliptvuser:)([^/]+)/*$'
125 IE_NAME
= u
'blip.tv:user'
127 def _real_extract(self
, url
):
129 mobj
= re
.match(self
._VALID
_URL
, url
)
131 raise ExtractorError(u
'Invalid URL: %s' % url
)
133 username
= mobj
.group(1)
135 page_base
= 'http://m.blip.tv/pr/show_get_full_episode_list?users_id=%s&lite=0&esi=1'
137 page
= self
._download
_webpage
(url
, username
, u
'Downloading user page')
138 mobj
= re
.search(r
'data-users-id="([^"]+)"', page
)
139 page_base
= page_base
% mobj
.group(1)
142 # Download video ids using BlipTV Ajax calls. Result size per
143 # query is limited (currently to 12 videos) so we need to query
144 # page by page until there are no video ids - it means we got
151 url
= page_base
+ "&page=" + str(pagenum
)
152 page
= self
._download
_webpage
(url
, username
,
153 u
'Downloading video ids from page %d' % pagenum
)
155 # Extract video identifiers
158 for mobj
in re
.finditer(r
'href="/([^"]+)"', page
):
159 if mobj
.group(1) not in ids_in_page
:
160 ids_in_page
.append(unescapeHTML(mobj
.group(1)))
162 video_ids
.extend(ids_in_page
)
164 # A little optimization - if current page is not
165 # "full", ie. does not contain PAGE_SIZE video ids then
166 # we can assume that this page is the last one - there
167 # are no more ids on further pages - no need to query
170 if len(ids_in_page
) < self
._PAGE
_SIZE
:
175 urls
= [u
'http://blip.tv/%s' % video_id
for video_id
in video_ids
]
176 url_entries
= [self
.url_result(url
, 'BlipTV') for url
in urls
]
177 return [self
.playlist_result(url_entries
, playlist_title
= username
)]