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.

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