private void TestForKodakPremierMember(KodakAlbum currentAlbum, KodakPhoto photo) { string fullPhotoUri = string.Format("http://www.kodakgallery.com/servlet/FullResDownload?collid={0}&photoid={1}", currentAlbum.albumId, photo.photoId); HttpStatusCode statusCode = HttpStatusCode.Unused; string errorString = ""; try { HttpWebRequest photoRequest = (HttpWebRequest)WebRequest.Create(fullPhotoUri); photoRequest.Method = "GET"; photoRequest.UserAgent = kegUserAgent; photoRequest.CookieContainer = new CookieContainer(); photoRequest.CookieContainer.Add(this.kodakLoginCookies); using (HttpWebResponse photoResponse = (HttpWebResponse)photoRequest.GetResponse()) { statusCode = photoResponse.StatusCode; } } catch (WebException wex) { using (HttpWebResponse response = (HttpWebResponse)wex.Response) { statusCode = response.StatusCode; } } catch (Exception ex) { errorString = ex.ToString(); } if (statusCode == HttpStatusCode.OK) { testForKodakPremierMember = true; return; } else { Console.WriteLine(); Console.WriteLine("Uh-oh! Looks like youre not a Kodak Easyshare Gallery premier member!"); Console.WriteLine("Request to Kodak Easyshare gallery failed with error {0}", (statusCode == HttpStatusCode.Unused) ? errorString : statusCode.ToString()); Environment.Exit(9); } }
/// <summary> /// Once a photo has been uploaded, record the fact so we can skip it next time around /// </summary> private void UpdatePhotoStatus(KodakAlbum currentAlbum, KodakPhoto currentPhoto) { string statusFile = string.Format("{0}\\Keg2Smug.{1}.status", temporaryLocation, currentAlbum.albumId); FileStream fileStream = null; if (File.Exists(statusFile)) { fileStream = File.OpenWrite(statusFile); } else { fileStream = File.Create(statusFile); } using (fileStream) { // go to the end of the file fileStream.Seek(0, SeekOrigin.End); // Write the albumId using (StreamWriter fileStreamWriter = new StreamWriter(fileStream)) { fileStreamWriter.WriteLine(currentPhoto.photoId); fileStreamWriter.Flush(); } fileStream.Close(); } }
/// <summary> /// Skip kodak albums which have already been uploaded to SmugMug /// </summary> private void SkipCompletedAlbums() { int skippedAlbums = 0; string statusFile = string.Format("{0}\\{1}", temporaryLocation, "keg2smug.status"); if (File.Exists(statusFile)) { using (FileStream fileStream = File.OpenRead(statusFile)) { using (StreamReader fileStreamReader = new StreamReader(fileStream)) { // Read each line from the file string currentAlbumId = fileStreamReader.ReadLine(); while (currentAlbumId != null) { KodakAlbum tempAlbum = new KodakAlbum(currentAlbumId, null); if (kodakAlbums.Contains(tempAlbum)) { kodakAlbums.Remove(tempAlbum); skippedAlbums++; } currentAlbumId = fileStreamReader.ReadLine(); } } } } Console.WriteLine("Skipped {0} albums.", skippedAlbums); // If no albums left to upload, end if (kodakAlbums.Count == 0) { Console.WriteLine(); Console.WriteLine("All albums have been uploaded to SmugMug"); Console.WriteLine(); } }
private bool SmugMugAlbumExists(KodakAlbum currentAlbum, out int smugAlbumId) { smugAlbumId = 0; string statusFile = string.Format("{0}\\Keg2Smug.{1}.status", temporaryLocation, currentAlbum.albumId); if (File.Exists(statusFile)) { using (FileStream fileStream = File.OpenRead(statusFile)) { // Read the smugmug albumId using (StreamReader reader = new StreamReader(fileStream)) { string smugMugAlbumId = reader.ReadLine(); if (!smugMugAlbumId.StartsWith("SmugMugAlbumId")) { Console.WriteLine("Unable to find SmugMug album id for album {0}", currentAlbum.albumName); return false; } string[] tokens = smugMugAlbumId.Split('|'); return int.TryParse(tokens[1], out smugAlbumId); } } } else { return false; } }
private void LoadExistingPhotoList(KodakAlbum currentAlbum, List<string> existingPhotoIds) { string statusFile = string.Format("{0}\\Keg2Smug.{1}.status", temporaryLocation, currentAlbum.albumId); FileStream fileStream = null; if (File.Exists(statusFile)) { fileStream = File.OpenRead(statusFile); } else { return; } using (fileStream) { // Read all photoIds from the file using (StreamReader reader = new StreamReader(fileStream)) { string photoId = reader.ReadLine(); while (photoId != null) { if (!photoId.StartsWith("SmugMugAlbumId")) { existingPhotoIds.Add(photoId); } photoId = reader.ReadLine(); } } fileStream.Close(); } }
/// <summary> /// Get the photo from Kodak /// </summary> private void GetPhoto(KodakAlbum currentAlbum, KodakPhoto currentPhoto) { // If the photo has already been fetched, do nothing if (File.Exists(string.Format("{0}\\{1}\\{2}.jpg", temporaryLocation, currentAlbum.albumId, currentPhoto.photoId))) { Console.WriteLine("already exists!"); return; } if (!testForKodakPremierMember) { // Check if the user is a premier kodak gallery user by trying to download the high-res photo ourselves TestForKodakPremierMember(currentAlbum, currentPhoto); } string fullPhotoUri = string.Format("http://www.kodakgallery.com/servlet/FullResDownload?collid={0}&photoid={1}", currentAlbum.albumId, currentPhoto.photoId); HttpStatusCode statusCode = HttpStatusCode.Unused; string errorString = null; try { HttpWebRequest photoRequest = (HttpWebRequest)WebRequest.Create(fullPhotoUri); photoRequest.Method = "GET"; photoRequest.UserAgent = kegUserAgent; photoRequest.CookieContainer = new CookieContainer(); photoRequest.CookieContainer.Add(this.kodakLoginCookies); using (HttpWebResponse photoResponse = (HttpWebResponse)photoRequest.GetResponse()) { if (photoResponse.StatusCode == HttpStatusCode.OK) { int contentLength = (int)photoResponse.ContentLength; byte[] buffer = new byte[contentLength]; int bytesRead = 0; using (Stream stream = photoResponse.GetResponseStream()) { while (bytesRead < contentLength) { int numBytesToRead = (contentLength - bytesRead) < 64000 ? (contentLength - bytesRead) : 64000; bytesRead += stream.Read(buffer, bytesRead, numBytesToRead); // Read 64KB chunks } Console.WriteLine("{0:D9}/{1} bytes....done!", bytesRead, contentLength); } string fileName = string.Format("{0}.jpg", currentPhoto.photoId); File.WriteAllBytes(string.Format("{0}\\{1}\\{2}.jpg", temporaryLocation, currentAlbum.albumId, currentPhoto.photoId), buffer); currentPhoto.fileName = fileName; } } } catch (WebException wex) { using (HttpWebResponse response = (HttpWebResponse)wex.Response) { statusCode = response.StatusCode; Console.WriteLine("Failed to fetch photo {0} in album {1} due to error {2}", currentPhoto.photoId, currentAlbum.albumId, statusCode); return; } } catch (Exception ex) { errorString = ex.ToString(); Console.WriteLine("Failed to fetch photo {0} in album {1} due to error {2}", currentPhoto.photoId, currentAlbum.albumId, errorString); return; } }
private void GetKodakAlbums() { // Get the complete list of albums HttpStatusCode statusCode; string albumCountString; string albumListUri = "http://www.kodakgallery.com/AlbumMenu.jsp?view=1&page=1&sort_order=2&navfolderid=0&albumsperpage=64&displayallyears=1"; if ((statusCode = HttpGet(albumListUri, kegUserAgent, out albumCountString)) != HttpStatusCode.OK) { Console.WriteLine("Unable to get list of albums from Kodak Easyshare Gallery - received response {0}", statusCode); Environment.Exit(2); } // Determine the count of albums Regex albumCountRegex = new Regex(@"All My Albums</a> \((?<AlbumCount>[0-9]+)\)", RegexOptions.Compiled | RegexOptions.IgnoreCase); Match albumCountMatch = albumCountRegex.Match(albumCountString); if (!albumCountMatch.Success) { Console.WriteLine("Unable to determine number of Kodak Easyshare Gallery albums"); Environment.Exit(13); } string albumCount = albumCountMatch.Groups[1].Captures[0].Value; int albumCountInt; if (!Int32.TryParse(albumCount, out albumCountInt)) { Console.WriteLine("Failed to parse albumcount from {0}", albumCount); Environment.Exit(3); } Console.WriteLine("Expect to get {0} albums from Kodak Easyshare Gallery", albumCountInt); // Figure out how many pages of albums we have int numberOfPages = (albumCountInt / 64) + ((albumCountInt % 64) > 0 ? 1 : 0); // Fetch each page of albums, extract the albumIds kodakAlbums = new List<KodakAlbum>(albumCountInt); for (int i = 1; i <= numberOfPages; i++) { albumListUri = string.Format("http://www.kodakgallery.com/AlbumMenu.jsp?view=1&sort_order=2&navfolderid=0&albumsperpage=64&displayallyears=1&page={0}", i); string albumList; if ((statusCode = HttpGet(albumListUri, kegUserAgent, out albumList)) != HttpStatusCode.OK) { Console.WriteLine("Failed to get album list for page {0} with statuscode {1}", i, statusCode); Environment.Exit(4); } // Use a regex to get all the albumids on the page Regex albumIdAndNameRegex = new Regex("<p class=\"albumname\"><a.*collid=([0-9]+\\.[0-9]+)\\.[0-9]+.*>(.*)</a></p>", RegexOptions.Compiled | RegexOptions.IgnoreCase); MatchCollection matches = albumIdAndNameRegex.Matches(albumList); Console.WriteLine("{0} albums found on page {1}", matches.Count, i); foreach (Match match in matches) { GroupCollection groups = match.Groups; string albumId = groups[1].Captures[0].Value; string albumName = groups[2].Captures[0].Value; albumName = HttpUtility.HtmlDecode(albumName); KodakAlbum album = new KodakAlbum(albumId, albumName); kodakAlbums.Add(album); } } if (kodakAlbums.Count != albumCountInt) { Console.WriteLine("AlbumCount is {0} but only got {1} albumss", albumCountInt, kodakAlbums.Count); Environment.Exit(5); } else { Console.WriteLine("Got all albums"); } }
private int UploadPhoto(KodakAlbum currentAlbum, KodakPhoto photo, int smugAlbumID) { string filePath = string.Format("{0}\\{1}\\{2}", temporaryLocation, currentAlbum.albumId, photo.fileName); SmugUploadResponse response; try { response = smClient.ImagesUploadBinary(filePath, smLogin.Session.id, 0, smugAlbumID.ToString(), null); return response.Image.id; } catch (Exception) { Console.WriteLine("Failed to upload image {0} to SmugMug", filePath); return 0; } /* NameValueCollection queryStringCollection = new NameValueCollection(); queryStringCollection.Add("method", "smugmug.images.uploadFromURL"); queryStringCollection.Add("SessionID", smClient.SessionID); queryStringCollection.Add("APIKey", API_KEY); queryStringCollection.Add("AlbumID", smugAlbumID.ToString()); queryStringCollection.Add("Caption", photo.photoCaption); string fullPhotoUri = string.Format("http://www.kodakgallery.com/servlet/FullResDownload?collid={0}&photoid={1}&UV={2}", currentAlbum.albumId, photo.photoId, uvkey); queryStringCollection.Add("URL", fullPhotoUri); string baseUri = "http://api.smugmug.com/hack/rest/1.2.0/"; string postBody = ""; foreach (string key in queryStringCollection.AllKeys) { if (postBody.Length > 0) { postBody = string.Format("{0}&{1}={2}", postBody, key, HttpUtility.UrlEncode(queryStringCollection[key])); } else { postBody = string.Format("{0}={1}", key, HttpUtility.UrlEncode(queryStringCollection[key])); } } string response = null; HttpStatusCode statusCode = HttpPost(baseUri, smugUserAgent, postBody, out response); if (statusCode == HttpStatusCode.OK) { Match match = imageIdRegex.Match(response); if (match.Success) { string imageIdString = match.Groups[1].Captures[0].Value; int imageId = int.Parse(imageIdString); return imageId; } else { Console.WriteLine("Could not parse imageId from SmugMug response for photo {0} in album {1}", photo.photoId, currentAlbum.albumId); Console.WriteLine("Response from SmugMug was {0}", response); return 0; } } else { Console.WriteLine("Failed to upload photo {0} in album {1}", photo.photoId, currentAlbum.albumId); Console.WriteLine("Response from server was {0}", statusCode); } return 0; */ }
private void UpdateSmugMugAlbumId(KodakAlbum currentAlbum, int smugAlbumId) { string statusFile = string.Format("{0}\\Keg2Smug.{1}.status", temporaryLocation, currentAlbum.albumId); using (FileStream fileStream = File.Create(statusFile)) { // Write smugmug albumid to the file using (StreamWriter writer= new StreamWriter(fileStream)) { writer.WriteLine("SmugMugAlbumId|{0}", smugAlbumId); } fileStream.Close(); } }