smotri.py 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. # encoding: utf-8
  2. import re
  3. import json
  4. import hashlib
  5. from .common import InfoExtractor
  6. from ..utils import (
  7. determine_ext,
  8. ExtractorError
  9. )
  10. class SmotriIE(InfoExtractor):
  11. IE_DESC = u'Smotri.com'
  12. IE_NAME = u'smotri'
  13. _VALID_URL = r'^(?:http://)?(?:www\.)?(?P<url>smotri\.com/video/view/\?id=(?P<videoid>v(?P<realvideoid>[0-9]+)[a-z0-9]{4}))'
  14. _TESTS = [
  15. # real video id 2610366
  16. {
  17. u'url': u'http://smotri.com/video/view/?id=v261036632ab',
  18. u'file': u'v261036632ab.mp4',
  19. u'md5': u'46a72e83a6ad8862b64fa6953fa93f8a',
  20. u'info_dict': {
  21. u'title': u'катастрофа с камер видеонаблюдения',
  22. u'uploader': u'rbc2008',
  23. u'uploader_id': u'rbc08',
  24. u'upload_date': u'20131118',
  25. u'thumbnail': u'http://frame6.loadup.ru/8b/a9/2610366.3.3.jpg'
  26. },
  27. },
  28. # real video id 57591
  29. {
  30. u'url': u'http://smotri.com/video/view/?id=v57591cb20',
  31. u'file': u'v57591cb20.flv',
  32. u'md5': u'9eae59f6dda7087bf39a140e2fff5757',
  33. u'info_dict': {
  34. u'title': u'test',
  35. u'uploader': u'Support Photofile@photofile',
  36. u'uploader_id': u'support-photofile',
  37. u'upload_date': u'20070704',
  38. u'thumbnail': u'http://frame4.loadup.ru/03/ed/57591.2.3.jpg'
  39. },
  40. },
  41. # video-password
  42. {
  43. u'url': u'http://smotri.com/video/view/?id=v1390466a13c',
  44. u'file': u'v1390466a13c.mp4',
  45. u'md5': u'fe4dd9357558d5ee3c8fc0ef0d39de66',
  46. u'info_dict': {
  47. u'title': u'TOCCA_A_NOI_-_LE_COSE_NON_VANNO_CAMBIAMOLE_ORA-1',
  48. u'uploader': u'timoxa40',
  49. u'uploader_id': u'timoxa40',
  50. u'upload_date': u'20100404',
  51. u'thumbnail': u'http://frame7.loadup.ru/af/3f/1390466.3.3.jpg'
  52. },
  53. u'params': {
  54. u'videopassword': u'qwerty',
  55. },
  56. },
  57. # age limit + video-password
  58. {
  59. u'url': u'http://smotri.com/video/view/?id=v15408898bcf',
  60. u'file': u'v15408898bcf.flv',
  61. u'md5': u'c66a5d61379ac6fde06f07eebe436316',
  62. u'info_dict': {
  63. u'title': u'этот ролик не покажут по ТВ',
  64. u'uploader': u'zzxxx',
  65. u'uploader_id': u'ueggb',
  66. u'upload_date': u'20101001',
  67. u'thumbnail': u'http://frame3.loadup.ru/75/75/1540889.1.3.jpg',
  68. u'age_limit': 18
  69. },
  70. u'params': {
  71. u'videopassword': u'333'
  72. }
  73. }
  74. ]
  75. _SUCCESS = 0
  76. _PASSWORD_NOT_VERIFIED = 1
  77. _PASSWORD_DETECTED = 2
  78. _VIDEO_NOT_FOUND = 3
  79. def _search_meta(self, name, html, display_name=None):
  80. if display_name is None:
  81. display_name = name
  82. return self._html_search_regex(
  83. r'<meta itemprop="%s" content="([^"]+)" />' % re.escape(name),
  84. html, display_name, fatal=False)
  85. def _real_extract(self, url):
  86. mobj = re.match(self._VALID_URL, url)
  87. video_id = mobj.group('videoid')
  88. real_video_id = mobj.group('realvideoid')
  89. # Download video JSON data
  90. video_json_url = 'http://smotri.com/vt.php?id=%s' % real_video_id
  91. video_json_page = self._download_webpage(video_json_url, video_id, u'Downloading video JSON')
  92. video_json = json.loads(video_json_page)
  93. status = video_json['status']
  94. if status == self._VIDEO_NOT_FOUND:
  95. raise ExtractorError(u'Video %s does not exist' % video_id, expected=True)
  96. elif status == self._PASSWORD_DETECTED: # The video is protected by a password, retry with
  97. # video-password set
  98. video_password = self._downloader.params.get('videopassword', None)
  99. if not video_password:
  100. raise ExtractorError(u'This video is protected by a password, use the --video-password option', expected=True)
  101. video_json_url += '&md5pass=%s' % hashlib.md5(video_password).hexdigest()
  102. video_json_page = self._download_webpage(video_json_url, video_id, u'Downloading video JSON (video-password set)')
  103. video_json = json.loads(video_json_page)
  104. status = video_json['status']
  105. if status == self._PASSWORD_NOT_VERIFIED:
  106. raise ExtractorError(u'Video password is invalid', expected=True)
  107. if status != self._SUCCESS:
  108. raise ExtractorError(u'Unexpected status value %s' % status)
  109. # Extract the URL of the video
  110. video_url = video_json['file_data']
  111. video_ext = determine_ext(video_url)
  112. # Video JSON does not provide enough meta data
  113. # We will extract some from the video web page instead
  114. video_page_url = 'http://' + mobj.group('url')
  115. video_page = self._download_webpage(video_page_url, video_id, u'Downloading video page')
  116. # Adult content
  117. if re.search(u'EroConfirmText">', video_page) is not None:
  118. self.report_age_confirmation()
  119. confirm_string = self._html_search_regex(
  120. ur'<a href="/video/view/\?id=%s&confirm=([^"]+)" title="[^"]+">' % video_id,
  121. video_page, u'confirm string')
  122. confirm_url = video_page_url + '&confirm=%s' % confirm_string
  123. video_page = self._download_webpage(confirm_url, video_id, u'Downloading video page (age confirmed)')
  124. adult_content = True
  125. else:
  126. adult_content = False
  127. # Extract the rest of meta data
  128. video_title = self._search_meta(u'name', video_page, u'title')
  129. if not video_title:
  130. video_title = video_url.rsplit('/', 1)[-1]
  131. video_description = self._search_meta(u'description', video_page)
  132. video_thumbnail = self._search_meta(u'thumbnail', video_page)
  133. upload_date_str = self._search_meta(u'uploadDate', video_page, u'upload date')
  134. upload_date_m = re.search(r'(?P<year>\d{4})\.(?P<month>\d{2})\.(?P<day>\d{2})T', upload_date_str)
  135. video_upload_date = (
  136. (
  137. upload_date_m.group('year') +
  138. upload_date_m.group('month') +
  139. upload_date_m.group('day')
  140. )
  141. if upload_date_m else None
  142. )
  143. duration_str = self._search_meta(u'duration', video_page)
  144. 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)
  145. video_duration = (
  146. (
  147. (int(duration_m.group('hours')) * 60 * 60) +
  148. (int(duration_m.group('minutes')) * 60) +
  149. int(duration_m.group('seconds'))
  150. )
  151. if duration_m else None
  152. )
  153. video_uploader = self._html_search_regex(
  154. ur'<div class="DescrUser"><div>Автор.*?onmouseover="popup_user_info[^"]+">(.*?)</a>',
  155. video_page, u'uploader', fatal=False, flags=re.MULTILINE|re.DOTALL)
  156. video_uploader_id = self._html_search_regex(
  157. ur'<div class="DescrUser"><div>Автор.*?onmouseover="popup_user_info\(.*?\'([^\']+)\'\);">',
  158. video_page, u'uploader id', fatal=False, flags=re.MULTILINE|re.DOTALL)
  159. video_view_count = self._html_search_regex(
  160. ur'Общее количество просмотров.*?<span class="Number">(\d+)</span>',
  161. video_page, u'view count', fatal=False, flags=re.MULTILINE|re.DOTALL)
  162. return {
  163. 'id': video_id,
  164. 'url': video_url,
  165. 'title': video_title,
  166. 'ext': video_ext,
  167. 'thumbnail': video_thumbnail,
  168. 'description': video_description,
  169. 'uploader': video_uploader,
  170. 'upload_date': video_upload_date,
  171. 'uploader_id': video_uploader_id,
  172. 'video_duration': video_duration,
  173. 'view_count': video_view_count,
  174. 'age_limit': 18 if adult_content else 0,
  175. 'video_page_url': video_page_url
  176. }
  177. class SmotriCommunityIE(InfoExtractor):
  178. IE_DESC = u'Smotri.com community videos'
  179. IE_NAME = u'smotri:community'
  180. _VALID_URL = r'^(?:http://)?(?:www\.)?smotri\.com/community/video/(?P<communityid>[0-9A-Za-z_\'-]+)'
  181. def _real_extract(self, url):
  182. mobj = re.match(self._VALID_URL, url)
  183. community_id = mobj.group('communityid')
  184. url = 'http://smotri.com/export/rss/video/by/community/-/%s/video.xml' % community_id
  185. rss = self._download_xml(url, community_id, u'Downloading community RSS')
  186. entries = [self.url_result(video_url.text, 'Smotri')
  187. for video_url in rss.findall('./channel/item/link')]
  188. community_title = self._html_search_regex(
  189. ur'^Видео сообщества "([^"]+)"$', rss.find('./channel/description').text, u'community title')
  190. return self.playlist_result(entries, community_id, community_title)
  191. class SmotriUserIE(InfoExtractor):
  192. IE_DESC = u'Smotri.com user videos'
  193. IE_NAME = u'smotri:user'
  194. _VALID_URL = r'^(?:http://)?(?:www\.)?smotri\.com/user/(?P<userid>[0-9A-Za-z_\'-]+)'
  195. def _real_extract(self, url):
  196. mobj = re.match(self._VALID_URL, url);
  197. user_id = mobj.group('userid')
  198. url = 'http://smotri.com/export/rss/user/video/-/%s/video.xml' % user_id
  199. rss = self._download_xml(url, user_id, u'Downloading user RSS')
  200. entries = [self.url_result(video_url.text, 'Smotri')
  201. for video_url in rss.findall('./channel/item/link')]
  202. user_nickname = self._html_search_regex(
  203. ur'^Видео режиссера (.*)$', rss.find('./channel/description').text, u'user nickname')
  204. return self.playlist_result(entries, user_id, user_nickname)