dogpixels / patreon-helper

Downloads media as you browse Patreon, so you don't have to do this manually.
MIT License
22 stars 5 forks source link

sha256sum hash based file names #15

Open BigBlueHat opened 1 year ago

BigBlueHat commented 1 year ago

Many times the filenames folks use are identical (i.e. 1.jpg, 2.jpg, etc.). Consequently, downloading overlapping named files leaves you with 1 (1).jpg and similar, so unrelated files end up sorting together.

SHA256sums wouldn't fix the sorting problem, but could prevent downloading duplicates--which happens when artists release the same content as a preview post and a full post for subscribers.

BigBlueHat commented 1 year ago

This should help (with love from Bing code generation 😛):

async function getHash(url) {
  const response = await fetch(url);
  const blob = await response.blob();
  const buffer = await blob.arrayBuffer();
  const hashBuffer = await crypto.subtle.digest('SHA-256', buffer);
  const hashArray = Array.from(new Uint8Array(hashBuffer));
  const hashHex = hashArray.map(b => b.toString(16).padStart(2, '0')).join('');
  return hashHex;
}

getHash('https://example.com/image.jpg').then(hash => {
  console.log(hash);
});