-
Notifications
You must be signed in to change notification settings - Fork 5.6k
New Components - news_api #14487
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
New Components - news_api #14487
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
100 changes: 100 additions & 0 deletions
100
components/news_api/actions/search-everything/search-everything.mjs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| import newsapi from "../../news_api.app.mjs"; | ||
| import utils from "../../common/utils.mjs"; | ||
|
|
||
| export default { | ||
| key: "news_api-search-everything", | ||
| name: "Search Everything", | ||
| description: "Search through millions of articles from over 150,000 large and small news sources and blogs. [See the documentation](https://newsapi.org/docs/endpoints/everything)", | ||
| version: "0.0.1", | ||
| type: "action", | ||
| props: { | ||
| newsapi, | ||
| q: { | ||
| propDefinition: [ | ||
| newsapi, | ||
| "q", | ||
| ], | ||
| }, | ||
| searchin: { | ||
| propDefinition: [ | ||
| newsapi, | ||
| "searchin", | ||
| ], | ||
| }, | ||
| sourceIds: { | ||
| propDefinition: [ | ||
| newsapi, | ||
| "sourceIds", | ||
| ], | ||
| }, | ||
| domains: { | ||
| type: "string[]", | ||
| label: "Domains", | ||
| description: "An array of domains to restrict the search to", | ||
| optional: true, | ||
| }, | ||
| excludeDomains: { | ||
| type: "string[]", | ||
| label: "Exclude Domains", | ||
| description: "An array of domains to remove from the results", | ||
| optional: true, | ||
| }, | ||
| from: { | ||
| type: "string", | ||
| label: "From", | ||
| description: "A date and optional time for the oldest article allowed. This should be in ISO 8601 format (e.g. `2024-11-01` or `2024-11-01T17:27:47`)", | ||
| optional: true, | ||
| }, | ||
| to: { | ||
| type: "string", | ||
| label: "To", | ||
| description: "A date and optional time for the newest article allowed. This should be in ISO 8601 format (e.g. `2024-11-01` or `2024-11-01T17:27:47`)", | ||
| optional: true, | ||
| }, | ||
| language: { | ||
| propDefinition: [ | ||
| newsapi, | ||
| "language", | ||
| ], | ||
| }, | ||
| sortBy: { | ||
| propDefinition: [ | ||
| newsapi, | ||
| "sortBy", | ||
| ], | ||
| }, | ||
| maxResults: { | ||
| propDefinition: [ | ||
| newsapi, | ||
| "maxResults", | ||
| ], | ||
| }, | ||
| }, | ||
| async run({ $ }) { | ||
| const { | ||
| status, articles, | ||
| } = await this.newsapi.searchEverything({ | ||
| $, | ||
| params: { | ||
| q: this.q, | ||
| searchin: utils.joinArray(this.searchin), | ||
| sources: utils.joinArray(this.sourceIds), | ||
| domains: utils.joinArray(this.domains), | ||
| excludeDomains: utils.joinArray(this.excludeDomains), | ||
| from: this.from, | ||
| to: this.to, | ||
| language: this.language, | ||
| sortBy: this.sortBy, | ||
| pageSize: this.maxResults, | ||
| }, | ||
| }); | ||
|
|
||
| if (status === "ok") { | ||
| $.export("$summary", `Successfully retrieved ${articles.length} article${articles.length === 1 | ||
| ? "" | ||
| : "s"}`); | ||
| } | ||
|
|
||
| return articles; | ||
| }, | ||
michelle0927 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| }; | ||
73 changes: 73 additions & 0 deletions
73
components/news_api/actions/search-top-headlines/search-top-headlines.mjs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| import newsapi from "../../news_api.app.mjs"; | ||
| import utils from "../../common/utils.mjs"; | ||
| import { ConfigurationError } from "@pipedream/platform"; | ||
|
|
||
| export default { | ||
| key: "news_api-search-top-headlines", | ||
| name: "Search Top Headlines", | ||
| description: "Retrieve live top and breaking headlines for a category, single source, multiple sources, or keywords. [See the documentation](https://newsapi.org/docs/endpoints/top-headlines)", | ||
| version: "0.0.1", | ||
| type: "action", | ||
| props: { | ||
| newsapi, | ||
| q: { | ||
| propDefinition: [ | ||
| newsapi, | ||
| "q", | ||
| ], | ||
| optional: true, | ||
| }, | ||
| category: { | ||
| type: "string", | ||
| label: "Category", | ||
| description: "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.", | ||
| optional: true, | ||
| }, | ||
michelle0927 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| sourceIds: { | ||
| propDefinition: [ | ||
| newsapi, | ||
| "sourceIds", | ||
| ], | ||
| }, | ||
| maxResults: { | ||
| propDefinition: [ | ||
| newsapi, | ||
| "maxResults", | ||
| ], | ||
| }, | ||
| }, | ||
| async run({ $ }) { | ||
| if (this.category && this.sourceIds) { | ||
| throw new ConfigurationError("Please specify only one of `Category` or `SourceIds`"); | ||
| } | ||
|
|
||
| const params = { | ||
| q: this.q, | ||
| category: this.category, | ||
| sources: utils.joinArray(this.sourceIds), | ||
| pageSize: this.maxResults, | ||
| }; | ||
michelle0927 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| // The only available country is "us", but it can't be specified along with category or sources. | ||
| // At least one of q, category, sources, or country must be entered, so adding in country if | ||
| // none of the others are specified. | ||
| if (!this.q && !this.category && !this.sourceIds) { | ||
| params.country = "us"; | ||
| } | ||
|
|
||
| const { | ||
| status, articles, | ||
| } = await this.newsapi.searchTopHeadlines({ | ||
| $, | ||
| params, | ||
| }); | ||
|
|
||
| if (status === "ok") { | ||
| $.export("$summary", `Successfully retrieved ${articles.length} article${articles.length === 1 | ||
| ? "" | ||
| : "s"}`); | ||
| } | ||
michelle0927 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| return articles; | ||
| }, | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| const SEARCH_IN_OPTIONS = [ | ||
| "title", | ||
| "description", | ||
| "content", | ||
| ]; | ||
|
|
||
| const SORT_OPTIONS = [ | ||
| "relevancy", | ||
| "popularity", | ||
| "publishedAt", | ||
| ]; | ||
|
|
||
| const LANGUAGES = [ | ||
| { | ||
| value: "ar", | ||
| label: "Arabic", | ||
| }, | ||
| { | ||
| value: "de", | ||
| label: "German", | ||
| }, | ||
| { | ||
| value: "en", | ||
| label: "English", | ||
| }, | ||
| { | ||
| value: "es", | ||
| label: "Spanish", | ||
| }, | ||
| { | ||
| value: "fr", | ||
| label: "French", | ||
| }, | ||
| { | ||
| value: "he", | ||
| label: "Hebrew", | ||
| }, | ||
| { | ||
| value: "it", | ||
| label: "Italian", | ||
| }, | ||
| { | ||
| value: "nl", | ||
| label: "Dutch", | ||
| }, | ||
| { | ||
| value: "no", | ||
| label: "Norwegian", | ||
| }, | ||
| { | ||
| value: "pt", | ||
| label: "Portuguese", | ||
| }, | ||
| { | ||
| value: "ru", | ||
| label: "Russian", | ||
| }, | ||
| { | ||
| value: "sv", | ||
| label: "Swedish", | ||
| }, | ||
| { | ||
| value: "ud", | ||
| label: "Urdu", | ||
| }, | ||
| { | ||
| value: "zh", | ||
| label: "Chinese", | ||
| }, | ||
| ]; | ||
michelle0927 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| export default { | ||
| SEARCH_IN_OPTIONS, | ||
| SORT_OPTIONS, | ||
| LANGUAGES, | ||
| }; | ||
michelle0927 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| function joinArray(arr) { | ||
| if (!arr) { | ||
| return undefined; | ||
| } | ||
| return arr.join(); | ||
| } | ||
michelle0927 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| export default { | ||
| joinArray, | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,92 @@ | ||
| import { axios } from "@pipedream/platform"; | ||
| import constants from "./common/constants.mjs"; | ||
|
|
||
| export default { | ||
| type: "app", | ||
| app: "news_api", | ||
| propDefinitions: {}, | ||
| propDefinitions: { | ||
| sourceIds: { | ||
| type: "string[]", | ||
| label: "Source IDs", | ||
| description: "An array of source identifiers (maximum 20) for the news sources or blogs you want headlines from", | ||
| optional: true, | ||
| async options() { | ||
| const { sources } = await this.listSources(); | ||
| return sources.map(({ | ||
| id: value, name: label, | ||
| }) => ({ | ||
| value, | ||
| label, | ||
| })); | ||
| }, | ||
michelle0927 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| }, | ||
| searchin: { | ||
| type: "string[]", | ||
| label: "Search In", | ||
| description: "The fields to restrict your q search to. Default: all fields are searched", | ||
| options: constants.SEARCH_IN_OPTIONS, | ||
| optional: true, | ||
| }, | ||
| q: { | ||
| type: "string", | ||
| label: "Query", | ||
| description: "Keywords or phrases to search for", | ||
| }, | ||
michelle0927 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| language: { | ||
| type: "string", | ||
| label: "Language", | ||
| description: "The 2-letter ISO-639-1 code of the language you want to get headlines for", | ||
| options: constants.LANGUAGES, | ||
| optional: true, | ||
| }, | ||
| sortBy: { | ||
| type: "string", | ||
| label: "Sort By", | ||
| description: "The order to sort the articles in. Default: `publishedAt`", | ||
| options: constants.SORT_OPTIONS, | ||
| optional: true, | ||
| }, | ||
| maxResults: { | ||
| type: "integer", | ||
| label: "Max Results", | ||
| description: "The maximum number of results to return. Must be between 1 and 100.", | ||
| optional: true, | ||
| }, | ||
| }, | ||
| methods: { | ||
| // this.$auth contains connected account data | ||
| authKeys() { | ||
| console.log(Object.keys(this.$auth)); | ||
| _baseUrl() { | ||
| return "https://newsapi.org/v2"; | ||
| }, | ||
| _makeRequest({ | ||
| $ = this, | ||
| path, | ||
| ...opts | ||
| }) { | ||
| return axios($, { | ||
| url: `${this._baseUrl()}${path}`, | ||
| headers: { | ||
| "X-Api-Key": `${this.$auth.api_key}`, | ||
michelle0927 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| }, | ||
| ...opts, | ||
| }); | ||
| }, | ||
michelle0927 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| listSources(opts = {}) { | ||
| return this._makeRequest({ | ||
| path: "/top-headlines/sources", | ||
| ...opts, | ||
| }); | ||
| }, | ||
| searchEverything(opts = {}) { | ||
| return this._makeRequest({ | ||
| path: "/everything", | ||
| ...opts, | ||
| }); | ||
| }, | ||
| searchTopHeadlines(opts = {}) { | ||
| return this._makeRequest({ | ||
| path: "/top-headlines", | ||
| ...opts, | ||
| }); | ||
| }, | ||
| }, | ||
| }; | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.