fc2.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. import re
  2. import urllib.parse
  3. from .common import InfoExtractor
  4. from ..networking import Request
  5. from ..utils import (
  6. ExtractorError,
  7. js_to_json,
  8. traverse_obj,
  9. update_url_query,
  10. urlencode_postdata,
  11. urljoin,
  12. )
  13. class FC2IE(InfoExtractor):
  14. _VALID_URL = r'^(?:https?://video\.fc2\.com/(?:[^/]+/)*content/|fc2:)(?P<id>[^/]+)'
  15. IE_NAME = 'fc2'
  16. _NETRC_MACHINE = 'fc2'
  17. _TESTS = [{
  18. 'url': 'http://video.fc2.com/en/content/20121103kUan1KHs',
  19. 'md5': 'a6ebe8ebe0396518689d963774a54eb7',
  20. 'info_dict': {
  21. 'id': '20121103kUan1KHs',
  22. 'ext': 'flv',
  23. 'title': 'Boxing again with Puff',
  24. },
  25. }, {
  26. 'url': 'http://video.fc2.com/en/content/20150125cEva0hDn/',
  27. 'info_dict': {
  28. 'id': '20150125cEva0hDn',
  29. 'ext': 'mp4',
  30. },
  31. 'params': {
  32. 'username': 'ytdl@yt-dl.org',
  33. 'password': '(snip)',
  34. },
  35. 'skip': 'requires actual password',
  36. }, {
  37. 'url': 'http://video.fc2.com/en/a/content/20130926eZpARwsF',
  38. 'only_matching': True,
  39. }]
  40. def _login(self):
  41. username, password = self._get_login_info()
  42. if username is None or password is None:
  43. return False
  44. # Log in
  45. login_form_strs = {
  46. 'email': username,
  47. 'password': password,
  48. 'done': 'video',
  49. 'Submit': ' Login ',
  50. }
  51. login_data = urlencode_postdata(login_form_strs)
  52. request = Request(
  53. 'https://secure.id.fc2.com/index.php?mode=login&switch_language=en', login_data)
  54. login_results = self._download_webpage(request, None, note='Logging in', errnote='Unable to log in')
  55. if 'mode=redirect&login=done' not in login_results:
  56. self.report_warning('unable to log in: bad username or password')
  57. return False
  58. # this is also needed
  59. login_redir = Request('http://id.fc2.com/?mode=redirect&login=done')
  60. self._download_webpage(
  61. login_redir, None, note='Login redirect', errnote='Login redirect failed')
  62. return True
  63. def _real_extract(self, url):
  64. video_id = self._match_id(url)
  65. self._login()
  66. webpage = None
  67. if not url.startswith('fc2:'):
  68. webpage = self._download_webpage(url, video_id)
  69. self.cookiejar.clear_session_cookies() # must clear
  70. self._login()
  71. title, thumbnail, description = None, None, None
  72. if webpage is not None:
  73. title = self._html_search_regex(
  74. (r'<h2\s+class="videoCnt_title">([^<]+?)</h2>',
  75. r'\s+href="[^"]+"\s*title="([^"]+?)"\s*rel="nofollow">\s*<img',
  76. # there's two matches in the webpage
  77. r'\s+href="[^"]+"\s*title="([^"]+?)"\s*rel="nofollow">\s*\1'),
  78. webpage,
  79. 'title', fatal=False)
  80. thumbnail = self._og_search_thumbnail(webpage)
  81. description = self._og_search_description(webpage, default=None)
  82. vidplaylist = self._download_json(
  83. f'https://video.fc2.com/api/v3/videoplaylist/{video_id}?sh=1&fs=0', video_id,
  84. note='Downloading info page')
  85. vid_url = traverse_obj(vidplaylist, ('playlist', 'nq'))
  86. if not vid_url:
  87. raise ExtractorError('Unable to extract video URL')
  88. vid_url = urljoin('https://video.fc2.com/', vid_url)
  89. return {
  90. 'id': video_id,
  91. 'title': title,
  92. 'url': vid_url,
  93. 'ext': 'mp4',
  94. 'protocol': 'm3u8_native',
  95. 'description': description,
  96. 'thumbnail': thumbnail,
  97. }
  98. class FC2EmbedIE(InfoExtractor):
  99. _VALID_URL = r'https?://video\.fc2\.com/flv2\.swf\?(?P<query>.+)'
  100. IE_NAME = 'fc2:embed'
  101. _TEST = {
  102. 'url': 'http://video.fc2.com/flv2.swf?t=201404182936758512407645&i=20130316kwishtfitaknmcgd76kjd864hso93htfjcnaogz629mcgfs6rbfk0hsycma7shkf85937cbchfygd74&i=201403223kCqB3Ez&d=2625&sj=11&lang=ja&rel=1&from=11&cmt=1&tk=TlRBM09EQTNNekU9&tl=プリズン・ブレイク%20S1-01%20マイケル%20【吹替】',
  103. 'md5': 'b8aae5334cb691bdb1193a88a6ab5d5a',
  104. 'info_dict': {
  105. 'id': '201403223kCqB3Ez',
  106. 'ext': 'flv',
  107. 'title': 'プリズン・ブレイク S1-01 マイケル 【吹替】',
  108. 'thumbnail': r're:^https?://.*\.jpg$',
  109. },
  110. }
  111. def _real_extract(self, url):
  112. mobj = self._match_valid_url(url)
  113. query = urllib.parse.parse_qs(mobj.group('query'))
  114. video_id = query['i'][-1]
  115. title = query.get('tl', [f'FC2 video {video_id}'])[0]
  116. sj = query.get('sj', [None])[0]
  117. thumbnail = None
  118. if sj:
  119. # See thumbnailImagePath() in ServerConst.as of flv2.swf
  120. thumbnail = 'http://video{}-thumbnail.fc2.com/up/pic/{}.jpg'.format(
  121. sj, '/'.join((video_id[:6], video_id[6:8], video_id[-2], video_id[-1], video_id)))
  122. return {
  123. '_type': 'url_transparent',
  124. 'ie_key': FC2IE.ie_key(),
  125. 'url': f'fc2:{video_id}',
  126. 'title': title,
  127. 'thumbnail': thumbnail,
  128. }
  129. class FC2LiveIE(InfoExtractor):
  130. _VALID_URL = r'https?://live\.fc2\.com/(?P<id>\d+)'
  131. IE_NAME = 'fc2:live'
  132. _TESTS = [{
  133. 'url': 'https://live.fc2.com/57892267/',
  134. 'info_dict': {
  135. 'id': '57892267',
  136. 'title': 'どこまで・・・',
  137. 'uploader': 'あつあげ',
  138. 'uploader_id': '57892267',
  139. 'thumbnail': r're:https?://.+fc2.+',
  140. },
  141. 'skip': 'livestream',
  142. }]
  143. def _real_extract(self, url):
  144. video_id = self._match_id(url)
  145. webpage = self._download_webpage(f'https://live.fc2.com/{video_id}/', video_id)
  146. self._set_cookie('live.fc2.com', 'js-player_size', '1')
  147. member_api = self._download_json(
  148. 'https://live.fc2.com/api/memberApi.php', video_id, data=urlencode_postdata({
  149. 'channel': '1',
  150. 'profile': '1',
  151. 'user': '1',
  152. 'streamid': video_id,
  153. }), note='Requesting member info')
  154. control_server = self._download_json(
  155. 'https://live.fc2.com/api/getControlServer.php', video_id, note='Downloading ControlServer data',
  156. data=urlencode_postdata({
  157. 'channel_id': video_id,
  158. 'mode': 'play',
  159. 'orz': '',
  160. 'channel_version': member_api['data']['channel_data']['version'],
  161. 'client_version': '2.1.0\n [1]',
  162. 'client_type': 'pc',
  163. 'client_app': 'browser_hls',
  164. 'ipv6': '',
  165. }), headers={'X-Requested-With': 'XMLHttpRequest'})
  166. self._set_cookie('live.fc2.com', 'l_ortkn', control_server['orz_raw'])
  167. ws_url = update_url_query(control_server['url'], {'control_token': control_server['control_token']})
  168. playlist_data = None
  169. ws = self._request_webpage(Request(ws_url, headers={
  170. 'Origin': 'https://live.fc2.com',
  171. }), video_id, note='Fetching HLS playlist info via WebSocket')
  172. self.write_debug('Sending HLS server request')
  173. while True:
  174. recv = ws.recv()
  175. if not recv:
  176. continue
  177. data = self._parse_json(recv, video_id, fatal=False)
  178. if not data or not isinstance(data, dict):
  179. continue
  180. if data.get('name') == 'connect_complete':
  181. break
  182. ws.send(r'{"name":"get_hls_information","arguments":{},"id":1}')
  183. while True:
  184. recv = ws.recv()
  185. if not recv:
  186. continue
  187. data = self._parse_json(recv, video_id, fatal=False)
  188. if not data or not isinstance(data, dict):
  189. continue
  190. if data.get('name') == '_response_' and data.get('id') == 1:
  191. self.write_debug('Goodbye')
  192. playlist_data = data
  193. break
  194. self.write_debug('Server said: {}{}'.format(recv[:100], '...' if len(recv) > 100 else ''))
  195. if not playlist_data:
  196. raise ExtractorError('Unable to fetch HLS playlist info via WebSocket')
  197. formats = []
  198. for name, playlists in playlist_data['arguments'].items():
  199. if not isinstance(playlists, list):
  200. continue
  201. for pl in playlists:
  202. if pl.get('status') == 0 and 'master_playlist' in pl.get('url'):
  203. formats.extend(self._extract_m3u8_formats(
  204. pl['url'], video_id, ext='mp4', m3u8_id=name, live=True,
  205. headers={
  206. 'Origin': 'https://live.fc2.com',
  207. 'Referer': url,
  208. }))
  209. for fmt in formats:
  210. fmt.update({
  211. 'protocol': 'fc2_live',
  212. 'ws': ws,
  213. })
  214. title = self._html_search_meta(('og:title', 'twitter:title'), webpage, 'live title', fatal=False)
  215. if not title:
  216. title = self._html_extract_title(webpage, 'html title', fatal=False)
  217. if title:
  218. # remove service name in <title>
  219. title = re.sub(r'\s+-\s+.+$', '', title)
  220. uploader = None
  221. if title:
  222. match = self._search_regex(r'^(.+?)\s*\[(.+?)\]$', title, 'title and uploader', default=None, group=(1, 2))
  223. if match and all(match):
  224. title, uploader = match
  225. live_info_view = self._search_regex(r'(?s)liveInfoView\s*:\s*({.+?}),\s*premiumStateView', webpage, 'user info', fatal=False) or None
  226. if live_info_view:
  227. # remove jQuery code from object literal
  228. live_info_view = re.sub(r'\$\(.+?\)[^,]+,', '"",', live_info_view)
  229. live_info_view = self._parse_json(js_to_json(live_info_view), video_id)
  230. return {
  231. 'id': video_id,
  232. 'title': title or traverse_obj(live_info_view, 'title'),
  233. 'description': self._html_search_meta(
  234. ('og:description', 'twitter:description'),
  235. webpage, 'live description', fatal=False) or traverse_obj(live_info_view, 'info'),
  236. 'formats': formats,
  237. 'uploader': uploader or traverse_obj(live_info_view, 'name'),
  238. 'uploader_id': video_id,
  239. 'thumbnail': traverse_obj(live_info_view, 'thumb'),
  240. 'is_live': True,
  241. }