How does one find the Album ID and Album Key given its URL using API 1.2.2?

ckelleyckelley Big grinsPosts: 23Registered Users Big grins
I'm writing server-side code in C# to upload images from an ASP.NET website to SmugMug via API 1.3.0. I figured out that a 1.3.0 Album has become a Gallery. The following code will find a Gallery given its URL:
    // post image to SmugMug
    IWebClient wc = new WebClient();
    IProtocol protocol = new RestProtocol();
    SmugmugClient smugMugClient = new SmugmugClient(wc, protocol);
    LoginWithHash login = new LoginWithHash(Config.ApiKey, Config.UserId.Value, Config.PasswordHash);
    SmugmugSession session = new SmugmugSession(smugMugClient, login);
    if (session != null)
   {
        List<Album> albums = session.GetAlbums().ToList();
        Album album = albums.Where(a => a.Url ==  "http://idtech.smugmug.com/UCBERK/IDTC/2014/June23June272014").FirstOrDefault();
        if (album != null)
        {
    	    session.UploadImage(path, (int)album.Id, album.Key, "CAK Test", null, null);
        }
    }

I need help on how to navigate the folder structure that determines the URL using API 1.3.0 because I need to create a nested folder structure if I don't find the the gallery by its URL. I need to:
  • search for a top-level folder,
  • retrieve one of its subfolders,
  • create a sub-folder and
  • create a gallery

Can API 1.3.0 traverse nested folders in the new SmugMug?

If not, what API verion and method should I use? I can't wait for API 2.0.

Comments

  • ckelleyckelley Big grins Posts: 23Registered Users Big grins
    edited April 25, 2014
    How to Create a New SmugMug Galleries I 3-Level Folder Structure with API 1.2.2 in C#
    I had to fix SmugMug.Net wrapper as it did not support SubCategories correctly. The following code is working:
    private Album CreateNewSmugMugGallery(string locationAbbrev, string divisionAbbrev, string sessionTitle, string sessionAbbrev)
    {
        string year = sessionAbbrev.Substring(sessionAbbrev.Length - 4);
        Album sessionGallery = null;
        Album gallery = new Album();
        gallery.Title = sessionTitle;
        gallery.NiceName = sessionAbbrev;
        try
        {
    	// retrieve the entire folder structure
    	List<Smugmug.Category> locationCategoryList = this.MySmugMugSession.GetCategories().ToList();
    	Category locationCat = locationCategoryList.Where(loc => locationAbbrev.Equals(loc.NiceName, StringComparison.InvariantCultureIgnoreCase)).FirstOrDefault();
    	if (locationCat == null)
    	{
    	    locationCat = this.MySmugMugSession.CreateCategory(ucLocationDropDownList.SelectedText, locationAbbrev);
    	    SubCategory divisionCat = this.MySmugMugSession.CreateSubCategory(locationCat.Id, divisionAbbrev);
    	    SubCategory yearCat = this.MySmugMugSession.CreateSubCategory(divisionCat.Id, year);
    	    gallery.Category = locationCat;
    	    gallery.SubCategory = yearCat;
    	    sessionGallery = this.MySmugMugSession.CreateAlbum(gallery);
    	}
    	else
    	{
    	    // location folder already exists
    	    // look for Division
    	    List<SubCategory> divisionCatList = this.MySmugMugSession.GetSubCategories(locationCat.Id, null).ToList();
    	    SubCategory divisionCat = divisionCatList.Where(d => divisionAbbrev.Equals(d.NiceName, StringComparison.InvariantCultureIgnoreCase)
    							|| divisionAbbrev.Equals(d.Name, StringComparison.InvariantCultureIgnoreCase)).FirstOrDefault();
    	    if (divisionCat == null)
    	    {
    		// division folder does not exist under location
    		divisionCat = this.MySmugMugSession.CreateSubCategory(locationCat.Id, divisionAbbrev);
    		SubCategory yearCat = this.MySmugMugSession.CreateSubCategory(divisionCat.Id, year);
    		gallery.Category = locationCat;
    		gallery.SubCategory = yearCat;
    		sessionGallery = this.MySmugMugSession.CreateAlbum(gallery);
    	    }
    	    else
    	    {
    		// division folder already exists
    		List<SubCategory> yearCatList = this.MySmugMugSession.GetSubCategories(divisionCat.Id, null).ToList();
    		SubCategory yearCat = yearCatList.Where(y => year == y.Name).FirstOrDefault();
    		if (yearCat == null)
    		{
    		    // year folder does not exist under division
    		    yearCat = this.MySmugMugSession.CreateSubCategory(divisionCat.Id, year);
    		    gallery.Category = locationCat;
    		    gallery.SubCategory = yearCat;
    		    sessionGallery = this.MySmugMugSession.CreateAlbum(gallery);
    		}
    		else
    		{
    		    // year folder already exists
    		    gallery.Category = locationCat;
    		    gallery.SubCategory = yearCat;
    		    sessionGallery = this.MySmugMugSession.CreateAlbum(gallery);
    		}
    	    }
    	}
        }
        finally 
        {
    	this.MySmugMugSession.Logout();
        }
    
        if (sessionGallery == null)
        {
    	throw new Exception("Unable to create SmugMug folders and/or gallery for URL:  " + this.SmugMugGalleryUrl);
        }
        else if (string.IsNullOrWhiteSpace(sessionGallery.Url))
        {
    	// it was just created, so we need to fetch it again for its URL and other generated values
    	sessionGallery = this.MySmugMugSession.GetAlbum(sessionGallery.Id, sessionGallery.Key);
        }
        return sessionGallery;
    }
    
Sign In or Register to comment.