public async Task <ActionResult> Index3() { // init with your API keyGetEverythingAsync var newsApiClient = new NewsApiClient("acffc7a435f24838a883e692c0fa44dc"); var articlesResponse = newsApiClient.GetEverythingAsync(new EverythingRequest { // Q = "Apple", SortBy = SortBys.PublishedAt, Language = Languages.TR, From = new DateTime(2021, 4, 19) }).GetAwaiter().GetResult(); List <News> mynews = new List <News>(); foreach (var article in articlesResponse.Articles) { News news = new News(); news.Author = article.Author; news.Content = article.Content; news.Title = article.Title; news.Url = article.Url; news.UrlToImage = article.UrlToImage; news.PublishedAt = article.PublishedAt; mynews.Add(news); } return(await Task.Run(() => View(mynews))); }
/// <summary> /// Queries news articles from all sources /// Grabs one containing "Donald Trump", one containing "republicans" /// stores to public vars /// </summary> /// <returns>NewsArticles struct</returns> public static async Task GetNewsAsync() { // Add your NewsAPI key here var newsApiClient = new NewsApiClient("<API KEY>"); var timeSpan = new TimeSpan(25, 0, 0, 0); var date = DateTime.Now.Subtract(timeSpan); var articlesResponse = await newsApiClient.GetEverythingAsync(new EverythingRequest { Q = "Trump", SortBy = SortBys.Popularity, PageSize = 100, From = date }); if (articlesResponse.Status == Statuses.Ok) { Vars.NewsArticles.Negative = articlesResponse.Articles[Vars.Rand.Next(articlesResponse.Articles.Count)].Title.ToLower(); } else { Vars.NewsArticles.Negative = "Trump tweets something mean"; } articlesResponse = await newsApiClient.GetEverythingAsync(new EverythingRequest { Q = "democrats", SortBy = SortBys.Popularity, PageSize = 100, From = date }); if (articlesResponse.Status == Statuses.Ok) { Vars.NewsArticles.Positive = articlesResponse.Articles[Vars.Rand.Next(articlesResponse.Articles.Count)].Title.ToLower(); } else { Vars.NewsArticles.Positive = "Hillary is really good"; } return; }
public async Task <List <Article> > getNoticia() { var newsApiClient = new NewsApiClient("3f608f568658410c81829fa44de7f860"); var articlesResponse = await newsApiClient.GetEverythingAsync(new EverythingRequest { Q = "Angola Coronavirus", SortBy = SortBys.Popularity, Language = Languages.PT, From = DateTime.Now.AddDays(-29) }); if (articlesResponse.Status == Statuses.Ok) { } return(articlesResponse.Articles.ToList()); }
public async Task <ArticlesResult> GetNewsSearchResult(int languageId, int sortById, string q, DateTime fromDate) { var newsApiClient = new NewsApiClient(ApiKey); ArticlesResult articlesResponse = await newsApiClient.GetEverythingAsync(new EverythingRequest { Q = q, SortBy = (SortBys)sortById, Language = (Languages)languageId, From = fromDate }); if (articlesResponse.Status == Statuses.Ok) { return(articlesResponse); } return(null); }
public async Task <IActionResult> GetNewsEverything([FromQuery] EverythingRequest requestEverything) { var invokingUser = int.Parse(User.FindFirst(claim => claim.Type == ClaimTypes.NameIdentifier).Value); var newsApiClient = new NewsApiClient(_configuration.GetSection("ApiKey").Value); var articlesResponse = await newsApiClient.GetEverythingAsync(requestEverything); if (articlesResponse.Status == Statuses.Ok) { var queryObject = _mapper.Map <EverythingRequest, QueryObjectEverythingRequest>(requestEverything); await _repository.PostQueryObjectEverythingRequestAsync(invokingUser, queryObject); var articlesResponseDto = _mapper.Map <ArticlesResult, ArticlesResultViewModel>(articlesResponse); return(Ok(articlesResponseDto)); } return(BadRequest(articlesResponse.Error.Message)); }
/// <summary> /// Retrieves the first 20 top headlines from NewsAPI /// </summary> /// <returns>List of articles</returns> public static async Task <List <Article> > RetrieveNewsAsync() { var newsApiClient = new NewsApiClient(Resource.News_API_Key); var response = await newsApiClient.GetEverythingAsync(new EverythingRequest() { Q = "*", From = DateTime.Now.AddDays(-1), Language = Languages.EN, PageSize = 20 }); if (response.Status != Statuses.Ok) { throw new Exception(response.Error.Message); } return(response.Articles); }
/* -------------------- News API -------------------- */ public async Task <IActionResult> News() { if (_currentUserId == 0) { return(View("Errors")); } var newsApiClient = new NewsApiClient("465ab82acc5b43999381d54823215a61"); var articlesResponse = await newsApiClient.GetEverythingAsync(new EverythingRequest { Q = "Budget, Budgeting, Finance", SortBy = SortBys.Relevancy, Language = Languages.EN }); return(View("News", articlesResponse)); }
public async Task <JsonResult> Noticias() { var newsApiClient = new NewsApiClient("3f608f568658410c81829fa44de7f860"); var articlesResponse = await newsApiClient.GetEverythingAsync(new EverythingRequest { Q = "Covid 19", SortBy = SortBys.Popularity, Language = Languages.PT, From = DateTime.Now.AddDays(-29) }); if (articlesResponse.Status == Statuses.Ok) { } foreach (var i in articlesResponse.Articles.ToList()) { noticias.Add(new NoticiaCopia { Id = i.Url, Fonte = i.Source.Name, Imagem = i.UrlToImage, Data = (DateTime)i.PublishedAt, Descricao = i.Description, Tema = i.Title }); } return(Json(noticias, JsonRequestBehavior.AllowGet)); }
public async Task <ArticlesResult> GetAll(string query) { // get user settings, this should be using the current user Id in the future UserSettings settings = await userSettingsService.Get(); ArticlesResult articlesResponse; if (query == null) { // We get the top headlines according to the user settings articlesResponse = await newsApi.GetTopHeadlinesAsync(new TopHeadlinesRequest { Sources = settings.SourcesIds.Split(',').ToList(), Language = (Languages)Enum.Parse(typeof(Languages), settings.LanguageCd.ToUpper()), Page = settings.Page, PageSize = settings.PageSize }); } else { articlesResponse = await newsApi.GetEverythingAsync(new EverythingRequest { Q = query, Sources = settings.SourcesIds.Split(',').ToList(), SortBy = settings.SortBy, Language = (Languages)Enum.Parse(typeof(Languages), settings.LanguageCd.ToUpper()), From = new DateTime(2018, 1, 25) }); } if (articlesResponse.Status == Statuses.Error) { // handle error return(null); } return(articlesResponse); }
public async Task <ArticlesResult> GetNewsByTopicAsync(string topic, List <string> domains, List <string> sources, DateTime?from, DateTime?to, Languages?language) { var request = new EverythingRequest(); if (!string.IsNullOrEmpty(topic) && !string.IsNullOrWhiteSpace(topic)) { request.Q = topic; } if (domains != null) { request.Domains = domains; } if (sources != null) { request.Sources = sources; } request.From = from; request.To = to; request.Language = language; request.SortBy = SortBys.Relevancy; return(await newsApiClient.GetEverythingAsync(request)); }
public static async Task <IEnumerable <ArticleViewModel> > GetSearchResults(string criteria, string language) { try { var lang = Enum.Parse <Languages>(language.ToUpper()); var articlesResponse = await newsApiClient.GetEverythingAsync(new EverythingRequest { Q = criteria, Language = lang, PageSize = 100 }); return(articlesResponse.Articles.MapToListArticleViewModel()); } catch (ArgumentNullException) { throw new ArgumentNullException(); } catch (Exception) { throw new InvalidOperationException(); } }
/// <summary> /// This method tries general search of articles /// Pass an array of keywords as parameter /// Please just don't pass a huge array 🙄, 5 items at most /// </summary> /// <returns>List of Articles</returns> public static async Task <List <Models.Article> > SearchArticles(string[] keywords, int page = 1) { /// please keep the limit of keywords to 5 int pageSize; if (keywords.Length > 5) { throw new ArgumentOutOfRangeException("Can you just keep a maximum of 5 keywords? Okay? Great :)"); } switch (keywords.Length) { case 1: pageSize = 50; break; case 2: pageSize = 25; break; case 3: pageSize = 15; break; case 4: pageSize = 12; break; case 5: pageSize = 10; break; default: pageSize = 50; break; } articles = new List <Models.Article>(); try { foreach (var keyword in keywords) { var articlesResponse = await newsApiClient.GetEverythingAsync(new EverythingRequest { Q = keyword, Page = page, PageSize = pageSize, // Switch this if you want to sort by popularity Language = Languages.EN, // Switch language to your preference }); if (articlesResponse.Status == Statuses.Ok) { articlesResponse.Articles.Reverse(); foreach (var article in articlesResponse.Articles) { var postition = articlesResponse.Articles.IndexOf(article); articles.Add(new Models.Article { Author = article.Author, Description = article.Description, PublishedAt = article.PublishedAt, Source = $"Source: {article.Source.Name} ", Title = article.Title, Url = article.Url, UrlToImage = article.UrlToImage, IsWideView = IsWideView(postition) }); } } } } catch (Exception ex) { Debug.WriteLine(ex.Message); } return(articles); }
public static async Task <IEnumerable <Article> > GetArticlesAsync(EverythingRequest request) { var articles = await client.GetEverythingAsync(request); return(articles.Articles.AsEnumerable()); }
/// <summary> /// This method tries general search of articles /// Pass an array of keywords as parameter /// Please just don't pass a huge array 🙄, 5 items at most /// </summary> /// <returns>List of Articles</returns> public static async Task <List <Article> > SearchArticles(string [] keywords, int page = 1) { /// please keep the limit of keywords to 5 int pageSize; if (keywords.Length > 5) { throw new ArgumentOutOfRangeException("Can you just keep a maximum of 5 keywords? Okay? Great :)"); } switch (keywords.Length) { case 1: pageSize = 50; break; case 2: pageSize = 25; break; case 3: pageSize = 15; break; case 4: pageSize = 12; break; case 5: pageSize = 10; break; default: pageSize = 50; break; } List <Article> articles = null; try { foreach (var keyword in keywords) { var articlesResponse = await newsApiClient.GetEverythingAsync(new EverythingRequest { Q = keyword, Page = page, PageSize = pageSize, SortBy = SortBys.PublishedAt, // Switch this if you want to sort by popularity Language = Languages.EN, From = new DateTime(2018, 1, 25) // Enter a date you're comfortable with }); if (articlesResponse.Status == Statuses.Ok) { foreach (var article in articlesResponse.Articles) { articles.Add(article); } } } } catch (Exception ex) { Debug.WriteLine(ex.Message); } return(articles); }