smotri.py 14 KB

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