Exemplo n.º 1
0
        public static async Task <HNQuestionsResult> ProfileSearch(int page, int id, string type)
        {
            string query = _host + "/profiles/" + id + "/" + type + "?page=" + page;
            ApplicationDataContainer localSettings = ApplicationData.Current.LocalSettings;
            HttpClient http = new HttpClient();

            http.DefaultRequestHeaders.Add("Authorization", "Token token=\"" + localSettings.Values["API_Token"] + "\"");

            var response = await http.GetAsync(query);

            var jsonMessage = await response.Content.ReadAsStringAsync();

            var result = JsonConvert.DeserializeObject <HNQuestionsResult>(jsonMessage);

            foreach (var question in result.questions)
            {
                question.text = SetQuestionText(question);

                question.user.native_language = DecodeService.DecodeLanguage((int)question.user.native_languages[0].language_id);

                if (question.user.image_url != "missing_thumb.png")
                {
                    question.user.profile_image = new BitmapImage {
                        UriSource = new Uri(question.user.image_url)
                    };
                }
                else
                {
                    question.user.profile_image = new BitmapImage {
                        UriSource = _missingImage
                    };
                }
            }
            return(result);
        }
Exemplo n.º 2
0
        public static async Task <HNQuestionRoot> LoadQuestionAndAnswers(int questionID)
        {
            string query = _host + "questions/" + questionID;
            ApplicationDataContainer localSettings = ApplicationData.Current.LocalSettings;
            HttpClient http = new HttpClient();

            http.DefaultRequestHeaders.Add("Authorization", "Token token=\"" + localSettings.Values["API_Token"] + "\"");

            var res = await http.GetAsync(query);

            var json = await res.Content.ReadAsStringAsync();

            var result = JsonConvert.DeserializeObject <HNQuestionRoot>(json);

            result.question.text = SetQuestionText(result.question);

            result.question.user.native_language = result.question.user.native_languages[0].language_id != null?
                                                   DecodeService.DecodeLanguage((int)result.question.user.native_languages[0].language_id) : "";

            if (result.question.user.image_url != "missing_thumb.png")
            {
                result.question.user.profile_image = new BitmapImage {
                    UriSource = new Uri(result.question.user.image_url)
                };
            }
            else
            {
                result.question.user.profile_image = new BitmapImage {
                    UriSource = new Uri("ms-appx:/Assets/icon_h120.png")
                };
            }

            foreach (var answer in result.question.answers)
            {
                if (answer.user.name != "Deleted account")
                {
                    answer.user.native_language = DecodeService.DecodeLanguage((int)answer.user.native_languages[0].language_id);
                    if (answer.user.image_url != "missing_thumb.png")
                    {
                        answer.user.profile_image = new BitmapImage {
                            UriSource = new Uri(answer.user.image_url)
                        };
                    }
                    else
                    {
                        answer.user.profile_image = new BitmapImage {
                            UriSource = new Uri("ms-appx:/Assets/icon_h120.png")
                        };
                    }
                }
            }
            return(result);
        }
Exemplo n.º 3
0
        public static string GetBodyText(HNActivity ac)
        {
            switch (ac.payload.type)
            {
            case "MeaningQuestion":
                ac.payload.text = "What does \"" +
                                  ac.payload.keywords[0].name +
                                  "\" mean?";
                break;

            case "ChoiceQuestion":
                ac.payload.text = "Does \"" +
                                  ac.payload.content +
                                  "\" sound natural?";
                break;

            case "DifferenceQuestion":
                ac.payload.text = "What is the difference between \"" +
                                  ac.payload.keywords[0].name +
                                  "\" and \"" +
                                  ac.payload.keywords[1].name +
                                  "\"?";
                // This needs to support more than two options!!!
                break;

            case "WhatsayQuestion":
                ac.payload.text = "How do you say \"" +
                                  ac.payload.keywords[0].name +
                                  "\" in " +
                                  DecodeService.DecodeLanguage((int)ac.payload.language_id) +
                                  "?";
                // Decode language
                break;

            case "ExampleQuestion":
                ac.payload.text = "Please show me example sentences with \"" +
                                  ac.payload.keywords[0].name + "\"";
                // Decode language
                break;

            case "FreeQuestion":
                ac.payload.text = ac.payload.content.ToString();
                // Decode language
                break;

            case "CountryQuestion":
                ac.payload.text = ac.payload.content.ToString();
                // Decode language
                break;
            }
            return(ac.payload.text);
        }
Exemplo n.º 4
0
        public static string SetQuestionText(HNQuestion question)
        {
            switch (question.type)
            {
            case "MeaningQuestion":
                question.text = "What does \"" +
                                question.keywords[0].name +
                                "\" mean?";
                break;

            case "ChoiceQuestion":
                question.text = "Does \"" +
                                question.content +
                                "\" sound natural?";
                break;

            case "DifferenceQuestion":
                question.text = "What is the difference between \"" +
                                question.keywords[0].name +
                                "\" and \"" +
                                question.keywords[1].name +
                                "\"?";
                // This needs to support more than two options!!!
                break;

            case "WhatsayQuestion":
                question.text = "How do you say \"" +
                                question.keywords[0].name +
                                "\" in " +
                                DecodeService.DecodeLanguage((int)question.language_id) +
                                "?";
                // Decode language
                break;

            case "ExampleQuestion":
                question.text = "Please show me example sentences with \"" +
                                question.keywords[0].name + "\"";
                // Decode language
                break;

            case "FreeQuestion":
                question.text = question.content;
                // Decode language
                break;

            case "CountryQuestion":
                question.text = question.content;
                // Decode language
                break;
            }
            return(question.text);
        }
Exemplo n.º 5
0
        public static async Task <HNUserProfile> LoadProfile(int profileID)
        {
            string query = _host + "profiles/" + profileID;
            ApplicationDataContainer localSettings = ApplicationData.Current.LocalSettings;

            HttpClient http = new HttpClient();

            http.DefaultRequestHeaders.Add("Authorization", "Token token=\"" + localSettings.Values["API_Token"] + "\"");
            var res = await http.GetAsync(query);

            var json = await res.Content.ReadAsStringAsync();

            var result = JsonConvert.DeserializeObject <HNUserResult>(json);

            #region Format
            foreach (var language in result.profile.user_attributes.study_languages_attributes)
            {
                language.language_name = DecodeService.DecodeLanguage((int)language.language_id);
            }
            foreach (var language in result.profile.user_attributes.native_languages_attributes)
            {
                language.language_name = DecodeService.DecodeLanguage((int)language.language_id);
            }
            foreach (var country in result.profile.user_attributes.user_interested_countries_attributes)
            {
                country.country_name = DecodeService.DecodeCountry((int)country.country_id);
            }
            if (result.profile.image_url == "missing_thumb.png")
            {
                result.profile.profile_image = new BitmapImage {
                    UriSource = _missingImage
                };
            }
            else
            {
                result.profile.profile_image = new BitmapImage {
                    UriSource = new Uri(result.profile.image_url)
                };
            }
            #endregion

            return(result.profile);
        }
Exemplo n.º 6
0
        public static int PopulateQuestions(int queryID, int pageNumber, ObservableCollection <HNQuestion> questions, bool append, bool language)
        {
            var questionsDataWrapper = new HNQuestionsResult();
            var results = new ObservableCollection <HNQuestion>();

            #region PriorityQuestions
            if (!append)
            {
                questionsDataWrapper = LoadQuestions(queryID, 1, 1, language);
                results = questionsDataWrapper.questions;
                foreach (var question in results)
                {
                    question.text = SetQuestionText(question);

                    question.user.native_language = question.user.native_languages[0].language_id != null?
                                                    DecodeService.DecodeLanguage((int)question.user.native_languages[0].language_id) : "";

                    if (question.user.image_url != "missing_thumb.png")
                    {
                        question.user.profile_image = new BitmapImage {
                            UriSource = new Uri(question.user.image_url)
                        };
                    }
                    else
                    {
                        question.user.profile_image = new BitmapImage {
                            UriSource = new Uri("ms-appx:/Assets/icon_h120.png")
                        };
                    }
                    questions.Add(question);
                }
            }
            Debug.WriteLine("Loaded premium questions");
            #endregion

            #region PlebQuestions
            questionsDataWrapper = LoadQuestions(queryID, pageNumber, 0, language);
            var maxPages = questionsDataWrapper.pagination.total_pages;
            results = questionsDataWrapper.questions;

            foreach (var question in results)
            {
                if (question.user.name != "Deleted account")
                {
                    question.text = SetQuestionText(question);

                    question.user.native_language = question.user.native_languages != null?
                                                    DecodeService.DecodeLanguage((int)question.user.native_languages[0].language_id) : "";

                    if (question.user.image_url != "missing_thumb.png")
                    {
                        question.user.profile_image = new BitmapImage {
                            UriSource = new Uri(question.user.image_url)
                        };
                    }
                    else
                    {
                        question.user.profile_image = new BitmapImage {
                            UriSource = new Uri("ms-appx:/Assets/icon_h120.png")
                        };;
                    }

                    questions.Add(question);
                }
                else
                {
                    break;
                }
            }
            Debug.WriteLine("Loaded normal questions");
            return(maxPages);

            #endregion
        }