vine.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. from __future__ import unicode_literals
  2. import re
  3. import json
  4. import itertools
  5. from .common import InfoExtractor
  6. from ..utils import unified_strdate
  7. class VineIE(InfoExtractor):
  8. _VALID_URL = r'https?://(?:www\.)?vine\.co/(?:v|oembed)/(?P<id>\w+)'
  9. _TESTS = [{
  10. 'url': 'https://vine.co/v/b9KOOWX7HUx',
  11. 'md5': '2f36fed6235b16da96ce9b4dc890940d',
  12. 'info_dict': {
  13. 'id': 'b9KOOWX7HUx',
  14. 'ext': 'mp4',
  15. 'title': 'Chicken.',
  16. 'alt_title': 'Vine by Jack Dorsey',
  17. 'description': 'Chicken.',
  18. 'upload_date': '20130519',
  19. 'uploader': 'Jack Dorsey',
  20. 'uploader_id': '76',
  21. },
  22. }, {
  23. 'url': 'https://vine.co/v/MYxVapFvz2z',
  24. 'md5': '7b9a7cbc76734424ff942eb52c8f1065',
  25. 'info_dict': {
  26. 'id': 'MYxVapFvz2z',
  27. 'ext': 'mp4',
  28. 'title': 'Fuck Da Police #Mikebrown #justice #ferguson #prayforferguson #protesting #NMOS14',
  29. 'alt_title': 'Vine by Luna',
  30. 'description': 'Fuck Da Police #Mikebrown #justice #ferguson #prayforferguson #protesting #NMOS14',
  31. 'upload_date': '20140815',
  32. 'uploader': 'Luna',
  33. 'uploader_id': '1102363502380728320',
  34. },
  35. }, {
  36. 'url': 'https://vine.co/v/bxVjBbZlPUH',
  37. 'md5': 'ea27decea3fa670625aac92771a96b73',
  38. 'info_dict': {
  39. 'id': 'bxVjBbZlPUH',
  40. 'ext': 'mp4',
  41. 'title': '#mw3 #ac130 #killcam #angelofdeath',
  42. 'alt_title': 'Vine by Z3k3',
  43. 'description': '#mw3 #ac130 #killcam #angelofdeath',
  44. 'upload_date': '20130430',
  45. 'uploader': 'Z3k3',
  46. 'uploader_id': '936470460173008896',
  47. },
  48. }, {
  49. 'url': 'https://vine.co/oembed/MYxVapFvz2z.json',
  50. 'only_matching': True,
  51. }]
  52. def _real_extract(self, url):
  53. video_id = self._match_id(url)
  54. webpage = self._download_webpage('https://vine.co/v/' + video_id, video_id)
  55. data = self._parse_json(
  56. self._html_search_regex(
  57. r'window\.POST_DATA = { %s: ({.+?}) };\s*</script>' % video_id,
  58. webpage, 'vine data'),
  59. video_id)
  60. formats = [{
  61. 'format_id': '%(format)s-%(rate)s' % f,
  62. 'vcodec': f['format'],
  63. 'quality': f['rate'],
  64. 'url': f['videoUrl'],
  65. } for f in data['videoUrls']]
  66. self._sort_formats(formats)
  67. return {
  68. 'id': video_id,
  69. 'title': self._og_search_title(webpage),
  70. 'alt_title': self._og_search_description(webpage),
  71. 'description': data['description'],
  72. 'thumbnail': data['thumbnailUrl'],
  73. 'upload_date': unified_strdate(data['created']),
  74. 'uploader': data['username'],
  75. 'uploader_id': data['userIdStr'],
  76. 'like_count': data['likes']['count'],
  77. 'comment_count': data['comments']['count'],
  78. 'repost_count': data['reposts']['count'],
  79. 'formats': formats,
  80. }
  81. class VineUserIE(InfoExtractor):
  82. IE_NAME = 'vine:user'
  83. _VALID_URL = r'(?:https?://)?vine\.co/(?P<u>u/)?(?P<user>[^/]+)/?(\?.*)?$'
  84. _VINE_BASE_URL = "https://vine.co/"
  85. _TESTS = [
  86. {
  87. 'url': 'https://vine.co/Visa',
  88. 'info_dict': {
  89. 'id': 'Visa',
  90. },
  91. 'playlist_mincount': 46,
  92. },
  93. {
  94. 'url': 'https://vine.co/u/941705360593584128',
  95. 'only_matching': True,
  96. },
  97. ]
  98. def _real_extract(self, url):
  99. mobj = re.match(self._VALID_URL, url)
  100. user = mobj.group('user')
  101. u = mobj.group('u')
  102. profile_url = "%sapi/users/profiles/%s%s" % (
  103. self._VINE_BASE_URL, 'vanity/' if not u else '', user)
  104. profile_data = self._download_json(
  105. profile_url, user, note='Downloading user profile data')
  106. user_id = profile_data['data']['userId']
  107. timeline_data = []
  108. for pagenum in itertools.count(1):
  109. timeline_url = "%sapi/timelines/users/%s?page=%s&size=100" % (
  110. self._VINE_BASE_URL, user_id, pagenum)
  111. timeline_page = self._download_json(
  112. timeline_url, user, note='Downloading page %d' % pagenum)
  113. timeline_data.extend(timeline_page['data']['records'])
  114. if timeline_page['data']['nextPage'] is None:
  115. break
  116. entries = [
  117. self.url_result(e['permalinkUrl'], 'Vine') for e in timeline_data]
  118. return self.playlist_result(entries, user)