mirror of
https://github.com/n8n-io/n8n.git
synced 2025-03-05 20:50:17 -08:00
Add subreddit resource
This commit is contained in:
parent
7c8bcd8567
commit
5556bf14a1
|
@ -85,3 +85,25 @@ export async function redditApiRequestAllItems(
|
||||||
|
|
||||||
return returnData;
|
return returnData;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handles a large Reddit listing by returning all items or up to a limit.
|
||||||
|
*/
|
||||||
|
export async function handleListing(
|
||||||
|
this: IExecuteFunctions,
|
||||||
|
i: number,
|
||||||
|
endpoint: string,
|
||||||
|
): Promise<any> { // tslint:disable-line:no-any
|
||||||
|
let responseData;
|
||||||
|
|
||||||
|
const returnAll = this.getNodeParameter('returnAll', i);
|
||||||
|
if (returnAll) {
|
||||||
|
return await redditApiRequestAllItems.call(this, 'GET', endpoint, {}, {}, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
const qs: IDataObject = { limit: this.getNodeParameter('limit', i) };
|
||||||
|
responseData = await redditApiRequestAllItems.call(this, 'GET', endpoint, qs, {}, true);
|
||||||
|
responseData = responseData.splice(0, qs.limit);
|
||||||
|
|
||||||
|
return responseData;
|
||||||
|
}
|
|
@ -1,158 +0,0 @@
|
||||||
import {
|
|
||||||
INodeProperties,
|
|
||||||
} from 'n8n-workflow';
|
|
||||||
|
|
||||||
export const listingOperations = [
|
|
||||||
{
|
|
||||||
displayName: 'Operation',
|
|
||||||
name: 'operation',
|
|
||||||
type: 'options',
|
|
||||||
default: 'get',
|
|
||||||
description: 'Operation to perform',
|
|
||||||
options: [
|
|
||||||
{
|
|
||||||
name: 'Get',
|
|
||||||
value: 'get',
|
|
||||||
},
|
|
||||||
],
|
|
||||||
displayOptions: {
|
|
||||||
show: {
|
|
||||||
resource: [
|
|
||||||
'listing',
|
|
||||||
],
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
] as INodeProperties[];
|
|
||||||
|
|
||||||
export const listingFields = [
|
|
||||||
{
|
|
||||||
displayName: 'Type',
|
|
||||||
name: 'type',
|
|
||||||
type: 'options',
|
|
||||||
required: true,
|
|
||||||
default: 'trending',
|
|
||||||
description: 'Type of listing to retrieve',
|
|
||||||
options: [
|
|
||||||
{
|
|
||||||
name: 'Trending',
|
|
||||||
value: 'trending',
|
|
||||||
description: 'Currently trending subreddits',
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: 'Best',
|
|
||||||
value: 'best',
|
|
||||||
description: 'Top posts in all of Reddit',
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: 'Top',
|
|
||||||
value: 'top',
|
|
||||||
description: 'Top posts in a specifc subreddit',
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: 'Hot',
|
|
||||||
value: 'hot',
|
|
||||||
description: 'Hot posts in a specifc subreddit',
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: 'New',
|
|
||||||
value: 'new',
|
|
||||||
description: 'New posts in a specifc subreddit',
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: 'Rising',
|
|
||||||
value: 'rising',
|
|
||||||
description: 'Rising posts in a specifc subreddit',
|
|
||||||
},
|
|
||||||
],
|
|
||||||
displayOptions: {
|
|
||||||
show: {
|
|
||||||
resource: [
|
|
||||||
'listing',
|
|
||||||
],
|
|
||||||
operation: [
|
|
||||||
'get',
|
|
||||||
],
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
displayName: 'Return All',
|
|
||||||
name: 'returnAll',
|
|
||||||
type: 'boolean',
|
|
||||||
default: false,
|
|
||||||
description: 'Return all results',
|
|
||||||
displayOptions: {
|
|
||||||
show: {
|
|
||||||
resource: [
|
|
||||||
'listing',
|
|
||||||
],
|
|
||||||
operation: [
|
|
||||||
'get',
|
|
||||||
],
|
|
||||||
type: [
|
|
||||||
'best',
|
|
||||||
'top',
|
|
||||||
'hot',
|
|
||||||
'new',
|
|
||||||
'rising',
|
|
||||||
],
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
displayName: 'Limit',
|
|
||||||
name: 'limit',
|
|
||||||
type: 'number',
|
|
||||||
default: 5,
|
|
||||||
description: 'The number of results to return',
|
|
||||||
typeOptions: {
|
|
||||||
minValue: 1,
|
|
||||||
maxValue: 100,
|
|
||||||
},
|
|
||||||
displayOptions: {
|
|
||||||
show: {
|
|
||||||
resource: [
|
|
||||||
'listing',
|
|
||||||
],
|
|
||||||
operation: [
|
|
||||||
'get',
|
|
||||||
],
|
|
||||||
type: [
|
|
||||||
'best',
|
|
||||||
'top',
|
|
||||||
'hot',
|
|
||||||
'new',
|
|
||||||
'rising',
|
|
||||||
],
|
|
||||||
returnAll: [
|
|
||||||
false,
|
|
||||||
],
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
displayName: 'Subreddit',
|
|
||||||
name: 'subreddit',
|
|
||||||
type: 'string',
|
|
||||||
required: true,
|
|
||||||
default: '',
|
|
||||||
description: 'The subreddit to retrieve the listing from',
|
|
||||||
displayOptions: {
|
|
||||||
show: {
|
|
||||||
resource: [
|
|
||||||
'listing',
|
|
||||||
],
|
|
||||||
operation: [
|
|
||||||
'get',
|
|
||||||
],
|
|
||||||
type: [
|
|
||||||
'top',
|
|
||||||
'hot',
|
|
||||||
'new',
|
|
||||||
'rising',
|
|
||||||
],
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
] as INodeProperties[];
|
|
|
@ -10,14 +10,15 @@ import {
|
||||||
} from 'n8n-workflow';
|
} from 'n8n-workflow';
|
||||||
|
|
||||||
import {
|
import {
|
||||||
|
handleListing,
|
||||||
redditApiRequest,
|
redditApiRequest,
|
||||||
redditApiRequestAllItems,
|
redditApiRequestAllItems,
|
||||||
} from './GenericFunctions';
|
} from './GenericFunctions';
|
||||||
|
|
||||||
import {
|
import {
|
||||||
listingFields,
|
allRedditFields,
|
||||||
listingOperations,
|
allRedditOperations,
|
||||||
} from './ListingDescription';
|
} from './AllRedditDescription';
|
||||||
|
|
||||||
import {
|
import {
|
||||||
myAccountFields,
|
myAccountFields,
|
||||||
|
@ -29,6 +30,11 @@ import {
|
||||||
submissionOperations,
|
submissionOperations,
|
||||||
} from './SubmissionDescription';
|
} from './SubmissionDescription';
|
||||||
|
|
||||||
|
import {
|
||||||
|
subredditFields,
|
||||||
|
subredditOperations,
|
||||||
|
} from './SubredditDescription';
|
||||||
|
|
||||||
export class Reddit implements INodeType {
|
export class Reddit implements INodeType {
|
||||||
description: INodeTypeDescription = {
|
description: INodeTypeDescription = {
|
||||||
displayName: 'Reddit',
|
displayName: 'Reddit',
|
||||||
|
@ -56,6 +62,10 @@ export class Reddit implements INodeType {
|
||||||
name: 'resource',
|
name: 'resource',
|
||||||
type: 'options',
|
type: 'options',
|
||||||
options: [
|
options: [
|
||||||
|
{
|
||||||
|
name: 'All Reddit',
|
||||||
|
value: 'allReddit',
|
||||||
|
},
|
||||||
{
|
{
|
||||||
name: 'My Account',
|
name: 'My Account',
|
||||||
value: 'myAccount',
|
value: 'myAccount',
|
||||||
|
@ -65,14 +75,18 @@ export class Reddit implements INodeType {
|
||||||
value: 'submission',
|
value: 'submission',
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: 'Listing',
|
name: 'Subreddit',
|
||||||
value: 'listing',
|
value: 'subreddit',
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
default: 'myAccount',
|
default: 'myAccount',
|
||||||
description: 'Resource to consume',
|
description: 'Resource to consume',
|
||||||
},
|
},
|
||||||
|
|
||||||
|
// allReddit
|
||||||
|
...allRedditOperations,
|
||||||
|
...allRedditFields,
|
||||||
|
|
||||||
// myAccount
|
// myAccount
|
||||||
...myAccountOperations,
|
...myAccountOperations,
|
||||||
...myAccountFields,
|
...myAccountFields,
|
||||||
|
@ -81,9 +95,9 @@ export class Reddit implements INodeType {
|
||||||
...submissionOperations,
|
...submissionOperations,
|
||||||
...submissionFields,
|
...submissionFields,
|
||||||
|
|
||||||
// listing
|
// subreddit
|
||||||
...listingOperations,
|
...subredditOperations,
|
||||||
...listingFields,
|
...subredditFields,
|
||||||
],
|
],
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -98,7 +112,26 @@ export class Reddit implements INodeType {
|
||||||
|
|
||||||
for (let i = 0; i < items.length; i++) {
|
for (let i = 0; i < items.length; i++) {
|
||||||
|
|
||||||
if (resource === 'myAccount') {
|
if (resource === 'allReddit') {
|
||||||
|
|
||||||
|
if (operation === 'get') {
|
||||||
|
|
||||||
|
const information = this.getNodeParameter('information', i) as string;
|
||||||
|
|
||||||
|
if (information === 'trending') {
|
||||||
|
|
||||||
|
const endpoint = 'api/trending_subreddits.json';
|
||||||
|
responseData = await redditApiRequest.call(this, 'GET', endpoint, {}, {}, true);
|
||||||
|
|
||||||
|
} else {
|
||||||
|
|
||||||
|
const endpoint = 'best.json';
|
||||||
|
responseData = await handleListing.call(this, i, endpoint);
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
} else if (resource === 'myAccount') {
|
||||||
|
|
||||||
if (operation === 'get') {
|
if (operation === 'get') {
|
||||||
|
|
||||||
|
@ -144,34 +177,31 @@ export class Reddit implements INodeType {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} else if (resource === 'listing') {
|
} else if (resource === 'subreddit') {
|
||||||
|
|
||||||
if (operation === 'get') {
|
if (operation === 'get') {
|
||||||
|
|
||||||
const type = this.getNodeParameter('type', i) as string;
|
const qs: IDataObject = {};
|
||||||
|
|
||||||
if (type === 'trending') {
|
const content = this.getNodeParameter('content', i) as string;
|
||||||
|
const subreddit = this.getNodeParameter('subreddit', i);
|
||||||
|
|
||||||
const endpoint = 'api/trending_subreddits.json';
|
if (['about', 'rules', 'sidebar', 'sticky'].includes(content)) {
|
||||||
responseData = await redditApiRequest.call(this, 'GET', endpoint, {}, {}, true);
|
|
||||||
|
|
||||||
} else {
|
const endpoint = `r/${subreddit}/about/${content}.json`;
|
||||||
|
responseData = await redditApiRequest.call(this, 'GET', endpoint, qs, {}, true);
|
||||||
|
|
||||||
const endpoint = type === 'best'
|
if (content === 'rules') {
|
||||||
? 'best.json'
|
responseData = responseData.rules;
|
||||||
: `r/${this.getNodeParameter('subreddit', i)}/${type}.json`;
|
|
||||||
|
|
||||||
const returnAll = this.getNodeParameter('returnAll', i);
|
|
||||||
|
|
||||||
if (returnAll) {
|
|
||||||
responseData = await redditApiRequestAllItems.call(this, 'GET', endpoint, {}, {}, true);
|
|
||||||
} else {
|
|
||||||
const qs: IDataObject = { limit: this.getNodeParameter('limit', i) };
|
|
||||||
responseData = await redditApiRequestAllItems.call(this, 'GET', endpoint, qs, {}, true);
|
|
||||||
responseData = responseData.splice(0, qs.limit);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
} else if (['top', 'hot', 'new', 'rising'].includes(content)) {
|
||||||
|
|
||||||
|
const endpoint = `r/${subreddit}/${content}.json`;
|
||||||
|
responseData = await handleListing.call(this, i, endpoint);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue