vine.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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*%s\s*:\s*({.+?})\s*};\s*</script>' % video_id,
  88. webpage, 'vine data'),
  89. video_id)
  90. formats = [{
  91. 'format_id': '%(format)s-%(rate)s' % f,
  92. 'vcodec': f.get('format'),
  93. 'quality': f.get('rate'),
  94. 'url': f['videoUrl'],
  95. } for f in data['videoUrls'] if f.get('videoUrl')]
  96. self._sort_formats(formats)
  97. username = data.get('username')
  98. return {
  99. 'id': video_id,
  100. 'title': data.get('description') or self._og_search_title(webpage),
  101. 'alt_title': 'Vine by %s' % username if username else self._og_search_description(webpage, default=None),
  102. 'thumbnail': data.get('thumbnailUrl'),
  103. 'upload_date': unified_strdate(data.get('created')),
  104. 'uploader': username,
  105. 'uploader_id': data.get('userIdStr'),
  106. 'view_count': int_or_none(data.get('loops', {}).get('count')),
  107. 'like_count': int_or_none(data.get('likes', {}).get('count')),
  108. 'comment_count': int_or_none(data.get('comments', {}).get('count')),
  109. 'repost_count': int_or_none(data.get('reposts', {}).get('count')),
  110. 'formats': formats,
  111. }
  112. class VineUserIE(InfoExtractor):
  113. IE_NAME = 'vine:user'
  114. _VALID_URL = r'(?:https?://)?vine\.co/(?P<u>u/)?(?P<user>[^/]+)/?(\?.*)?$'
  115. _VINE_BASE_URL = 'https://vine.co/'
  116. _TESTS = [
  117. {
  118. 'url': 'https://vine.co/Visa',
  119. 'info_dict': {
  120. 'id': 'Visa',
  121. },
  122. 'playlist_mincount': 46,
  123. },
  124. {
  125. 'url': 'https://vine.co/u/941705360593584128',
  126. 'only_matching': True,
  127. },
  128. ]
  129. def _real_extract(self, url):
  130. mobj = re.match(self._VALID_URL, url)
  131. user = mobj.group('user')
  132. u = mobj.group('u')
  133. profile_url = '%sapi/users/profiles/%s%s' % (
  134. self._VINE_BASE_URL, 'vanity/' if not u else '', user)
  135. profile_data = self._download_json(
  136. profile_url, user, note='Downloading user profile data')
  137. user_id = profile_data['data']['userId']
  138. timeline_data = []
  139. for pagenum in itertools.count(1):
  140. timeline_url = '%sapi/timelines/users/%s?page=%s&size=100' % (
  141. self._VINE_BASE_URL, user_id, pagenum)
  142. timeline_page = self._download_json(
  143. timeline_url, user, note='Downloading page %d' % pagenum)
  144. timeline_data.extend(timeline_page['data']['records'])
  145. if timeline_page['data']['nextPage'] is None:
  146. break
  147. entries = [
  148. self.url_result(e['permalinkUrl'], 'Vine') for e in timeline_data]
  149. return self.playlist_result(entries, user)