smotri.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. # encoding: utf-8
  2. from __future__ import unicode_literals
  3. import os.path
  4. import re
  5. import json
  6. import hashlib
  7. import uuid
  8. from .common import InfoExtractor
  9. from ..utils import (
  10. compat_urllib_parse,
  11. compat_urllib_request,
  12. ExtractorError,
  13. url_basename,
  14. )
  15. class SmotriIE(InfoExtractor):
  16. IE_DESC = 'Smotri.com'
  17. IE_NAME = 'smotri'
  18. _VALID_URL = r'^https?://(?:www\.)?(?P<url>smotri\.com/video/view/\?id=(?P<videoid>v(?P<realvideoid>[0-9]+)[a-z0-9]{4}))'
  19. _NETRC_MACHINE = 'smotri'
  20. _TESTS = [
  21. # real video id 2610366
  22. {
  23. 'url': 'http://smotri.com/video/view/?id=v261036632ab',
  24. 'file': 'v261036632ab.mp4',
  25. 'md5': '2a7b08249e6f5636557579c368040eb9',
  26. 'info_dict': {
  27. 'title': 'катастрофа с камер видеонаблюдения',
  28. 'uploader': 'rbc2008',
  29. 'uploader_id': 'rbc08',
  30. 'upload_date': '20131118',
  31. 'description': 'катастрофа с камер видеонаблюдения, видео катастрофа с камер видеонаблюдения',
  32. 'thumbnail': 'http://frame6.loadup.ru/8b/a9/2610366.3.3.jpg',
  33. },
  34. },
  35. # real video id 57591
  36. {
  37. 'url': 'http://smotri.com/video/view/?id=v57591cb20',
  38. 'file': 'v57591cb20.flv',
  39. 'md5': '830266dfc21f077eac5afd1883091bcd',
  40. 'info_dict': {
  41. 'title': 'test',
  42. 'uploader': 'Support Photofile@photofile',
  43. 'uploader_id': 'support-photofile',
  44. 'upload_date': '20070704',
  45. 'description': 'test, видео test',
  46. 'thumbnail': 'http://frame4.loadup.ru/03/ed/57591.2.3.jpg',
  47. },
  48. },
  49. # video-password
  50. {
  51. 'url': 'http://smotri.com/video/view/?id=v1390466a13c',
  52. 'file': 'v1390466a13c.mp4',
  53. 'md5': 'f6331cef33cad65a0815ee482a54440b',
  54. 'info_dict': {
  55. 'title': 'TOCCA_A_NOI_-_LE_COSE_NON_VANNO_CAMBIAMOLE_ORA-1',
  56. 'uploader': 'timoxa40',
  57. 'uploader_id': 'timoxa40',
  58. 'upload_date': '20100404',
  59. 'thumbnail': 'http://frame7.loadup.ru/af/3f/1390466.3.3.jpg',
  60. 'description': 'TOCCA_A_NOI_-_LE_COSE_NON_VANNO_CAMBIAMOLE_ORA-1, видео TOCCA_A_NOI_-_LE_COSE_NON_VANNO_CAMBIAMOLE_ORA-1',
  61. },
  62. 'params': {
  63. 'videopassword': 'qwerty',
  64. },
  65. },
  66. # age limit + video-password
  67. {
  68. 'url': 'http://smotri.com/video/view/?id=v15408898bcf',
  69. 'file': 'v15408898bcf.flv',
  70. 'md5': '91e909c9f0521adf5ee86fbe073aad70',
  71. 'info_dict': {
  72. 'title': 'этот ролик не покажут по ТВ',
  73. 'uploader': 'zzxxx',
  74. 'uploader_id': 'ueggb',
  75. 'upload_date': '20101001',
  76. 'thumbnail': 'http://frame3.loadup.ru/75/75/1540889.1.3.jpg',
  77. 'age_limit': 18,
  78. 'description': 'этот ролик не покажут по ТВ, видео этот ролик не покажут по ТВ',
  79. },
  80. 'params': {
  81. 'videopassword': '333'
  82. }
  83. }
  84. ]
  85. _SUCCESS = 0
  86. _PASSWORD_NOT_VERIFIED = 1
  87. _PASSWORD_DETECTED = 2
  88. _VIDEO_NOT_FOUND = 3
  89. def _search_meta(self, name, html, display_name=None):
  90. if display_name is None:
  91. display_name = name
  92. return self._html_search_regex(
  93. r'<meta itemprop="%s" content="([^"]+)" />' % re.escape(name),
  94. html, display_name, fatal=False)
  95. return self._html_search_meta(name, html, display_name)
  96. def _real_extract(self, url):
  97. mobj = re.match(self._VALID_URL, url)
  98. video_id = mobj.group('videoid')
  99. real_video_id = mobj.group('realvideoid')
  100. # Download video JSON data
  101. video_json_url = 'http://smotri.com/vt.php?id=%s' % real_video_id
  102. video_json_page = self._download_webpage(video_json_url, video_id, 'Downloading video JSON')
  103. video_json = json.loads(video_json_page)
  104. status = video_json['status']
  105. if status == self._VIDEO_NOT_FOUND:
  106. raise ExtractorError('Video %s does not exist' % video_id, expected=True)
  107. elif status == self._PASSWORD_DETECTED: # The video is protected by a password, retry with
  108. # video-password set
  109. video_password = self._downloader.params.get('videopassword', None)
  110. if not video_password:
  111. raise ExtractorError('This video is protected by a password, use the --video-password option', expected=True)
  112. video_json_url += '&md5pass=%s' % hashlib.md5(video_password.encode('utf-8')).hexdigest()
  113. video_json_page = self._download_webpage(video_json_url, video_id, 'Downloading video JSON (video-password set)')
  114. video_json = json.loads(video_json_page)
  115. status = video_json['status']
  116. if status == self._PASSWORD_NOT_VERIFIED:
  117. raise ExtractorError('Video password is invalid', expected=True)
  118. if status != self._SUCCESS:
  119. raise ExtractorError('Unexpected status value %s' % status)
  120. # Extract the URL of the video
  121. video_url = video_json['file_data']
  122. # Video JSON does not provide enough meta data
  123. # We will extract some from the video web page instead
  124. video_page_url = 'http://' + mobj.group('url')
  125. video_page = self._download_webpage(video_page_url, video_id, 'Downloading video page')
  126. # Warning if video is unavailable
  127. warning = self._html_search_regex(
  128. r'<div class="videoUnModer">(.*?)</div>', video_page,
  129. 'warning message', default=None)
  130. if warning is not None:
  131. self._downloader.report_warning(
  132. 'Video %s may not be available; smotri said: %s ' %
  133. (video_id, warning))
  134. # Adult content
  135. if re.search('EroConfirmText">', video_page) is not None:
  136. self.report_age_confirmation()
  137. confirm_string = self._html_search_regex(
  138. r'<a href="/video/view/\?id=%s&confirm=([^"]+)" title="[^"]+">' % video_id,
  139. video_page, 'confirm string')
  140. confirm_url = video_page_url + '&confirm=%s' % confirm_string
  141. video_page = self._download_webpage(confirm_url, video_id, 'Downloading video page (age confirmed)')
  142. adult_content = True
  143. else:
  144. adult_content = False
  145. # Extract the rest of meta data
  146. video_title = self._search_meta('name', video_page, 'title')
  147. if not video_title:
  148. video_title = os.path.splitext(url_basename(video_url))[0]
  149. video_description = self._search_meta('description', video_page)
  150. END_TEXT = ' на сайте Smotri.com'
  151. if video_description and video_description.endswith(END_TEXT):
  152. video_description = video_description[:-len(END_TEXT)]
  153. START_TEXT = 'Смотреть онлайн ролик '
  154. if video_description and video_description.startswith(START_TEXT):
  155. video_description = video_description[len(START_TEXT):]
  156. video_thumbnail = self._search_meta('thumbnail', video_page)
  157. upload_date_str = self._search_meta('uploadDate', video_page, 'upload date')
  158. if upload_date_str:
  159. upload_date_m = re.search(r'(?P<year>\d{4})\.(?P<month>\d{2})\.(?P<day>\d{2})T', upload_date_str)
  160. video_upload_date = (
  161. (
  162. upload_date_m.group('year') +
  163. upload_date_m.group('month') +
  164. upload_date_m.group('day')
  165. )
  166. if upload_date_m else None
  167. )
  168. else:
  169. video_upload_date = None
  170. duration_str = self._search_meta('duration', video_page)
  171. if duration_str:
  172. duration_m = re.search(r'T(?P<hours>[0-9]{2})H(?P<minutes>[0-9]{2})M(?P<seconds>[0-9]{2})S', duration_str)
  173. video_duration = (
  174. (
  175. (int(duration_m.group('hours')) * 60 * 60) +
  176. (int(duration_m.group('minutes')) * 60) +
  177. int(duration_m.group('seconds'))
  178. )
  179. if duration_m else None
  180. )
  181. else:
  182. video_duration = None
  183. video_uploader = self._html_search_regex(
  184. '<div class="DescrUser"><div>Автор.*?onmouseover="popup_user_info[^"]+">(.*?)</a>',
  185. video_page, 'uploader', fatal=False, flags=re.MULTILINE|re.DOTALL)
  186. video_uploader_id = self._html_search_regex(
  187. '<div class="DescrUser"><div>Автор.*?onmouseover="popup_user_info\\(.*?\'([^\']+)\'\\);">',
  188. video_page, 'uploader id', fatal=False, flags=re.MULTILINE|re.DOTALL)
  189. video_view_count = self._html_search_regex(
  190. 'Общее количество просмотров.*?<span class="Number">(\\d+)</span>',
  191. video_page, 'view count', fatal=False, flags=re.MULTILINE|re.DOTALL)
  192. return {
  193. 'id': video_id,
  194. 'url': video_url,
  195. 'title': video_title,
  196. 'thumbnail': video_thumbnail,
  197. 'description': video_description,
  198. 'uploader': video_uploader,
  199. 'upload_date': video_upload_date,
  200. 'uploader_id': video_uploader_id,
  201. 'duration': video_duration,
  202. 'view_count': video_view_count,
  203. 'age_limit': 18 if adult_content else 0,
  204. 'video_page_url': video_page_url
  205. }
  206. class SmotriCommunityIE(InfoExtractor):
  207. IE_DESC = 'Smotri.com community videos'
  208. IE_NAME = 'smotri:community'
  209. _VALID_URL = r'^https?://(?:www\.)?smotri\.com/community/video/(?P<communityid>[0-9A-Za-z_\'-]+)'
  210. def _real_extract(self, url):
  211. mobj = re.match(self._VALID_URL, url)
  212. community_id = mobj.group('communityid')
  213. url = 'http://smotri.com/export/rss/video/by/community/-/%s/video.xml' % community_id
  214. rss = self._download_xml(url, community_id, 'Downloading community RSS')
  215. entries = [self.url_result(video_url.text, 'Smotri')
  216. for video_url in rss.findall('./channel/item/link')]
  217. description_text = rss.find('./channel/description').text
  218. community_title = self._html_search_regex(
  219. '^Видео сообщества "([^"]+)"$', description_text, 'community title')
  220. return self.playlist_result(entries, community_id, community_title)
  221. class SmotriUserIE(InfoExtractor):
  222. IE_DESC = 'Smotri.com user videos'
  223. IE_NAME = 'smotri:user'
  224. _VALID_URL = r'^https?://(?:www\.)?smotri\.com/user/(?P<userid>[0-9A-Za-z_\'-]+)'
  225. def _real_extract(self, url):
  226. mobj = re.match(self._VALID_URL, url)
  227. user_id = mobj.group('userid')
  228. url = 'http://smotri.com/export/rss/user/video/-/%s/video.xml' % user_id
  229. rss = self._download_xml(url, user_id, 'Downloading user RSS')
  230. entries = [self.url_result(video_url.text, 'Smotri')
  231. for video_url in rss.findall('./channel/item/link')]
  232. description_text = rss.find('./channel/description').text
  233. user_nickname = self._html_search_regex(
  234. '^Видео режиссера (.*)$', description_text,
  235. 'user nickname')
  236. return self.playlist_result(entries, user_id, user_nickname)
  237. class SmotriBroadcastIE(InfoExtractor):
  238. IE_DESC = 'Smotri.com broadcasts'
  239. IE_NAME = 'smotri:broadcast'
  240. _VALID_URL = r'^https?://(?:www\.)?(?P<url>smotri\.com/live/(?P<broadcastid>[^/]+))/?.*'
  241. def _real_extract(self, url):
  242. mobj = re.match(self._VALID_URL, url)
  243. broadcast_id = mobj.group('broadcastid')
  244. broadcast_url = 'http://' + mobj.group('url')
  245. broadcast_page = self._download_webpage(broadcast_url, broadcast_id, 'Downloading broadcast page')
  246. if re.search('>Режиссер с логином <br/>"%s"<br/> <span>не существует<' % broadcast_id, broadcast_page) is not None:
  247. raise ExtractorError('Broadcast %s does not exist' % broadcast_id, expected=True)
  248. # Adult content
  249. if re.search('EroConfirmText">', broadcast_page) is not None:
  250. (username, password) = self._get_login_info()
  251. if username is None:
  252. raise ExtractorError('Erotic broadcasts allowed only for registered users, '
  253. 'use --username and --password options to provide account credentials.', expected=True)
  254. login_form = {
  255. 'login-hint53': '1',
  256. 'confirm_erotic': '1',
  257. 'login': username,
  258. 'password': password,
  259. }
  260. request = compat_urllib_request.Request(broadcast_url + '/?no_redirect=1', compat_urllib_parse.urlencode(login_form))
  261. request.add_header('Content-Type', 'application/x-www-form-urlencoded')
  262. broadcast_page = self._download_webpage(request, broadcast_id, 'Logging in and confirming age')
  263. if re.search('>Неверный логин или пароль<', broadcast_page) is not None:
  264. raise ExtractorError('Unable to log in: bad username or password', expected=True)
  265. adult_content = True
  266. else:
  267. adult_content = False
  268. ticket = self._html_search_regex(
  269. 'window\.broadcast_control\.addFlashVar\\(\'file\', \'([^\']+)\'\\);',
  270. broadcast_page, 'broadcast ticket')
  271. url = 'http://smotri.com/broadcast/view/url/?ticket=%s' % ticket
  272. broadcast_password = self._downloader.params.get('videopassword', None)
  273. if broadcast_password:
  274. url += '&pass=%s' % hashlib.md5(broadcast_password.encode('utf-8')).hexdigest()
  275. broadcast_json_page = self._download_webpage(url, broadcast_id, 'Downloading broadcast JSON')
  276. try:
  277. broadcast_json = json.loads(broadcast_json_page)
  278. protected_broadcast = broadcast_json['_pass_protected'] == 1
  279. if protected_broadcast and not broadcast_password:
  280. raise ExtractorError('This broadcast is protected by a password, use the --video-password option', expected=True)
  281. broadcast_offline = broadcast_json['is_play'] == 0
  282. if broadcast_offline:
  283. raise ExtractorError('Broadcast %s is offline' % broadcast_id, expected=True)
  284. rtmp_url = broadcast_json['_server']
  285. if not rtmp_url.startswith('rtmp://'):
  286. raise ExtractorError('Unexpected broadcast rtmp URL')
  287. broadcast_playpath = broadcast_json['_streamName']
  288. broadcast_thumbnail = broadcast_json['_imgURL']
  289. broadcast_title = broadcast_json['title']
  290. broadcast_description = broadcast_json['description']
  291. broadcaster_nick = broadcast_json['nick']
  292. broadcaster_login = broadcast_json['login']
  293. rtmp_conn = 'S:%s' % uuid.uuid4().hex
  294. except KeyError:
  295. if protected_broadcast:
  296. raise ExtractorError('Bad broadcast password', expected=True)
  297. raise ExtractorError('Unexpected broadcast JSON')
  298. return {
  299. 'id': broadcast_id,
  300. 'url': rtmp_url,
  301. 'title': broadcast_title,
  302. 'thumbnail': broadcast_thumbnail,
  303. 'description': broadcast_description,
  304. 'uploader': broadcaster_nick,
  305. 'uploader_id': broadcaster_login,
  306. 'age_limit': 18 if adult_content else 0,
  307. 'ext': 'flv',
  308. 'play_path': broadcast_playpath,
  309. 'rtmp_live': True,
  310. 'rtmp_conn': rtmp_conn
  311. }