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.

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