Created
April 4, 2019 09:35
-
-
Save rbrahul/0d4cacef214f8e7a1fd44350a5a68c77 to your computer and use it in GitHub Desktop.
Grab all ASINs from all countries from Amazon using one ASIN
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta name="description" content="ASIN GRABBER"> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width"> | |
<title>JS Bin</title> | |
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" type="text/css" /> | |
<script src="https://code.jquery.com/jquery-2.2.4.js"></script> | |
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script> | |
</head> | |
<body> | |
<div class="container"> | |
<h2>ASIN Grabber</h2> | |
<div class="card"> | |
<form> | |
<div class="form-group row"> | |
<label for="staticEmail" class="col-sm-2 col-form-label">Enter your ASIN</label> | |
<div class="col-sm-10"> | |
<input type="text" placeholder="asin" class="form-control-plaintext" id="asin"> | |
</div> | |
</div> | |
<div class="form-group"> | |
<label for="exampleTextarea">ASINS</label> | |
<textarea class="form-control" id="asins" rows="3"></textarea> | |
</div> | |
</div> | |
<div class="form-group"> | |
<label for="exampleTextarea">Missing Countries</label> | |
<div class="alert alert-danger"><strong id="missing"></strong></div> | |
</div> | |
<div class="form-group"> | |
<button type="button" id="asin-btn" class="btn btn-success">Grab ASIN</button> | |
</div> | |
</form> | |
</div> | |
</div> | |
<script id="jsbin-javascript"> | |
$(document).ready(function() { | |
var countryCode= { | |
de: 'de', | |
uk: 'co.uk', | |
it: 'it', | |
es: 'es', | |
fr: 'fr', | |
cn: 'cn', | |
jp: 'co.jp', | |
in: 'in', | |
ca: 'ca', | |
br: 'com.br', | |
us:'com' | |
}; | |
$('#asin-btn').click(function() { | |
var asin = $.trim($('#asin').val()); | |
var assinAttrs = []; | |
var missingCountries = []; | |
var totalRequest = Object.keys(countryCode).length; | |
for(var [code, domain] of Object.entries(countryCode)) { | |
(function(_code,_domain){ | |
$.ajax({ | |
url: `https://www.amazon.${_domain}/dp/${asin}`, | |
beforeSend: () => { | |
--totalRequest; | |
}, | |
method:'GET', | |
success: data => { | |
assinAttrs.push(`asin[${_code}]=${asin}`); | |
}, | |
error: err => { | |
console.error('Error:') | |
missingCountries.push(_code); | |
}, | |
complete: () => { | |
console.log(totalRequest); | |
if(totalRequest===0 || totalRequest < -1) { | |
console.log('Finished'); | |
$("#asins").val(assinAttrs.join('&')); | |
$("#missing").html('<p>Missing Countries: <span class="badge badge-primary badge-pill">'+missingCountries.length+'</span></p>'+missingCountries.join('<br/>')); | |
} | |
} | |
}, | |
) | |
})(code, domain); | |
} | |
}) | |
}); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment