vine.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. import re
  4. import itertools
  5. from .common import InfoExtractor
  6. from ..utils import (
  7. int_or_none,
  8. unified_strdate,
  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 Dorsey',
  20. 'upload_date': '20130519',
  21. 'uploader': 'Jack Dorsey',
  22. 'uploader_id': '76',
  23. 'view_count': int,
  24. 'like_count': int,
  25. 'comment_count': int,
  26. 'repost_count': int,
  27. },
  28. }, {
  29. 'url': 'https://vine.co/v/MYxVapFvz2z',
  30. 'md5': '7b9a7cbc76734424ff942eb52c8f1065',
  31. 'info_dict': {
  32. 'id': 'MYxVapFvz2z',
  33. 'ext': 'mp4',
  34. 'title': 'Fuck Da Police #Mikebrown #justice #ferguson #prayforferguson #protesting #NMOS14',
  35. 'alt_title': 'Vine by Mars Ruiz',
  36. 'upload_date': '20140815',
  37. 'uploader': 'Mars Ruiz',
  38. 'uploader_id': '1102363502380728320',
  39. 'view_count': int,
  40. 'like_count': int,
  41. 'comment_count': int,
  42. 'repost_count': int,
  43. },
  44. }, {
  45. 'url': 'https://vine.co/v/bxVjBbZlPUH',
  46. 'md5': 'ea27decea3fa670625aac92771a96b73',
  47. 'info_dict': {
  48. 'id': 'bxVjBbZlPUH',
  49. 'ext': 'mp4',
  50. 'title': '#mw3 #ac130 #killcam #angelofdeath',
  51. 'alt_title': 'Vine by Z3k3',
  52. 'upload_date': '20130430',
  53. 'uploader': 'Z3k3',
  54. 'uploader_id': '936470460173008896',
  55. 'view_count': int,
  56. 'like_count': int,
  57. 'comment_count': int,
  58. 'repost_count': int,
  59. },
  60. }, {
  61. 'url': 'https://vine.co/oembed/MYxVapFvz2z.json',
  62. 'only_matching': True,
  63. }, {
  64. 'url': 'https://vine.co/v/e192BnZnZ9V',
  65. 'info_dict': {
  66. 'id': 'e192BnZnZ9V',
  67. 'ext': 'mp4',
  68. 'title': 'ยิ้ม~ เขิน~ อาย~ น่าร้ากอ้ะ >//< @n_whitewo @orlameena #lovesicktheseries #lovesickseason2',
  69. 'alt_title': 'Vine by Pimry_zaa',
  70. 'upload_date': '20150705',
  71. 'uploader': 'Pimry_zaa',
  72. 'uploader_id': '1135760698325307392',
  73. 'view_count': int,
  74. 'like_count': int,
  75. 'comment_count': int,
  76. 'repost_count': int,
  77. },
  78. 'params': {
  79. 'skip_download': True,
  80. },
  81. }]
  82. def _real_extract(self, url):
  83. video_id = self._match_id(url)
  84. webpage = self._download_webpage('https://vine.co/v/' + video_id, video_id)
  85. data = self._parse_json(
  86. self._search_regex(
  87. r'window\.POST_DATA\s*=\s*({.+?});\s*</script>',
  88. webpage, 'vine data'),
  89. video_id)
  90. data = data[list(data.keys())[0]]
  91. formats = [{
  92. 'format_id': '%(format)s-%(rate)s' % f,
  93. 'vcodec': f.get('format'),
  94. 'quality': f.get('rate'),
  95. 'url': f['videoUrl'],
  96. } for f in data['videoUrls'] if f.get('videoUrl')]
  97. self._sort_formats(formats)
  98. username = data.get('username')
  99. return {
  100. 'id': video_id,
  101. 'title': data.get('description') or self._og_search_title(webpage),
  102. 'alt_title': 'Vine by %s' % username if username else self._og_search_description(webpage, default=None),
  103. 'thumbnail': data.get('thumbnailUrl'),
  104. 'upload_date': unified_strdate(data.get('created')),
  105. 'uploader': username,
  106. 'uploader_id': data.get('userIdStr'),
  107. 'view_count': int_or_none(data.get('loops', {}).get('count')),
  108. 'like_count': int_or_none(data.get('likes', {}).get('count')),
  109. 'comment_count': int_or_none(data.get('comments', {}).get('count')),
  110. 'repost_count': int_or_none(data.get('reposts', {}).get('count')),
  111. 'formats': formats,
  112. }
  113. class VineUserIE(InfoExtractor):
  114. IE_NAME = 'vine:user'
  115. _VALID_URL = r'(?:https?://)?vine\.co/(?P<u>u/)?(?P<user>[^/]+)/?(\?.*)?$'
  116. _VINE_BASE_URL = 'https://vine.co/'
  117. _TESTS = [
  118. {
  119. 'url': 'https://vine.co/Visa',
  120. 'info_dict': {
  121. 'id': 'Visa',
  122. },
  123. 'playlist_mincount': 46,
  124. },
  125. {
  126. 'url': 'https://vine.co/u/941705360593584128',
  127. 'only_matching': True,
  128. },
  129. ]
  130. def _real_extract(self, url):
  131. mobj = re.match(self._VALID_URL, url)
  132. user = mobj.group('user')
  133. u = mobj.group('u')
  134. profile_url = '%sapi/users/profiles/%s%s' % (
  135. self._VINE_BASE_URL, 'vanity/' if not u else '', user)
  136. profile_data = self._download_json(
  137. profile_url, user, note='Downloading user profile data')
  138. user_id = profile_data['data']['userId']
  139. timeline_data = []
  140. for pagenum in itertools.count(1):
  141. timeline_url = '%sapi/timelines/users/%s?page=%s&size=100' % (
  142. self._VINE_BASE_URL, user_id, pagenum)
  143. timeline_page = self._download_json(
  144. timeline_url, user, note='Downloading page %d' % pagenum)
  145. timeline_data.extend(timeline_page['data']['records'])
  146. if timeline_page['data']['nextPage'] is None:
  147. break
  148. entries = [
  149. self.url_result(e['permalinkUrl'], 'Vine') for e in timeline_data]
  150. return self.playlist_result(entries, user)