using System; using System.Collections.Generic; using System.Linq; using sharpknife.Controls; using System.Windows.Navigation; using System.Threading; using System.Drawing; using System.Net; #if WIN32 using MSHTML; #else using mshtml; #endif using System.Runtime.InteropServices; using sharpknife.Utils; using sharpknife.Strategies; using System.IO; using System.Xml; using System.Xml.Linq; using System.Diagnostics; namespace sharpknife.Commands { public class PetFinderCommand : Command { // API Key // edc5049bd035dd7f2ac78691a8ff1253 public static string APIKey = "edc5049bd035dd7f2ac78691a8ff1253"; // API Secret // 73963b0186372806ca3d5829fa2a2514 // API Status // Active //http://api.petfinder.com/my.method?key=12345&arg1=foo&token=67890&sig=abcdef //pet.get //Returns a record for a single pet. //ARGUMENTS //Name Type Required? Description //key string required your developer key //token string optional session token //id integer required pet ID //format string optional (default=xml) Response format: xml, json //XML return type: petfinderPetRecord //| Back to Top | //pet.getRandom //Returns a record for a randomly selected pet. You can choose the characteristics of the pet you want returned using the various arguments to this method. //This method can return pet records in three formats: //id: just the pet ID //basic: essential information like name, animal, breed, shelter ID, primary photo //full: the complete pet record //ARGUMENTS //Name Type Required? Description //key string required your developer key //token string optional session token //animal string optional type of animal (barnyard, bird, cat, dog, horse, pig, reptile, smallfurry) //breed string optional breed of animal (use breeds.list for a list of valid breeds) //size string optional size of animal (S=small, M=medium, L=large, XL=extra-large) //sex character optional M=male, F=female //location string optional the ZIP/postal code or city and state the animal should be located (NOTE: the closest possible animal will be selected) //shelterid string optional ID of the shelter that posted the pet //output string optional (default=id) How much of the pet record to return: id, basic, full //format string optional (default=xml) Response format: xml, json //XML return type: petfinderPetRecord //pet.find //Searches for pets according to the criteria you provde and returns a collection of pet records matching your search. The results will contain at most count records per query, and a lastOffset tag. To retrieve the next result set, use the lastOffset value as the offset to the next pet.find call. //NOTE: the total number of records you are allowed to request may vary depending on the type of developer key you have. //ARGUMENTS //Name Type Required? Description //key string required your developer key //token string optional session token //animal string optional type of animal (barnyard, bird, cat, dog, horse, pig, reptile, smallfurry) //breed string optional breed of animal (use breed.list for a list of valid breeds) //size string optional size of animal (S=small, M=medium, L=large, XL=extra-large) //sex character optional M=male, F=female //location string required the ZIP/postal code or city and state where the search should begin //age string optional age of the animal (Baby, Young, Adult, Senior) //offset string optional set this to the value of lastOffset returned by a previous call to pet.find, and it will retrieve the next result set //count integer optional how many records to return for this particular API call (default is 25) //output string optional (default=basic) How much of each record to return: basic (no description) or full (includes description) //format string optional (default=xml) Response format: xml, json //XML return type: petfinderPetRecordList public PetFinderCommand() : base() { this.Group = "Web"; this.Timeout = 45; } // // //
// 0.1 // 2012-12-12T16:37:10Z // // 100 // // //
// 25 // // // 24258975 // AL293 // // Chess // Cat // // Domestic Short Hair // // no // Baby // F // S // // // // // Chess is a little bitty kitten with a big personality. No one told her how small she is. She is outgoing, friendly, and loves to play and interact with others. She is a special girl that would be a great addition to almost any home.]]> // 2012-11-20T19:10:34Z // A // // // http://photos.petfinder.com/photos/US/AL/AL293/24258975/AL293.24258975-1-x.jpg // http://photos.petfinder.com/photos/US/AL/AL293/24258975/AL293.24258975-1-fpm.jpg // http://photos.petfinder.com/photos/US/AL/AL293/24258975/AL293.24258975-1-pn.jpg // http://photos.petfinder.com/photos/US/AL/AL293/24258975/AL293.24258975-1-pnt.jpg // http://photos.petfinder.com/photos/US/AL/AL293/24258975/AL293.24258975-1-t.jpg // http://photos.petfinder.com/photos/US/AL/AL293/24258975/AL293.24258975-2-x.jpg // http://photos.petfinder.com/photos/US/AL/AL293/24258975/AL293.24258975-2-fpm.jpg // http://photos.petfinder.com/photos/US/AL/AL293/24258975/AL293.24258975-2-pn.jpg // http://photos.petfinder.com/photos/US/AL/AL293/24258975/AL293.24258975-2-pnt.jpg // http://photos.petfinder.com/photos/US/AL/AL293/24258975/AL293.24258975-2-t.jpg // http://photos.petfinder.com/photos/US/AL/AL293/24258975/AL293.24258975-3-x.jpg // http://photos.petfinder.com/photos/US/AL/AL293/24258975/AL293.24258975-3-fpm.jpg // http://photos.petfinder.com/photos/US/AL/AL293/24258975/AL293.24258975-3-pn.jpg // http://photos.petfinder.com/photos/US/AL/AL293/24258975/AL293.24258975-3-pnt.jpg // http://photos.petfinder.com/photos/US/AL/AL293/24258975/AL293.24258975-3-t.jpg // // // // 1826 Carl Jones Rd. // // Moody // AL // 35004 // 205-640-4327 // 205-640-4367 // crossroadsanimalhospital@yahoo.com // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // public override void Execute() { base.Execute(); //HttpWebRequest request = (HttpWebRequest)WebRequest.Create( // string.Format("http://api.petfinder.com/pet.find?key={0}&animal={1}&location={2}", APIKey, "cat", "35004")); //HttpWebRequest request = (HttpWebRequest)WebRequest.Create( // string.Format("http://api.petfinder.com/pet.getRandom?key={0}&animal={1}&output={2}", APIKey, "cat", "basic")); int id = 1; // !1000 for (id = 24570913 - 1; id < 24570913 + 1000; id++) { Debug = id.ToString(); HttpWebRequest request = (HttpWebRequest)WebRequest.Create( string.Format("http://api.petfinder.com/pet.get?key={0}&id={1}", APIKey, id)); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); StreamReader responseStream = new StreamReader(response.GetResponseStream()); XDocument document = XDocument.Load(responseStream); // //
// 0.1 // 2013-01-11T13:34:49Z // // 201 // pet id 1 not found // //
//
//Code Name Notes //100 PFAPI_OK No error //200 PFAPI_ERR_INVALID Invalid request //201 PFAPI_ERR_NOENT Record does not exist //202 PFAPI_ERR_LIMIT A limit was exceeded //203 PFAPI_ERR_LOCATION Invalid geographical location //300 PFAPI_ERR_UNAUTHORIZED Request is unauthorized //301 PFAPI_ERR_AUTHFAIL Authentication failure //999 PFAPI_ERR_INTERNAL Generic internal error var code = (from c in document.Descendants("code") select c).FirstOrDefault().Value; if (code == "201") { continue; } else if (code == "100") { /* NOOP */ } else if (code == "300") { /* NOOP */ } else { Debugger.Break(); } var pets = (from pet in document.Descendants("pet") select pet); foreach (var pet in pets) { id = int.Parse((from i in document.Descendants("id") select i).FirstOrDefault().Value); if (StringUtils.Contains((from a in document.Descendants("animal") select a).FirstOrDefault().Value, "cat")) { // Image size 128x96 var photos = ( from photo in pet.Descendants("photo") where photo.Attribute("size").Value == "x" select new { Photo = photo.Value, Id = photo.Attribute("id").Value }); foreach (var photo in photos) { Bitmap bitmap = ImageUtils.BitmapFromWeb(photo.Photo); string path = string.Format(@"..\..\Images\Captcha\PetFinder"); string filename = string.Format("{0}-{1}.png", id, photo.Id); bitmap.Save(Path.Combine(path, filename)); } } } responseStream.Close(); response.Close(); } Status = StatusCode.Completed; } } }