Current section
2 Versions
Jump to
Current section
2 Versions
Compare versions
6
files changed
+179
additions
-6
deletions
| @@ -6,20 +6,19 @@ Source: tommasop/news_api | |
| 6 6 | |
| 7 7 | ## Installation |
| 8 8 | |
| 9 | - If [available in Hex](https://hex.pm/docs/publish), the package can be installed |
| 10 | - by adding `news_api` to your list of dependencies in `mix.exs`: |
| 9 | + The package can be installed by adding `news_api_ex` to your list of dependencies in `mix.exs`: |
| 11 10 | |
| 12 11 | ```elixir |
| 13 12 | def deps do |
| 14 13 | [ |
| 15 | - {:news_api, "~> 0.1.0"} |
| 14 | + {:news_api_ex, "~> 0.1.0"} |
| 16 15 | ] |
| 17 16 | end |
| 18 17 | ``` |
| 19 18 | |
| 20 19 | Documentation can be generated with [ExDoc](https://github.com/elixir-lang/ex_doc) |
| 21 20 | and published on [HexDocs](https://hexdocs.pm). Once published, the docs can |
| 22 | - be found at [https://hexdocs.pm/news_api](https://hexdocs.pm/news_api). |
| 21 | + be found at [https://hexdocs.pm/news_api_ex](https://hexdocs.pm/news_api_ex). |
| 23 22 | |
| 24 23 | ## Usage |
| @@ -26,4 +26,4 @@ | |
| 26 26 | {<<"optional">>,false}, |
| 27 27 | {<<"repository">>,<<"hexpm">>}, |
| 28 28 | {<<"requirement">>,<<"~> 1.1">>}]]}. |
| 29 | - {<<"version">>,<<"0.1.0">>}. |
| 29 | + {<<"version">>,<<"0.2.0">>}. |
| @@ -1,2 +1,6 @@ | |
| 1 1 | defmodule NewsApi do |
| 2 | + @moduledoc """ |
| 3 | + Interface to the static endpoints of the News API. |
| 4 | + See the [API docs](https://newsapi.org/docs) for more information. |
| 5 | + """ |
| 2 6 | end |
| @@ -3,6 +3,119 @@ defmodule NewsApi.Everything do | |
| 3 3 | import NewsApi.Base |
| 4 4 | @url_part "/everything" |
| 5 5 | |
| 6 | + @doc """ |
| 7 | + Searches all the news with available parameters. |
| 8 | + |
| 9 | + q: Keywords or phrases to search for in the article title and body. |
| 10 | + |
| 11 | + Advanced search is supported here: |
| 12 | + |
| 13 | + Surround phrases with quotes (") for exact match. |
| 14 | + Prepend words or phrases that must appear with a + symbol. Eg: +bitcoin |
| 15 | + Prepend words that must not appear with a - symbol. Eg: -bitcoin |
| 16 | + Alternatively you can use the AND / OR / NOT keywords, and optionally group these with parenthesis. Eg: crypto AND (ethereum OR litecoin) NOT bitcoin. |
| 17 | + |
| 18 | + The complete value for q must be URL-encoded. |
| 19 | + |
| 20 | + qInTitle: Keywords or phrases to search for in the article title only. |
| 21 | + |
| 22 | + Advanced search is supported here: |
| 23 | + |
| 24 | + Surround phrases with quotes (") for exact match. |
| 25 | + Prepend words or phrases that must appear with a + symbol. Eg: +bitcoin |
| 26 | + Prepend words that must not appear with a - symbol. Eg: -bitcoin |
| 27 | + Alternatively you can use the AND / OR / NOT keywords, and optionally group these with parenthesis. Eg: crypto AND (ethereum OR litecoin) NOT bitcoin. |
| 28 | + |
| 29 | + The complete value for qInTitle must be URL-encoded. |
| 30 | + |
| 31 | + sources: A comma-seperated string of identifiers (maximum 20) for the news sources or blogs you want headlines from. Use the /sources endpoint to locate these programmatically or look at the sources index. |
| 32 | + |
| 33 | + domains: A comma-seperated string of domains (eg bbc.co.uk, techcrunch.com, engadget.com) to restrict the search to. |
| 34 | + |
| 35 | + excludeDomains: A comma-seperated string of domains (eg bbc.co.uk, techcrunch.com, engadget.com) to remove from the results. |
| 36 | + |
| 37 | + from: A date for the oldest article allowed. This should be in ISO 8601 format (e.g. 2020-01-30) Default: the oldest according to your plan. |
| 38 | + |
| 39 | + to: A date for the newest article allowed. This should be in ISO 8601 format (e.g. 2020-01-30) Default: the newest according to your plan. |
| 40 | + |
| 41 | + language: The 2-letter ISO-639-1 code of the language you want to get headlines for. Possible options: ar de en es fr he it nl no pt ru se ud zh. Default: all languages returned. |
| 42 | + |
| 43 | + sortBy: The order to sort the articles in. Possible options: relevancy, popularity, publishedAt. Default: publishedAt |
| 44 | + |
| 45 | + pageSize: int The number of results to return per page. 20 is the default, 100 is the maximum. |
| 46 | + |
| 47 | + page: int Use this to page through the results. |
| 48 | + |
| 49 | + ## Example |
| 50 | + iex> NewsApi.Everything.search(%{ q: 'bitcoin', |
| 51 | + sources: 'bbc-news,the-verge', |
| 52 | + domains: 'bbc.co.uk,techcrunch.com', |
| 53 | + from: '2017-12-01', |
| 54 | + to: '2017-12-12', |
| 55 | + language: 'en', |
| 56 | + sortBy: 'relevancy', |
| 57 | + page: 2}, "your_api_key") |
| 58 | + {:ok, |
| 59 | + { |
| 60 | + "status" => "ok", |
| 61 | + "totalResults" => 3434, |
| 62 | + "articles" => [ |
| 63 | + %{ |
| 64 | + "source" => { |
| 65 | + "id" => "mashable", |
| 66 | + "name" => "Mashable" |
| 67 | + }, |
| 68 | + "author" => "Stan Schroeder", |
| 69 | + "title" => "Bitcoin whale moves $1.1 billion in bitcoins for an $80 fee", |
| 70 | + "description" => "Bitcoin hasn't (yet) fulfilled its original promise of being widely-used electronic cash, but it still offers some features that would be hard to achieve within the traditional banking system. Namely, moving $1.1 billion from one address to another, in a sing…", |
| 71 | + "url" => "https://mashable.com/article/bitcoin-1-1-billion-transaction/", |
| 72 | + "urlToImage" => "https://mondrian.mashable.com/2020%252F01%252F15%252F38%252Fd26e834787934c56a33fdeb39faa0be8.84e34.jpg%252F1200x630.jpg?signature=IHj6xz7nTFxvmjn6XOvUiHKJCIM=", |
| 73 | + "publishedAt" => "2020-01-15T09:10:59Z", |
| 74 | + "content" => "Bitcoin hasn't (yet) fulfilled its original promise of being widely-used electronic cash, but it still offers some features that would be hard to achieve within the traditional banking system. Namely, moving $1.1 billion from one address to another, in a sing… [+1589 chars]" |
| 75 | + }, |
| 76 | + %{ |
| 77 | + "source" => { |
| 78 | + "id" => "techcrunch", |
| 79 | + "name" => "TechCrunch" |
| 80 | + }, |
| 81 | + "author" => "Josh Constine", |
| 82 | + "title" => "The crypto rich find security in Anchorage", |
| 83 | + "description" => "Not the city, the $57 million-funded cryptocurrency custodian startup. When someone wants to keep tens or hundreds of millions of dollars in Bitcoin, Ethereum, or other coins safe, they put them in Anchorage’s vault. And now they can trade straight from custo…", |
| 84 | + "url" => "http://techcrunch.com/2020/01/15/anchorage-trading-from-custody/", |
| 85 | + "urlToImage" => "https://techcrunch.com/wp-content/uploads/2020/01/Anchorage-Trading-1.png?w=740", |
| 86 | + "publishedAt" => "2020-01-15T11:57:30Z", |
| 87 | + "content" => "Not the city, the $57 million-funded cryptocurrency custodian startup. When someone wants to keep tens or hundreds of millions of dollars in Bitcoin, Ethereum, or other coins safe, they put them in Anchorage’s vault. And now they can trade straight from custo… [+3802 chars]" |
| 88 | + }, |
| 89 | + %{ |
| 90 | + "source" => { |
| 91 | + "id" => "business-insider", |
| 92 | + "name" => "Business Insider" |
| 93 | + }, |
| 94 | + "author" => "Trista Kelley", |
| 95 | + "title" => "No, bitcoin is not the new gold", |
| 96 | + "description" => "Bitcoin bulls like to claim that bitcoin is a great haven investment, like gold, in times of geopolitical uncertainty. \"These people ignore all the times that it moves inversely with sentiment,\" says Oanda's Craig Erlam. \"Bitcoin is not gold and certainly not…", |
| 97 | + "url" => "https://markets.businessinsider.com/currencies/news/bitcoin-not-new-gold-price-doesnt-correlate-safe-haven-asset-2020-1-1028798749", |
| 98 | + "urlToImage" => "https://images.markets.businessinsider.com/image/5e145a76855cc2346900f816-2400/shutterstock241760587.jpg", |
| 99 | + "publishedAt" => "2020-01-07T11:19:15Z", |
| 100 | + "content" => "Shutterstock\r\n<ul><li>Bitcoin bulls like to claim that bitcoin is a great haven investment, like gold, in times of geopolitical uncertainty. </li><li>\"These people ignore all the times that it moves inversely with sentiment,\" says Oanda's Craig Erlam.</li><li… [+2002 chars]" |
| 101 | + }, |
| 102 | + %{ |
| 103 | + "source" => { |
| 104 | + "id" => "mashable", |
| 105 | + "name" => "Mashable" |
| 106 | + }, |
| 107 | + "author" => "Jack Morse", |
| 108 | + "title" => "2019 was a phenomenal year for cryptocurrency (scams)", |
| 109 | + "description" => "Bust out the champagne, folks! It's time to celebrate. 2019 was one hell of a year for cryptocurrency. Or, more specifically, cryptocurrency scams. That's according to Chainalysis, a respected blockchain analysis company that works with law enforcement and th…", |
| 110 | + "url" => "https://mashable.com/article/cryptocurrency-crime-ponzi-schemes-2019-report/", |
| 111 | + "urlToImage" => "https://mondrian.mashable.com/2020%252F01%252F29%252Fc8%252F4f6062d2a40343ebbc0acd60e2466265.aa895.jpg%252F1200x630.jpg?signature=Jn5fRIHQU781OBvmtI8btPhDNms=", |
| 112 | + "publishedAt" => "2020-01-29T20:54:18Z", |
| 113 | + "content" => "Bust out the champagne, folks! It's time to celebrate. \r\n2019 was one hell of a year for cryptocurrency. Or, more specifically, cryptocurrency scams. That's according to Chainalysis, a respected blockchain analysis company that works with law enforcement and … [+2253 chars]" |
| 114 | + } |
| 115 | + ] |
| 116 | + } |
| 117 | + } |
| 118 | + """ |
| 6 119 | def search(params, api_key \\ :global) do |
| 7 120 | case check(params) do |
| 8 121 | {:ok, params} -> |
| @@ -3,6 +3,63 @@ defmodule NewsApi.TopHeadlines do | |
| 3 3 | import NewsApi.Base |
| 4 4 | @url_part "/top-headlines" |
| 5 5 | |
| 6 | + @doc """ |
| 7 | + Searches the top headlines with available parameters |
| 8 | + |
| 9 | + country: The 2-letter ISO 3166-1 code of the country you want to get headlines for. Possible options: ae ar at au be bg br ca ch cn co cu cz de eg fr gb gr hk hu id ie il in it jp kr lt lv ma mx my ng nl no nz ph pl pt ro rs ru sa se sg si sk th tr tw ua us ve za . Note: you can't mix this param with the sources param. |
| 10 | + |
| 11 | + category: The category you want to get headlines for. Possible options: business entertainment general health science sports technology . Note: you can't mix this param with the sources param. |
| 12 | + |
| 13 | + sources: A comma-seperated string of identifiers for the news sources or blogs you want headlines from. Use the /sources endpoint to locate these programmatically or look at the sources index. Note: you can't mix this param with the country or category params. |
| 14 | + |
| 15 | + q: Keywords or a phrase to search for. |
| 16 | + |
| 17 | + pageSize: int The number of results to return per page (request). 20 is the default, 100 is the maximum. |
| 18 | + |
| 19 | + page: int Use this to page through the results if the total results found is greater than the page size. |
| 20 | + |
| 21 | + ## Example |
| 22 | + iex> NewsApi.TopHeadlines.search(%{ q: 'bitcoin', |
| 23 | + sources: 'bbc-news,the-verge', |
| 24 | + category: 'business', |
| 25 | + language: 'en', |
| 26 | + country: 'us'}, "your_api_key") |
| 27 | + {:ok, |
| 28 | + %{ |
| 29 | + "status" => "ok", |
| 30 | + "totalResults" => 38, |
| 31 | + "articles" => [ |
| 32 | + %{ |
| 33 | + "source" => %{ |
| 34 | + "id" => nil, |
| 35 | + "name" => "Investors.com" |
| 36 | + }, |
| 37 | + "author" => "Investor's Business Daily", |
| 38 | + "title" => "Dow Jones Today, Futures Dive On Fed, Coronavirus Risk; Microsoft, Qorvo, Lam Research Rally - Investor's Business Daily", |
| 39 | + "description" => "ServiceNow led the S&P 500, while Microsoft topped the Dow Jones today, but global markets fell hard as the coronavirus outbreak widened.", |
| 40 | + "url" => "https://www.investors.com/market-trend/stock-market-today/dow-jones-today-futures-dive-fed-coronavirus-risk-microsoft-qorvo-lam-research-rally/", |
| 41 | + "urlToImage" => "https://www.investors.com/wp-content/uploads/2017/04/Stock-bullbutt2-adobe.jpg", |
| 42 | + "publishedAt" => "2020-01-30T13:59:00Z", |
| 43 | + "content" => "Stocks took a leg down at Thursday's starting bell, as global markets read increasing economic impact into China's spreading coronavirus outbreak. Chip stocks were mixed but software stocks showed strength, as ServiceNow (NOW) headed the S&P 500 and Micro… [+6680 chars]" |
| 44 | + }, |
| 45 | + %{ |
| 46 | + "source" => { |
| 47 | + "id" => nil, |
| 48 | + "name" => "Nytimes.com" |
| 49 | + }, |
| 50 | + "author" => "Katie Robertson", |
| 51 | + "title" => "Altria Takes a $4.1 Billion Hit on Juul Stake - The New York Times", |
| 52 | + "description" => "Following an earlier write-down of $4.5 billion, the tobacco giant’s stake in the vaping start-up is now worth a fraction of what it paid in 2018.", |
| 53 | + "url" => "https://www.nytimes.com/2020/01/30/business/juul-altria-vaping.html", |
| 54 | + "urlToImage" => "https://static01.nyt.com/images/2020/01/30/world/30altria/30altria-facebookJumbo.jpg", |
| 55 | + "publishedAt" => "2020-01-30T13:36:00Z", |
| 56 | + "content" => "Altria reported a $4.1 billion write-down on its Juul Labs investment on Thursday, another sizable charge as the vaping crisis continues to roil the e-cigarette industry. The company now values its 35 percent stake in the e-cigarette company at $4.2 billion,… [+616 chars]" |
| 57 | + } |
| 58 | + ] |
| 59 | + } |
| 60 | + } |
| 61 | + """ |
| 62 | + |
| 6 63 | def search(params, api_key \\ :global) do |
| 7 64 | case check(params) do |
| 8 65 | {:ok, params} -> |
Loading more files…