Skip to content

Commit ccfb643

Browse files
committed
new components
1 parent 02197b3 commit ccfb643

File tree

6 files changed

+350
-7
lines changed

6 files changed

+350
-7
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
import newsapi from "../../news_api.app.mjs";
2+
import utils from "../../common/utils.mjs";
3+
4+
export default {
5+
key: "news_api-search-everything",
6+
name: "Search Everything",
7+
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)",
8+
version: "0.0.1",
9+
type: "action",
10+
props: {
11+
newsapi,
12+
q: {
13+
propDefinition: [
14+
newsapi,
15+
"q",
16+
],
17+
},
18+
searchin: {
19+
propDefinition: [
20+
newsapi,
21+
"searchin",
22+
],
23+
},
24+
sourceIds: {
25+
propDefinition: [
26+
newsapi,
27+
"sourceIds",
28+
],
29+
},
30+
domains: {
31+
type: "string[]",
32+
label: "Domains",
33+
description: "An array of domains to restrict the search to",
34+
optional: true,
35+
},
36+
excludeDomains: {
37+
type: "string[]",
38+
label: "Exclude Domains",
39+
description: "An array of domains to remove from the results",
40+
optional: true,
41+
},
42+
from: {
43+
type: "string",
44+
label: "From",
45+
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`)",
46+
optional: true,
47+
},
48+
to: {
49+
type: "string",
50+
label: "To",
51+
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`)",
52+
optional: true,
53+
},
54+
language: {
55+
propDefinition: [
56+
newsapi,
57+
"language",
58+
],
59+
},
60+
sortBy: {
61+
propDefinition: [
62+
newsapi,
63+
"sortBy",
64+
],
65+
},
66+
maxResults: {
67+
propDefinition: [
68+
newsapi,
69+
"maxResults",
70+
],
71+
},
72+
},
73+
async run({ $ }) {
74+
const {
75+
status, articles,
76+
} = await this.newsapi.searchEverything({
77+
$,
78+
params: {
79+
q: this.q,
80+
searchin: utils.joinArray(this.searchin),
81+
sources: utils.joinArray(this.sourceIds),
82+
domains: utils.joinArray(this.domains),
83+
excludeDomains: utils.joinArray(this.excludeDomains),
84+
from: this.from,
85+
to: this.to,
86+
language: this.language,
87+
sortBy: this.sortBy,
88+
pageSize: this.maxResults,
89+
},
90+
});
91+
92+
if (status === "ok") {
93+
$.export("$summary", `Successfully retrieved ${articles.length} article${articles.length === 1
94+
? ""
95+
: "s"}`);
96+
}
97+
98+
return articles;
99+
},
100+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import newsapi from "../../news_api.app.mjs";
2+
import utils from "../../common/utils.mjs";
3+
import { ConfigurationError } from "@pipedream/platform";
4+
5+
export default {
6+
key: "news_api-search-top-headlines",
7+
name: "Search Top Headlines",
8+
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)",
9+
version: "0.0.1",
10+
type: "action",
11+
props: {
12+
newsapi,
13+
q: {
14+
propDefinition: [
15+
newsapi,
16+
"q",
17+
],
18+
optional: true,
19+
},
20+
category: {
21+
type: "string",
22+
label: "Category",
23+
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.",
24+
optional: true,
25+
},
26+
sourceIds: {
27+
propDefinition: [
28+
newsapi,
29+
"sourceIds",
30+
],
31+
},
32+
maxResults: {
33+
propDefinition: [
34+
newsapi,
35+
"maxResults",
36+
],
37+
},
38+
},
39+
async run({ $ }) {
40+
if (this.category && this.sourceIds) {
41+
throw new ConfigurationError("Please specify only one of `Category` or `SourceIds`");
42+
}
43+
44+
const params = {
45+
q: this.q,
46+
category: this.category,
47+
sources: utils.joinArray(this.sourceIds),
48+
pageSize: this.maxResults,
49+
};
50+
51+
// The only available country is "us", but it can't be specified along with category or sources.
52+
// At least one of q, category, sources, or country must be entered, so adding in country if
53+
// none of the others are specified.
54+
if (!this.q && !this.category && !this.sourceIds) {
55+
params.country = "us";
56+
}
57+
58+
const {
59+
status, articles,
60+
} = await this.newsapi.searchTopHeadlines({
61+
$,
62+
params,
63+
});
64+
65+
if (status === "ok") {
66+
$.export("$summary", `Successfully retrieved ${articles.length} article${articles.length === 1
67+
? ""
68+
: "s"}`);
69+
}
70+
71+
return articles;
72+
},
73+
};
+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
const SEARCH_IN_OPTIONS = [
2+
"title",
3+
"description",
4+
"content",
5+
];
6+
7+
const SORT_OPTIONS = [
8+
"relevancy",
9+
"popularity",
10+
"publishedAt",
11+
];
12+
13+
const LANGUAGES = [
14+
{
15+
value: "ar",
16+
label: "Arabic",
17+
},
18+
{
19+
value: "de",
20+
label: "German",
21+
},
22+
{
23+
value: "en",
24+
label: "English",
25+
},
26+
{
27+
value: "es",
28+
label: "Spanish",
29+
},
30+
{
31+
value: "fr",
32+
label: "French",
33+
},
34+
{
35+
value: "he",
36+
label: "Hebrew",
37+
},
38+
{
39+
value: "it",
40+
label: "Italian",
41+
},
42+
{
43+
value: "nl",
44+
label: "Dutch",
45+
},
46+
{
47+
value: "no",
48+
label: "Norwegian",
49+
},
50+
{
51+
value: "pt",
52+
label: "Portuguese",
53+
},
54+
{
55+
value: "ru",
56+
label: "Russian",
57+
},
58+
{
59+
value: "sv",
60+
label: "Swedish",
61+
},
62+
{
63+
value: "ud",
64+
label: "Urdu",
65+
},
66+
{
67+
value: "zh",
68+
label: "Chinese",
69+
},
70+
];
71+
72+
export default {
73+
SEARCH_IN_OPTIONS,
74+
SORT_OPTIONS,
75+
LANGUAGES,
76+
};

components/news_api/common/utils.mjs

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
function joinArray(arr) {
2+
if (!arr) {
3+
return undefined;
4+
}
5+
return arr.join();
6+
}
7+
8+
export default {
9+
joinArray,
10+
};

components/news_api/news_api.app.mjs

+86-5
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,92 @@
1+
import { axios } from "@pipedream/platform";
2+
import constants from "./common/constants.mjs";
3+
14
export default {
25
type: "app",
36
app: "news_api",
4-
propDefinitions: {},
7+
propDefinitions: {
8+
sourceIds: {
9+
type: "string[]",
10+
label: "Source IDs",
11+
description: "An array of source identifiers (maximum 20) for the news sources or blogs you want headlines from",
12+
optional: true,
13+
async options() {
14+
const { sources } = await this.listSources();
15+
return sources.map(({
16+
id: value, name: label,
17+
}) => ({
18+
value,
19+
label,
20+
}));
21+
},
22+
},
23+
searchin: {
24+
type: "string[]",
25+
label: "Search In",
26+
description: "The fields to restrict your q search to. Default: all fields are searched",
27+
options: constants.SEARCH_IN_OPTIONS,
28+
optional: true,
29+
},
30+
q: {
31+
type: "string",
32+
label: "Query",
33+
description: "Keywords or phrases to search for",
34+
},
35+
language: {
36+
type: "string",
37+
label: "Language",
38+
description: "The 2-letter ISO-639-1 code of the language you want to get headlines for",
39+
options: constants.LANGUAGES,
40+
optional: true,
41+
},
42+
sortBy: {
43+
type: "string",
44+
label: "Sort By",
45+
description: "The order to sort the articles in. Default: `publishedAt`",
46+
options: constants.SORT_OPTIONS,
47+
optional: true,
48+
},
49+
maxResults: {
50+
type: "integer",
51+
label: "Max Results",
52+
description: "The maximum number of results to return. Must be between 1 and 100.",
53+
optional: true,
54+
},
55+
},
556
methods: {
6-
// this.$auth contains connected account data
7-
authKeys() {
8-
console.log(Object.keys(this.$auth));
57+
_baseUrl() {
58+
return "https://newsapi.org/v2";
59+
},
60+
_makeRequest({
61+
$ = this,
62+
path,
63+
...opts
64+
}) {
65+
return axios($, {
66+
url: `${this._baseUrl()}${path}`,
67+
headers: {
68+
"X-Api-Key": `${this.$auth.api_key}`,
69+
},
70+
...opts,
71+
});
72+
},
73+
listSources(opts = {}) {
74+
return this._makeRequest({
75+
path: "/top-headlines/sources",
76+
...opts,
77+
});
78+
},
79+
searchEverything(opts = {}) {
80+
return this._makeRequest({
81+
path: "/everything",
82+
...opts,
83+
});
84+
},
85+
searchTopHeadlines(opts = {}) {
86+
return this._makeRequest({
87+
path: "/top-headlines",
88+
...opts,
89+
});
990
},
1091
},
11-
};
92+
};

components/news_api/package.json

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@pipedream/news_api",
3-
"version": "0.0.1",
3+
"version": "0.1.0",
44
"description": "Pipedream News API Components",
55
"main": "news_api.app.mjs",
66
"keywords": [
@@ -11,5 +11,8 @@
1111
"author": "Pipedream <[email protected]> (https://pipedream.com/)",
1212
"publishConfig": {
1313
"access": "public"
14+
},
15+
"dependencies": {
16+
"@pipedream/platform": "^3.0.3"
1417
}
15-
}
18+
}

0 commit comments

Comments
 (0)