Is this a reliable way to get the album key from an album's URL?

andreinlaandreinla Registered Users Posts: 15 Big grins

My goal is to be able to paste an album URL and have my script generate a gallery with the album's images as delivered by the API.

The API needs an album's key in order to list the images.

This script attempts to get the album key from an album URL.

My question for the smugmug team: Is this a reliable way to get the album key?

  def get_album_key_from_url(album_url)
    if image_part = album_url.match(/(\/n-[\w]+)(\/i-[\w]+)/).to_a.last
      ensured_album_url = album_url.gsub(image_part, '')
    else
      image_part = album_url.match(/(\/i-[\w]+)/).to_a.last
      ensured_album_url = album_url.gsub(image_part, '')
    end
    album_url_encoded = URI.encode_www_form_component ensured_album_url
    r = HTTParty.get("#{URL}!weburilookup?APIKey=#{APIKey}&WebUri=#{album_url_encoded}", headers: {accept: 'application/json'})
    begin
      uri = r["Response"]["Album"]["Uri"]
      return nil unless uri.match? '/api/v2/album/'

      uri.gsub('/api/v2/album/', '')
    rescue
      nil
    end
  end

What the code does:

  1. Removes the image part of the URL (remove the /n-ID/i-ID part or just the /i-ID part)
  2. What (hopefully) remains is the "album URL" -- I am not 100% sure. Is this true?
  3. Uses weburllookup to request the API URL for that album
  4. The album key is the last part of that album API's result's hash ["Response"]["Album"]["Uri"]

Comments

Sign In or Register to comment.