You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

297 lines
9.7 KiB

10 years ago
  1. from __future__ import unicode_literals
  2. import itertools
  3. from .common import InfoExtractor
  4. from ..compat import (
  5. compat_HTTPError,
  6. compat_str,
  7. )
  8. from ..utils import (
  9. ExtractorError,
  10. int_or_none,
  11. float_or_none,
  12. parse_iso8601,
  13. )
  14. class VidmeIE(InfoExtractor):
  15. IE_NAME = 'vidme'
  16. _VALID_URL = r'https?://vid\.me/(?:e/)?(?P<id>[\da-zA-Z]{,5})(?:[^\da-zA-Z]|$)'
  17. _TESTS = [{
  18. 'url': 'https://vid.me/QNB',
  19. 'md5': 'f42d05e7149aeaec5c037b17e5d3dc82',
  20. 'info_dict': {
  21. 'id': 'QNB',
  22. 'ext': 'mp4',
  23. 'title': 'Fishing for piranha - the easy way',
  24. 'description': 'source: https://www.facebook.com/photo.php?v=312276045600871',
  25. 'thumbnail': r're:^https?://.*\.jpg',
  26. 'timestamp': 1406313244,
  27. 'upload_date': '20140725',
  28. 'age_limit': 0,
  29. 'duration': 119.92,
  30. 'view_count': int,
  31. 'like_count': int,
  32. 'comment_count': int,
  33. },
  34. }, {
  35. 'url': 'https://vid.me/Gc6M',
  36. 'md5': 'f42d05e7149aeaec5c037b17e5d3dc82',
  37. 'info_dict': {
  38. 'id': 'Gc6M',
  39. 'ext': 'mp4',
  40. 'title': 'O Mere Dil ke chain - Arnav and Khushi VM',
  41. 'thumbnail': r're:^https?://.*\.jpg',
  42. 'timestamp': 1441211642,
  43. 'upload_date': '20150902',
  44. 'uploader': 'SunshineM',
  45. 'uploader_id': '3552827',
  46. 'age_limit': 0,
  47. 'duration': 223.72,
  48. 'view_count': int,
  49. 'like_count': int,
  50. 'comment_count': int,
  51. },
  52. 'params': {
  53. 'skip_download': True,
  54. },
  55. }, {
  56. # tests uploader field
  57. 'url': 'https://vid.me/4Iib',
  58. 'info_dict': {
  59. 'id': '4Iib',
  60. 'ext': 'mp4',
  61. 'title': 'The Carver',
  62. 'description': 'md5:e9c24870018ae8113be936645b93ba3c',
  63. 'thumbnail': r're:^https?://.*\.jpg',
  64. 'timestamp': 1433203629,
  65. 'upload_date': '20150602',
  66. 'uploader': 'Thomas',
  67. 'uploader_id': '109747',
  68. 'age_limit': 0,
  69. 'duration': 97.859999999999999,
  70. 'view_count': int,
  71. 'like_count': int,
  72. 'comment_count': int,
  73. },
  74. 'params': {
  75. 'skip_download': True,
  76. },
  77. }, {
  78. # nsfw test from http://naked-yogi.tumblr.com/post/118312946248/naked-smoking-stretching
  79. 'url': 'https://vid.me/e/Wmur',
  80. 'info_dict': {
  81. 'id': 'Wmur',
  82. 'ext': 'mp4',
  83. 'title': 'naked smoking & stretching',
  84. 'thumbnail': r're:^https?://.*\.jpg',
  85. 'timestamp': 1430931613,
  86. 'upload_date': '20150506',
  87. 'uploader': 'naked-yogi',
  88. 'uploader_id': '1638622',
  89. 'age_limit': 18,
  90. 'duration': 653.26999999999998,
  91. 'view_count': int,
  92. 'like_count': int,
  93. 'comment_count': int,
  94. },
  95. 'params': {
  96. 'skip_download': True,
  97. },
  98. }, {
  99. # nsfw, user-disabled
  100. 'url': 'https://vid.me/dzGJ',
  101. 'only_matching': True,
  102. }, {
  103. # suspended
  104. 'url': 'https://vid.me/Ox3G',
  105. 'only_matching': True,
  106. }, {
  107. # deleted
  108. 'url': 'https://vid.me/KTPm',
  109. 'only_matching': True,
  110. }, {
  111. # no formats in the API response
  112. 'url': 'https://vid.me/e5g',
  113. 'info_dict': {
  114. 'id': 'e5g',
  115. 'ext': 'mp4',
  116. 'title': 'Video upload (e5g)',
  117. 'thumbnail': r're:^https?://.*\.jpg',
  118. 'timestamp': 1401480195,
  119. 'upload_date': '20140530',
  120. 'uploader': None,
  121. 'uploader_id': None,
  122. 'age_limit': 0,
  123. 'duration': 483,
  124. 'view_count': int,
  125. 'like_count': int,
  126. 'comment_count': int,
  127. },
  128. 'params': {
  129. 'skip_download': True,
  130. },
  131. }]
  132. def _real_extract(self, url):
  133. video_id = self._match_id(url)
  134. try:
  135. response = self._download_json(
  136. 'https://api.vid.me/videoByUrl/%s' % video_id, video_id)
  137. except ExtractorError as e:
  138. if isinstance(e.cause, compat_HTTPError) and e.cause.code == 400:
  139. response = self._parse_json(e.cause.read(), video_id)
  140. else:
  141. raise
  142. error = response.get('error')
  143. if error:
  144. raise ExtractorError(
  145. '%s returned error: %s' % (self.IE_NAME, error), expected=True)
  146. video = response['video']
  147. if video.get('state') == 'deleted':
  148. raise ExtractorError(
  149. 'Vidme said: Sorry, this video has been deleted.',
  150. expected=True)
  151. if video.get('state') in ('user-disabled', 'suspended'):
  152. raise ExtractorError(
  153. 'Vidme said: This video has been suspended either due to a copyright claim, '
  154. 'or for violating the terms of use.',
  155. expected=True)
  156. formats = []
  157. for f in video.get('formats', []):
  158. format_url = f.get('uri')
  159. if not format_url or not isinstance(format_url, compat_str):
  160. continue
  161. format_type = f.get('type')
  162. if format_type == 'dash':
  163. formats.extend(self._extract_mpd_formats(
  164. format_url, video_id, mpd_id='dash', fatal=False))
  165. elif format_type == 'hls':
  166. formats.extend(self._extract_m3u8_formats(
  167. format_url, video_id, 'mp4', entry_protocol='m3u8_native',
  168. m3u8_id='hls', fatal=False))
  169. else:
  170. formats.append({
  171. 'format_id': f.get('type'),
  172. 'url': format_url,
  173. 'width': int_or_none(f.get('width')),
  174. 'height': int_or_none(f.get('height')),
  175. 'preference': 0 if f.get('type', '').endswith(
  176. 'clip') else 1,
  177. })
  178. if not formats and video.get('complete_url'):
  179. formats.append({
  180. 'url': video.get('complete_url'),
  181. 'width': int_or_none(video.get('width')),
  182. 'height': int_or_none(video.get('height')),
  183. })
  184. self._sort_formats(formats)
  185. title = video['title']
  186. description = video.get('description')
  187. thumbnail = video.get('thumbnail_url')
  188. timestamp = parse_iso8601(video.get('date_created'), ' ')
  189. uploader = video.get('user', {}).get('username')
  190. uploader_id = video.get('user', {}).get('user_id')
  191. age_limit = 18 if video.get('nsfw') is True else 0
  192. duration = float_or_none(video.get('duration'))
  193. view_count = int_or_none(video.get('view_count'))
  194. like_count = int_or_none(video.get('likes_count'))
  195. comment_count = int_or_none(video.get('comment_count'))
  196. return {
  197. 'id': video_id,
  198. 'title': title or 'Video upload (%s)' % video_id,
  199. 'description': description,
  200. 'thumbnail': thumbnail,
  201. 'uploader': uploader,
  202. 'uploader_id': uploader_id,
  203. 'age_limit': age_limit,
  204. 'timestamp': timestamp,
  205. 'duration': duration,
  206. 'view_count': view_count,
  207. 'like_count': like_count,
  208. 'comment_count': comment_count,
  209. 'formats': formats,
  210. }
  211. class VidmeListBaseIE(InfoExtractor):
  212. # Max possible limit according to https://docs.vid.me/#api-Videos-List
  213. _LIMIT = 100
  214. def _entries(self, user_id, user_name):
  215. for page_num in itertools.count(1):
  216. page = self._download_json(
  217. 'https://api.vid.me/videos/%s?user=%s&limit=%d&offset=%d'
  218. % (self._API_ITEM, user_id, self._LIMIT, (page_num - 1) * self._LIMIT),
  219. user_name, 'Downloading user %s page %d' % (self._API_ITEM, page_num))
  220. videos = page.get('videos', [])
  221. if not videos:
  222. break
  223. for video in videos:
  224. video_url = video.get('full_url') or video.get('embed_url')
  225. if video_url:
  226. yield self.url_result(video_url, VidmeIE.ie_key())
  227. total = int_or_none(page.get('page', {}).get('total'))
  228. if total and self._LIMIT * page_num >= total:
  229. break
  230. def _real_extract(self, url):
  231. user_name = self._match_id(url)
  232. user_id = self._download_json(
  233. 'https://api.vid.me/userByUsername?username=%s' % user_name,
  234. user_name)['user']['user_id']
  235. return self.playlist_result(
  236. self._entries(user_id, user_name), user_id,
  237. '%s - %s' % (user_name, self._TITLE))
  238. class VidmeUserIE(VidmeListBaseIE):
  239. IE_NAME = 'vidme:user'
  240. _VALID_URL = r'https?://vid\.me/(?:e/)?(?P<id>[\da-zA-Z_-]{6,})(?!/likes)(?:[^\da-zA-Z_-]|$)'
  241. _API_ITEM = 'list'
  242. _TITLE = 'Videos'
  243. _TESTS = [{
  244. 'url': 'https://vid.me/MasakoX',
  245. 'info_dict': {
  246. 'id': '16112341',
  247. 'title': 'MasakoX - %s' % _TITLE,
  248. },
  249. 'playlist_mincount': 191,
  250. }, {
  251. 'url': 'https://vid.me/unsQuare_netWork',
  252. 'only_matching': True,
  253. }]
  254. class VidmeUserLikesIE(VidmeListBaseIE):
  255. IE_NAME = 'vidme:user:likes'
  256. _VALID_URL = r'https?://vid\.me/(?:e/)?(?P<id>[\da-zA-Z_-]{6,})/likes'
  257. _API_ITEM = 'likes'
  258. _TITLE = 'Likes'
  259. _TESTS = [{
  260. 'url': 'https://vid.me/ErinAlexis/likes',
  261. 'info_dict': {
  262. 'id': '6483530',
  263. 'title': 'ErinAlexis - %s' % _TITLE,
  264. },
  265. 'playlist_mincount': 415,
  266. }, {
  267. 'url': 'https://vid.me/Kaleidoscope-Ish/likes',
  268. 'only_matching': True,
  269. }]