//===================================================================================== // Parsing Heandler Section: // Current Section is parsing XML and JSON objects //===================================================================================== /// <summary> /// Parse XML object /// </summary> /// <param name="param"></param> /// <returns></returns> private async Task parseByXMLDocument(string param) { // build search string string xmlUrl = "http://www.omdbapi.com/?s=" + param + "&r=xml"; logger.Information("Creating new object based on XML format. Link: {0}", xmlUrl); XmlDocument doc = new XmlDocument(); doc.Load(xmlUrl); XmlNodeList MovieNodeList = doc.SelectNodes("/root/result"); // check if xml file return anything if (MovieNodeList != null) { // pasre the xml object foreach (XmlNode node in MovieNodeList) { var Movie = new Movie(); Movie.Title = node.Attributes.GetNamedItem("Title").Value; Movie.Year = node.Attributes.GetNamedItem("Year").Value; Movie.imdbID = node.Attributes.GetNamedItem("imdbID").Value; Movie.Type = node.Attributes.GetNamedItem("Type").Value; if (node.Attributes["Poster"] != null) { Movie.Poster = node.Attributes.GetNamedItem("Poster").Value; } db.Movies.Add(Movie); await db.SaveChangesAsync(); var queueMessage = new CloudQueueMessage(Movie.MovieId.ToString()); await imagesQueue.AddMessageAsync(queueMessage); logger.Information("Created queue message for MovieId {0}", Movie.MovieId); } } else { logger.Error("Not valid request for XML object. Link: {0}", xmlUrl); } }
/// <summary> /// parseByJSONDocument Function: /// Parse JSON object /// </summary> /// <param name="temp"></param> /// <returns></returns> private async Task parseByJSONDocument(string temp) { // build search string string jsonUrl = "http://www.omdbapi.com/?s=" + temp + "&r=json"; logger.Information("Creating new object based on JSON format. Link: {0}", jsonUrl); // make web request to retrive json objects WebRequest request = WebRequest.Create(jsonUrl); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); // check for valid repsonce if ((response.StatusCode == HttpStatusCode.OK)) { string json; using (var sr = new StreamReader(response.GetResponseStream())) { json = sr.ReadToEnd(); } dynamic sData = Newtonsoft.Json.JsonConvert.DeserializeObject(json); // JObject sData = JObject.Parse(json); foreach (var item in sData["Search"]) { string title = (string)item["Title"]; string year = (string)item["Year"]; string imdbID = (string)item["imdbID"]; string type = (string)item["Type"]; string poster = (string)item["Poster"]; Movie movie = new Movie(); movie.Title = title; movie.Year = year; movie.imdbID = imdbID; movie.Type = type; movie.Poster = poster; db.Movies.Add(movie); await db.SaveChangesAsync(); var queueMessage = new CloudQueueMessage(movie.MovieId.ToString()); await imagesQueue.AddMessageAsync(queueMessage); logger.Information("Created queue message for MovieId {0}", movie.MovieId); } } else { logger.Error("Not valid request for JSON object. Link: {0}", jsonUrl); } }
//===================================================================================== // Blob Deleting Section: // Current Section takes care of deleting the blobs instance if image exists //===================================================================================== /// <summary> /// DeleteMovieBlobsAsync Fucntion: /// delete blob from the storage /// </summary> /// <param name="movie"></param> /// <returns></returns> private async Task DeleteMovieBlobsAsync(Movie movie) { if (!string.IsNullOrWhiteSpace(movie.Poster) && (movie.Poster !="N/A")) { Uri blobUri = new Uri(movie.Poster); await DeleteMovieBlobAsync(blobUri); } if (!string.IsNullOrWhiteSpace(movie.Poster) && (movie.Poster !="N/A")) if (!string.IsNullOrWhiteSpace(movie.Thumbnail)) { Uri blobUri = new Uri(movie.Thumbnail); await DeleteMovieBlobAsync(blobUri); } }