logo

youtube-dl

[mirror] Download/Watch videos from video hostersgit clone https://hacktivis.me/git/mirror/youtube-dl.git

pinterest.py (7598B)


  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. import json
  4. import re
  5. from .common import InfoExtractor
  6. from ..compat import compat_str
  7. from ..utils import (
  8. determine_ext,
  9. float_or_none,
  10. int_or_none,
  11. try_get,
  12. unified_timestamp,
  13. url_or_none,
  14. )
  15. class PinterestBaseIE(InfoExtractor):
  16. _VALID_URL_BASE = r'https?://(?:[^/]+\.)?pinterest\.(?:com|fr|de|ch|jp|cl|ca|it|co\.uk|nz|ru|com\.au|at|pt|co\.kr|es|com\.mx|dk|ph|th|com\.uy|co|nl|info|kr|ie|vn|com\.vn|ec|mx|in|pe|co\.at|hu|co\.in|co\.nz|id|com\.ec|com\.py|tw|be|uk|com\.bo|com\.pe)'
  17. def _call_api(self, resource, video_id, options):
  18. return self._download_json(
  19. 'https://www.pinterest.com/resource/%sResource/get/' % resource,
  20. video_id, 'Download %s JSON metadata' % resource, query={
  21. 'data': json.dumps({'options': options})
  22. })['resource_response']
  23. def _extract_video(self, data, extract_formats=True):
  24. video_id = data['id']
  25. title = (data.get('title') or data.get('grid_title') or video_id).strip()
  26. urls = []
  27. formats = []
  28. duration = None
  29. if extract_formats:
  30. for format_id, format_dict in data['videos']['video_list'].items():
  31. if not isinstance(format_dict, dict):
  32. continue
  33. format_url = url_or_none(format_dict.get('url'))
  34. if not format_url or format_url in urls:
  35. continue
  36. urls.append(format_url)
  37. duration = float_or_none(format_dict.get('duration'), scale=1000)
  38. ext = determine_ext(format_url)
  39. if 'hls' in format_id.lower() or ext == 'm3u8':
  40. formats.extend(self._extract_m3u8_formats(
  41. format_url, video_id, 'mp4', entry_protocol='m3u8_native',
  42. m3u8_id=format_id, fatal=False))
  43. else:
  44. formats.append({
  45. 'url': format_url,
  46. 'format_id': format_id,
  47. 'width': int_or_none(format_dict.get('width')),
  48. 'height': int_or_none(format_dict.get('height')),
  49. 'duration': duration,
  50. })
  51. self._sort_formats(
  52. formats, field_preference=('height', 'width', 'tbr', 'format_id'))
  53. description = data.get('description') or data.get('description_html') or data.get('seo_description')
  54. timestamp = unified_timestamp(data.get('created_at'))
  55. def _u(field):
  56. return try_get(data, lambda x: x['closeup_attribution'][field], compat_str)
  57. uploader = _u('full_name')
  58. uploader_id = _u('id')
  59. repost_count = int_or_none(data.get('repin_count'))
  60. comment_count = int_or_none(data.get('comment_count'))
  61. categories = try_get(data, lambda x: x['pin_join']['visual_annotation'], list)
  62. tags = data.get('hashtags')
  63. thumbnails = []
  64. images = data.get('images')
  65. if isinstance(images, dict):
  66. for thumbnail_id, thumbnail in images.items():
  67. if not isinstance(thumbnail, dict):
  68. continue
  69. thumbnail_url = url_or_none(thumbnail.get('url'))
  70. if not thumbnail_url:
  71. continue
  72. thumbnails.append({
  73. 'url': thumbnail_url,
  74. 'width': int_or_none(thumbnail.get('width')),
  75. 'height': int_or_none(thumbnail.get('height')),
  76. })
  77. return {
  78. 'id': video_id,
  79. 'title': title,
  80. 'description': description,
  81. 'duration': duration,
  82. 'timestamp': timestamp,
  83. 'thumbnails': thumbnails,
  84. 'uploader': uploader,
  85. 'uploader_id': uploader_id,
  86. 'repost_count': repost_count,
  87. 'comment_count': comment_count,
  88. 'categories': categories,
  89. 'tags': tags,
  90. 'formats': formats,
  91. 'extractor_key': PinterestIE.ie_key(),
  92. }
  93. class PinterestIE(PinterestBaseIE):
  94. _VALID_URL = r'%s/pin/(?P<id>\d+)' % PinterestBaseIE._VALID_URL_BASE
  95. _TESTS = [{
  96. 'url': 'https://www.pinterest.com/pin/664281013778109217/',
  97. 'md5': '6550c2af85d6d9f3fe3b88954d1577fc',
  98. 'info_dict': {
  99. 'id': '664281013778109217',
  100. 'ext': 'mp4',
  101. 'title': 'Origami',
  102. 'description': 'md5:b9d90ddf7848e897882de9e73344f7dd',
  103. 'duration': 57.7,
  104. 'timestamp': 1593073622,
  105. 'upload_date': '20200625',
  106. 'uploader': 'Love origami -I am Dafei',
  107. 'uploader_id': '586523688879454212',
  108. 'repost_count': 50,
  109. 'comment_count': 0,
  110. 'categories': list,
  111. 'tags': list,
  112. },
  113. }, {
  114. 'url': 'https://co.pinterest.com/pin/824721750502199491/',
  115. 'only_matching': True,
  116. }]
  117. def _real_extract(self, url):
  118. video_id = self._match_id(url)
  119. data = self._call_api(
  120. 'Pin', video_id, {
  121. 'field_set_key': 'unauth_react_main_pin',
  122. 'id': video_id,
  123. })['data']
  124. return self._extract_video(data)
  125. class PinterestCollectionIE(PinterestBaseIE):
  126. _VALID_URL = r'%s/(?P<username>[^/]+)/(?P<id>[^/?#&]+)' % PinterestBaseIE._VALID_URL_BASE
  127. _TESTS = [{
  128. 'url': 'https://www.pinterest.ca/mashal0407/cool-diys/',
  129. 'info_dict': {
  130. 'id': '585890301462791043',
  131. 'title': 'cool diys',
  132. },
  133. 'playlist_count': 8,
  134. }, {
  135. 'url': 'https://www.pinterest.ca/fudohub/videos/',
  136. 'info_dict': {
  137. 'id': '682858430939307450',
  138. 'title': 'VIDEOS',
  139. },
  140. 'playlist_mincount': 365,
  141. 'skip': 'Test with extract_formats=False',
  142. }]
  143. @classmethod
  144. def suitable(cls, url):
  145. return False if PinterestIE.suitable(url) else super(
  146. PinterestCollectionIE, cls).suitable(url)
  147. def _real_extract(self, url):
  148. username, slug = re.match(self._VALID_URL, url).groups()
  149. board = self._call_api(
  150. 'Board', slug, {
  151. 'slug': slug,
  152. 'username': username
  153. })['data']
  154. board_id = board['id']
  155. options = {
  156. 'board_id': board_id,
  157. 'page_size': 250,
  158. }
  159. bookmark = None
  160. entries = []
  161. while True:
  162. if bookmark:
  163. options['bookmarks'] = [bookmark]
  164. board_feed = self._call_api('BoardFeed', board_id, options)
  165. for item in (board_feed.get('data') or []):
  166. if not isinstance(item, dict) or item.get('type') != 'pin':
  167. continue
  168. video_id = item.get('id')
  169. if video_id:
  170. # Some pins may not be available anonymously via pin URL
  171. # video = self._extract_video(item, extract_formats=False)
  172. # video.update({
  173. # '_type': 'url_transparent',
  174. # 'url': 'https://www.pinterest.com/pin/%s/' % video_id,
  175. # })
  176. # entries.append(video)
  177. entries.append(self._extract_video(item))
  178. bookmark = board_feed.get('bookmark')
  179. if not bookmark:
  180. break
  181. return self.playlist_result(
  182. entries, playlist_id=board_id, playlist_title=board.get('name'))