Skip to content

Commit

Permalink
New Components - news_api (#14487)
Browse files Browse the repository at this point in the history
* new components

* pnpm-lock.yaml
  • Loading branch information
michelle0927 authored Nov 4, 2024
1 parent 9a24ee0 commit 6bed164
Show file tree
Hide file tree
Showing 7 changed files with 405 additions and 59 deletions.
100 changes: 100 additions & 0 deletions components/news_api/actions/search-everything/search-everything.mjs
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;
},
};
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,
},
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,
};

// 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"}`);
}

return articles;
},
};
76 changes: 76 additions & 0 deletions components/news_api/common/constants.mjs
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",
},
];

export default {
SEARCH_IN_OPTIONS,
SORT_OPTIONS,
LANGUAGES,
};
10 changes: 10 additions & 0 deletions components/news_api/common/utils.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
function joinArray(arr) {
if (!arr) {
return undefined;
}
return arr.join();
}

export default {
joinArray,
};
91 changes: 86 additions & 5 deletions components/news_api/news_api.app.mjs
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,
}));
},
},
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",
},
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}`,
},
...opts,
});
},
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,
});
},
},
};
};
7 changes: 5 additions & 2 deletions components/news_api/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@pipedream/news_api",
"version": "0.0.1",
"version": "0.1.0",
"description": "Pipedream News API Components",
"main": "news_api.app.mjs",
"keywords": [
Expand All @@ -11,5 +11,8 @@
"author": "Pipedream <[email protected]> (https://pipedream.com/)",
"publishConfig": {
"access": "public"
},
"dependencies": {
"@pipedream/platform": "^3.0.3"
}
}
}
Loading

0 comments on commit 6bed164

Please sign in to comment.