Code Monkey home page Code Monkey logo

newsapi's Introduction

NewsAPI

Version License Platform

Example

To run the example project, clone the repo, and run pod install from the Example directory first.

Usage

Initialization

Setup with your API key first.

let news: News = News(apiKey: "your-api-key")

Fetch top headlines

Returns breaking news headlines for a country and category, or currently running on a single or multiple sources.

let options: [QueryOptions] = [
    .country(.us),
    .pageSize(5),
    .sortBy(.popularity)
]
        
news.get(.topHeadlines, with: options, headlinesCompletion: { [weak self] (headlines, error) in
    if let headlines = headlines, let articles = headlines.articles {
	debugPrint(articles)
    }
})

Fetch everything

The NewsAPI indexes every recent news and blog article published by over 50,000 different sources large and small, and you can search through them with this endpoint.

let options: [QueryOptions] = [
    .domains(["bbc.co.uk", "bbc.com"]),
    .excludeDomains(["google.com"]),
    .country(.us)
]
        
news.get(.everything, with: options, headlinesCompletion: { [weak self] (headlines, error) in
    if let headlines = headlines, let articles = headlines.articles {
	debugPrint(articles)
    }
})

Fetch sources

Returns information (including name, description, and category) about the most notable sources the NewsAPI indexes.

let options: [QueryOptions] = [
    .language(.en),
    .category(.technology),
    .country(.us)
]
        
news.get(.sources, with: options, sourcesCompletion: { [weak self] (sources, error) in
    if let sources = sources {
	debugPrint(sources)
    }
})

Query Options

The available query options for filtering results.

/// Keywords or a phrase to search for.
case query(String)

/// Keywords or phrases to search for in the article title only.
case titleQuery(String)

/// The news sources or blogs you want headlines from.
case sources([String])

/// Domains (eg bbc.co.uk, techcrunch.com, engadget.com) to restrict the search to.
case domains([String])

/// Domains (eg bbc.co.uk, techcrunch.com, engadget.com) to remove from the results.
case excludeDomains([String])

/// A date and optional time for the oldest article allowed.
case fromDate(Date)

/// A date and optional time for the newest article allowed.
case toDate(Date)

/// Code of the language you want to get headlines for.
case language(Language)

/// The order to sort the articles in.
case sortBy(SortOptions)

/// The category you want to get headlines for.
case category(Category)

/// The code of the country you want to get headlines for.
case country(Country)

/// The number of results to return per page (request). 20 is the default, 100 is the maximum.
case pageSize(Int)

/// Use this to page through the results if the total results found is greater than the page size.
case page(Int)

Installation

NewsAPI is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod 'SwiftNewsAPI'

Author

imryan, [email protected]

License

NewsAPI is available under the MIT license. See the LICENSE file for more info.

newsapi's People

Contributors

imryan avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.