Exemplo n.º 1
0
        public static DkApiResult ImageLookUp(string word)
        {
            HttpResponseMessage response = ApiRequest(DK_URL + "?caption=" + word + "&apikey=" + DK_KEY);

            if (response.IsSuccessStatusCode)
            {
                string      jsonResult = response.Content.ReadAsStringAsync().Result;
                DkApiResult result     = JsonConvert.DeserializeObject <DkApiResult>(jsonResult);
                return(result);
            }
            else
            {
                throw new Exception("Failed getting result back from DK API. ImageLookUp failed. ");
            }
        }
Exemplo n.º 2
0
        // <Word, Definition, ImageUrl>
        public static List <Tuple <string, string, string> > GetWordDefImageList()
        {
            List <Tuple <string, string, string> > result = new List <Tuple <string, string, string> >();

            // get 4 random words
            List <string> fourWords = new List <string>();

            while (fourWords.Count < 4)
            {
                Random rnd        = new Random();
                string randomWord = WordRepo.Words[rnd.Next(0, WordRepo.Words.Count)];
                if (!fourWords.Contains(randomWord))
                {
                    fourWords.Add(randomWord);
                }
            }

            string imageUrl   = "";
            string definition = "";

            for (int i = 0; i < 4; i++)
            {
                // try cache first
                using (Models.Messi messi = new Models.Messi())
                {
                    string   word  = fourWords[i];
                    ApiCache cache = messi.ApiCaches.Where(a => a.Word.Equals(word)).FirstOrDefault();
                    if (cache != null)
                    {
                        imageUrl   = cache.ImageUrl;
                        definition = cache.Definition;
                    }
                    else
                    {
                        // get imageUrl from API.
                        DkApiResult dkResult = Helper.ImageLookUp(word);
                        if (dkResult.total < 1)
                        {
                            throw new Exception("Number of images for this word is less than 1. Word: " + word);
                        }
                        else
                        {
                            imageUrl = dkResult.images[0].url;
                        }

                        // get definition from API
                        JObject lmResult = Helper.DefinitionLookUpObj(word);
                        JToken  lmEntry  = (lmResult["Entries"]["Entry"] is JArray) ? lmResult["Entries"]["Entry"][0] : lmResult["Entries"]["Entry"];
                        JToken  lmSense  = (lmEntry["Sense"] is JArray) ? lmEntry["Sense"][0] : lmEntry["Sense"];
                        JToken  lmDEF    = lmSense["DEF"];
                        if (lmDEF == null)
                        {
                            JToken lmSubsense = (lmSense["Subsense"] is JArray) ? lmSense["Subsense"][0] : lmSense["Subsense"];
                            lmDEF = lmSubsense["DEF"];
                        }
                        definition = lmDEF["#text"].ToString();

                        // cache it
                        messi.ApiCaches.Add(new ApiCache()
                        {
                            Word       = word,
                            ImageUrl   = imageUrl,
                            Definition = definition
                        });
                        messi.SaveChanges();
                    }
                    // done. <Word, Definition, ImageUrl>
                    result.Add(new Tuple <string, string, string>(word, definition, imageUrl));
                }
            }
            return(result);
        }