pinterest.py 7.2 KB

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