示例#1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="article">string do edycjii </param>
        /// <returns>edytowany string</returns>

        public static List <Articles> querySearch(string token, string query)
        {
            List <Articles> articles = new List <Articles>();
            RestClient      client   = new RestClient("https://newsapi.org/v2/");
            RestRequest     request  = new RestRequest("everything", Method.GET);

            request.AddParameter("apiKey", token);
            request.AddParameter("q", query);
            request.AddParameter("pageSize", 50);
            var    _response = client.Execute(request);
            string response  = _response.Content;
            var    settings  = new JsonSerializerSettings
            {
                NullValueHandling     = NullValueHandling.Ignore,
                MissingMemberHandling = MissingMemberHandling.Ignore
            };
            Headlines       headlines = JsonConvert.DeserializeObject <Headlines>(response, settings);
            List <Articles> _articles = headlines.articles;

            articles.AddRange(_articles);
            for (var i = 0; i < articles.Count; i++)
            {
                articles[i].content = cutEnd(articles[i].content);
            }
            addDescription(articles);
            return(articles);
        }
示例#2
0
        //apiKey=46491296cc1342edb76a89e49524896a


        /// <summary>
        ///
        /// </summary>
        /// <param name="token">apitoken</param>
        /// <param name="category">Lista kategorrii wybranych przez użytkownika</param>
        /// <returns>lista Articles</returns>
        public static List <Articles> getTopHeadlines(string token, List <string> category)
        {
            List <Articles> articles = new List <Articles>();

            for (var i = 0; i < category.Count; i++)
            {
                RestClient  client  = new RestClient("https://newsapi.org/v2/");
                RestRequest request = new RestRequest("top-headlines", Method.GET);
                request.AddParameter("apiKey", token);
                request.AddParameter("country", "us");
                request.AddParameter("pageSize", 50);
                request.AddParameter("category", category[i]);
                var    _response = client.Execute(request);
                string response  = _response.Content;
                var    settings  = new JsonSerializerSettings
                {
                    NullValueHandling     = NullValueHandling.Ignore,
                    MissingMemberHandling = MissingMemberHandling.Ignore
                };
                Headlines       headlines = JsonConvert.DeserializeObject <Headlines>(response, settings);
                List <Articles> _articles = headlines.articles;
                articles.AddRange(_articles);
            }

            articles = articles.OrderBy(a => Guid.NewGuid()).ToList();
            while (articles.Count > 50)
            {
                articles.RemoveAt(articles.Count - 1);
            }
            for (var i = 0; i < articles.Count; i++)
            {
                articles[i].content = cutEnd(articles[i].content);
            }
            return(articles);
        }