amazon.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. from .common import InfoExtractor
  2. from ..utils import ExtractorError, int_or_none
  3. class AmazonStoreIE(InfoExtractor):
  4. _VALID_URL = r'https?://(?:www\.)?amazon\.(?:[a-z]{2,3})(?:\.[a-z]{2})?/(?:[^/]+/)?(?:dp|gp/product)/(?P<id>[^/&#$?]+)'
  5. _TESTS = [{
  6. 'url': 'https://www.amazon.co.uk/dp/B098XNCHLD/',
  7. 'info_dict': {
  8. 'id': 'B098XNCHLD',
  9. 'title': 'md5:dae240564cbb2642170c02f7f0d7e472',
  10. },
  11. 'playlist_mincount': 1,
  12. 'playlist': [{
  13. 'info_dict': {
  14. 'id': 'A1F83G8C2ARO7P',
  15. 'ext': 'mp4',
  16. 'title': 'mcdodo usb c cable 100W 5a',
  17. 'thumbnail': r're:^https?://.*\.jpg$',
  18. 'duration': 34,
  19. },
  20. }]
  21. }, {
  22. 'url': 'https://www.amazon.in/Sony-WH-1000XM4-Cancelling-Headphones-Bluetooth/dp/B0863TXGM3',
  23. 'info_dict': {
  24. 'id': 'B0863TXGM3',
  25. 'title': 'md5:d1d3352428f8f015706c84b31e132169',
  26. },
  27. 'playlist_mincount': 4,
  28. }, {
  29. 'url': 'https://www.amazon.com/dp/B0845NXCXF/',
  30. 'info_dict': {
  31. 'id': 'B0845NXCXF',
  32. 'title': 'md5:f3fa12779bf62ddb6a6ec86a360a858e',
  33. },
  34. 'playlist-mincount': 1,
  35. }, {
  36. 'url': 'https://www.amazon.es/Samsung-Smartphone-s-AMOLED-Quad-c%C3%A1mara-espa%C3%B1ola/dp/B08WX337PQ',
  37. 'info_dict': {
  38. 'id': 'B08WX337PQ',
  39. 'title': 'md5:f3fa12779bf62ddb6a6ec86a360a858e',
  40. },
  41. 'playlist_mincount': 1,
  42. }]
  43. def _real_extract(self, url):
  44. id = self._match_id(url)
  45. for retry in self.RetryManager():
  46. webpage = self._download_webpage(url, id)
  47. try:
  48. data_json = self._search_json(
  49. r'var\s?obj\s?=\s?jQuery\.parseJSON\(\'', webpage, 'data', id,
  50. transform_source=lambda x: x.replace(R'\\u', R'\u'))
  51. except ExtractorError as e:
  52. retry.error = e
  53. entries = [{
  54. 'id': video['marketPlaceID'],
  55. 'url': video['url'],
  56. 'title': video.get('title'),
  57. 'thumbnail': video.get('thumbUrl') or video.get('thumb'),
  58. 'duration': video.get('durationSeconds'),
  59. 'height': int_or_none(video.get('videoHeight')),
  60. 'width': int_or_none(video.get('videoWidth')),
  61. } for video in (data_json.get('videos') or []) if video.get('isVideo') and video.get('url')]
  62. return self.playlist_result(entries, playlist_id=id, playlist_title=data_json.get('title'))