vidme.py 8.1 KB

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