Skip to content
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

New Components - news_api #14487

Merged
merged 2 commits into from
Nov 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
},
michelle0927 marked this conversation as resolved.
Show resolved Hide resolved
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
};
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;
},
};
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",
},
];
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
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();
}
michelle0927 marked this conversation as resolved.
Show resolved Hide resolved

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,
}));
},
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,
});
},
},
};
};
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
Loading