Options

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

  • Options
    leftquarkleftquark Registered Users, Retired Mod Posts: 3,784 Many Grins

    One of our developers will get in touch with you. If you do ever need to email about API questions, feel free to email api@smugmug.com instead of the heroes, as you'll get in touch with one of our developers quicker at the API email.

    One suggestion would be to avoid any regexes, assume that the URL can contain either a AlbumImage, Node, or Album. You can always expand the Album expansion; if it’s an Album things get ignored, otherwise the data you needs would be provided (AlbumKey is in the Album object).

    For example, this URL: https://www.aaronmphotography.com/Galleries/Aurora can be expanded: https://www.smugmug.com/api/v2!weburilookup?WebUri=https://www.aaronmphotography.com/Galleries/Aurora&_expand=Album

    dGrin Afficionado
    Former SmugMug Product Team
    aaron AT aaronmphotography DOT com
    Website: http://www.aaronmphotography.com
    My SmugMug CSS Customizations website: http://www.aaronmphotography.com/Customizations
  • Options
    andreinlaandreinla Registered Users Posts: 15 Big grins

    Thank you. This simplifies things significantly as the script won't need to figure out if the passed url is an image and attempt to remove the image parts.

    I'll keep in mind the api@smugmug.com address.

  • Options
    andreinlaandreinla Registered Users Posts: 15 Big grins

    Seems like these results depend on what I am passing as the URL. But this will sometimes be a link to an image and sometimes a link to an album...

    It could be this:
    https://www.smugmug.com/api/v2!weburilookup?WebUri=https://albums.andreimoment.com/Blogs/WwwTheDancePhotographercom/Blog-Posts-TDP&_expand=Album

    or it could be this:
    https://www.smugmug.com/api/v2!weburilookup?WebUri=https://albums.andreimoment.com/Blogs/WwwTheDancePhotographercom/Blog-Posts-TDP/n-5TX4D/i-FJdjfFv/A&_expand=Album

    because I cannot expect people to figure out and edit the URL at the time of pasting it into a form. It will be "the album URL" which they copied from their browser, and will most likely, but not guaranteed, include the image.

    Is there a way to get a reliable path to the AlbumKey from either result?

    Here's what seems to work now:

     if r["Response"]["Locator"] == "Album"
        album_key = r["Response"]["Album"]["AlbumKey"]
      else
        album_key = r["Expansions"].values.first["Album"]["AlbumKey"]
      end
    

    IMAGE URL
    In the case of an image URL, this seems to work
    uri = r["Expansions"].values.first["Album"]["AlbumKey"]

    but only if the order of the "Expansions" is guaranteed, i.e. the first kay-pair value must contain the "Album" and "AlbumKey". I depend on order because the key is not hardcoded and so I can't guess it.

    Is the order guaranteed?

    "Expansions"=>

    ** {"/api/v2/album/2QkKHc"=> IS THIS GUARANTEED to be the first key/value pair?
    {"Uri"=>"/api/v2/album/2QkKHc",**
    ...
    "Album"=>
    {"NiceName"=>"UCLA-02252010-Gillespie",
    ...
    "AlbumKey"=>"2QkKHc",

  • Options
    gibertigiberti Registered Users, Vanilla Admin Posts: 19 SmugMug Employee

    Is the order guaranteed?

    No, but there will only be one Album returned for an image, so unless you define more expansions, there will only be the one item. You can also inspect the EndpointType field of the response to ensure you are looking at the correct album, or alternatively, for this simple case, use the inline expansion option.

    APIv2 and OAuth are your friend! Having issues? Just ask, I can help!
Sign In or Register to comment.