vine.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. from .common import InfoExtractor
  2. from ..compat import compat_str
  3. from ..utils import (
  4. determine_ext,
  5. format_field,
  6. int_or_none,
  7. unified_timestamp,
  8. )
  9. class VineIE(InfoExtractor):
  10. _VALID_URL = r'https?://(?:www\.)?vine\.co/(?:v|oembed)/(?P<id>\w+)'
  11. _EMBED_REGEX = [r'<iframe[^>]+src=[\'"](?P<url>(?:https?:)?//(?:www\.)?vine\.co/v/[^/]+/embed/(?:simple|postcard))']
  12. _TESTS = [{
  13. 'url': 'https://vine.co/v/b9KOOWX7HUx',
  14. 'md5': '2f36fed6235b16da96ce9b4dc890940d',
  15. 'info_dict': {
  16. 'id': 'b9KOOWX7HUx',
  17. 'ext': 'mp4',
  18. 'title': 'Chicken.',
  19. 'alt_title': 'Vine by Jack',
  20. 'timestamp': 1368997951,
  21. 'upload_date': '20130519',
  22. 'uploader': 'Jack',
  23. 'uploader_id': '76',
  24. 'view_count': int,
  25. 'like_count': int,
  26. 'comment_count': int,
  27. 'repost_count': int,
  28. },
  29. }, {
  30. 'url': 'https://vine.co/v/e192BnZnZ9V',
  31. 'info_dict': {
  32. 'id': 'e192BnZnZ9V',
  33. 'ext': 'mp4',
  34. 'title': 'ยิ้ม~ เขิน~ อาย~ น่าร้ากอ้ะ >//< @n_whitewo @orlameena #lovesicktheseries #lovesickseason2',
  35. 'alt_title': 'Vine by Pimry_zaa',
  36. 'timestamp': 1436057405,
  37. 'upload_date': '20150705',
  38. 'uploader': 'Pimry_zaa',
  39. 'uploader_id': '1135760698325307392',
  40. 'view_count': int,
  41. 'like_count': int,
  42. 'comment_count': int,
  43. 'repost_count': int,
  44. },
  45. 'params': {
  46. 'skip_download': True,
  47. },
  48. }, {
  49. 'url': 'https://vine.co/v/MYxVapFvz2z',
  50. 'only_matching': True,
  51. }, {
  52. 'url': 'https://vine.co/v/bxVjBbZlPUH',
  53. 'only_matching': True,
  54. }, {
  55. 'url': 'https://vine.co/oembed/MYxVapFvz2z.json',
  56. 'only_matching': True,
  57. }]
  58. def _real_extract(self, url):
  59. video_id = self._match_id(url)
  60. data = self._download_json(
  61. 'https://archive.vine.co/posts/%s.json' % video_id, video_id)
  62. def video_url(kind):
  63. for url_suffix in ('Url', 'URL'):
  64. format_url = data.get('video%s%s' % (kind, url_suffix))
  65. if format_url:
  66. return format_url
  67. formats = []
  68. for quality, format_id in enumerate(('low', '', 'dash')):
  69. format_url = video_url(format_id.capitalize())
  70. if not format_url:
  71. continue
  72. # DASH link returns plain mp4
  73. if format_id == 'dash' and determine_ext(format_url) == 'mpd':
  74. formats.extend(self._extract_mpd_formats(
  75. format_url, video_id, mpd_id='dash', fatal=False))
  76. else:
  77. formats.append({
  78. 'url': format_url,
  79. 'format_id': format_id or 'standard',
  80. 'quality': quality,
  81. })
  82. self._check_formats(formats, video_id)
  83. username = data.get('username')
  84. alt_title = format_field(username, None, 'Vine by %s')
  85. return {
  86. 'id': video_id,
  87. 'title': data.get('description') or alt_title or 'Vine video',
  88. 'alt_title': alt_title,
  89. 'thumbnail': data.get('thumbnailUrl'),
  90. 'timestamp': unified_timestamp(data.get('created')),
  91. 'uploader': username,
  92. 'uploader_id': data.get('userIdStr'),
  93. 'view_count': int_or_none(data.get('loops')),
  94. 'like_count': int_or_none(data.get('likes')),
  95. 'comment_count': int_or_none(data.get('comments')),
  96. 'repost_count': int_or_none(data.get('reposts')),
  97. 'formats': formats,
  98. }
  99. class VineUserIE(InfoExtractor):
  100. IE_NAME = 'vine:user'
  101. _VALID_URL = r'https?://vine\.co/(?P<u>u/)?(?P<user>[^/]+)'
  102. _VINE_BASE_URL = 'https://vine.co/'
  103. _TESTS = [{
  104. 'url': 'https://vine.co/itsruthb',
  105. 'info_dict': {
  106. 'id': 'itsruthb',
  107. 'title': 'Ruth B',
  108. 'description': '| Instagram/Twitter: itsruthb | still a lost boy from neverland',
  109. },
  110. 'playlist_mincount': 611,
  111. }, {
  112. 'url': 'https://vine.co/u/942914934646415360',
  113. 'only_matching': True,
  114. }]
  115. @classmethod
  116. def suitable(cls, url):
  117. return False if VineIE.suitable(url) else super(VineUserIE, cls).suitable(url)
  118. def _real_extract(self, url):
  119. mobj = self._match_valid_url(url)
  120. user = mobj.group('user')
  121. u = mobj.group('u')
  122. profile_url = '%sapi/users/profiles/%s%s' % (
  123. self._VINE_BASE_URL, 'vanity/' if not u else '', user)
  124. profile_data = self._download_json(
  125. profile_url, user, note='Downloading user profile data')
  126. data = profile_data['data']
  127. user_id = data.get('userId') or data['userIdStr']
  128. profile = self._download_json(
  129. 'https://archive.vine.co/profiles/%s.json' % user_id, user_id)
  130. entries = [
  131. self.url_result(
  132. 'https://vine.co/v/%s' % post_id, ie='Vine', video_id=post_id)
  133. for post_id in profile['posts']
  134. if post_id and isinstance(post_id, compat_str)]
  135. return self.playlist_result(
  136. entries, user, profile.get('username'), profile.get('description'))