vine.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. import re
  4. from .common import InfoExtractor
  5. from ..utils import (
  6. determine_ext,
  7. int_or_none,
  8. unified_timestamp,
  9. )
  10. class VineIE(InfoExtractor):
  11. _VALID_URL = r'https?://(?:www\.)?vine\.co/(?:v|oembed)/(?P<id>\w+)'
  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._sort_formats(formats)
  83. username = data.get('username')
  84. alt_title = 'Vine by %s' % username if username else None
  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. {
  105. 'url': 'https://vine.co/itsruthb',
  106. 'info_dict': {
  107. 'id': 'itsruthb',
  108. },
  109. 'playlist_mincount': 611,
  110. },
  111. {
  112. 'url': 'https://vine.co/u/942914934646415360',
  113. 'only_matching': True,
  114. },
  115. ]
  116. def _real_extract(self, url):
  117. mobj = re.match(self._VALID_URL, url)
  118. user = mobj.group('user')
  119. u = mobj.group('u')
  120. profile_url = '%sapi/users/profiles/%s%s' % (
  121. self._VINE_BASE_URL, 'vanity/' if not u else '', user)
  122. profile_data = self._download_json(
  123. profile_url, user, note='Downloading user profile data')
  124. user_id = profile_data['data']['userId']
  125. user_archive = self._download_json(
  126. 'https://archive.vine.co/profiles/%s.json' % user_id, user_id)
  127. posts = user_archive['posts']
  128. entries = [
  129. self.url_result('https://vine.co/v/%s' % post_id, 'Vine')
  130. for post_id in posts]
  131. return self.playlist_result(entries, user)