Add subreddit resource

This commit is contained in:
Iván Ovejero 2021-01-17 19:36:30 -03:00
parent 7c8bcd8567
commit 5556bf14a1
3 changed files with 79 additions and 185 deletions

View file

@ -85,3 +85,25 @@ export async function redditApiRequestAllItems(
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;
}

View file

@ -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[];

View file

@ -10,14 +10,15 @@ import {
} from 'n8n-workflow';
import {
handleListing,
redditApiRequest,
redditApiRequestAllItems,
} from './GenericFunctions';
import {
listingFields,
listingOperations,
} from './ListingDescription';
allRedditFields,
allRedditOperations,
} from './AllRedditDescription';
import {
myAccountFields,
@ -29,6 +30,11 @@ import {
submissionOperations,
} from './SubmissionDescription';
import {
subredditFields,
subredditOperations,
} from './SubredditDescription';
export class Reddit implements INodeType {
description: INodeTypeDescription = {
displayName: 'Reddit',
@ -56,6 +62,10 @@ export class Reddit implements INodeType {
name: 'resource',
type: 'options',
options: [
{
name: 'All Reddit',
value: 'allReddit',
},
{
name: 'My Account',
value: 'myAccount',
@ -65,14 +75,18 @@ export class Reddit implements INodeType {
value: 'submission',
},
{
name: 'Listing',
value: 'listing',
name: 'Subreddit',
value: 'subreddit',
},
],
default: 'myAccount',
description: 'Resource to consume',
},
// allReddit
...allRedditOperations,
...allRedditFields,
// myAccount
...myAccountOperations,
...myAccountFields,
@ -81,9 +95,9 @@ export class Reddit implements INodeType {
...submissionOperations,
...submissionFields,
// listing
...listingOperations,
...listingFields,
// subreddit
...subredditOperations,
...subredditFields,
],
};
@ -98,7 +112,26 @@ export class Reddit implements INodeType {
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') {
@ -144,34 +177,31 @@ export class Reddit implements INodeType {
}
} else if (resource === 'listing') {
} else if (resource === 'subreddit') {
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';
responseData = await redditApiRequest.call(this, 'GET', endpoint, {}, {}, true);
if (['about', 'rules', 'sidebar', 'sticky'].includes(content)) {
} else {
const endpoint = `r/${subreddit}/about/${content}.json`;
responseData = await redditApiRequest.call(this, 'GET', endpoint, qs, {}, true);
const endpoint = type === 'best'
? 'best.json'
: `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);
if (content === 'rules') {
responseData = responseData.rules;
}
} else if (['top', 'hot', 'new', 'rising'].includes(content)) {
const endpoint = `r/${subreddit}/${content}.json`;
responseData = await handleListing.call(this, i, endpoint);
}
}
}