smotri.py 15 KB

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