smotri.py 16 KB

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