2021-02-04 00:37:03 -08:00
|
|
|
import {
|
|
|
|
IExecuteFunctions,
|
|
|
|
} from 'n8n-core';
|
|
|
|
|
|
|
|
import {
|
|
|
|
IDataObject,
|
|
|
|
INodeExecutionData,
|
|
|
|
INodeType,
|
|
|
|
INodeTypeDescription,
|
2021-04-16 09:33:36 -07:00
|
|
|
NodeApiError,
|
2021-02-04 00:37:03 -08:00
|
|
|
} from 'n8n-workflow';
|
|
|
|
|
|
|
|
import {
|
|
|
|
handleListing,
|
|
|
|
redditApiRequest,
|
|
|
|
} from './GenericFunctions';
|
|
|
|
|
|
|
|
import {
|
|
|
|
postCommentFields,
|
|
|
|
postCommentOperations,
|
|
|
|
} from './PostCommentDescription';
|
|
|
|
|
|
|
|
import {
|
|
|
|
postFields,
|
|
|
|
postOperations,
|
|
|
|
} from './PostDescription';
|
|
|
|
|
|
|
|
import {
|
|
|
|
profileFields,
|
|
|
|
profileOperations,
|
|
|
|
} from './ProfileDescription';
|
|
|
|
|
|
|
|
import {
|
|
|
|
subredditFields,
|
|
|
|
subredditOperations,
|
|
|
|
} from './SubredditDescription';
|
|
|
|
|
|
|
|
import {
|
|
|
|
userFields,
|
|
|
|
userOperations,
|
|
|
|
} from './UserDescription';
|
|
|
|
|
|
|
|
export class Reddit implements INodeType {
|
|
|
|
description: INodeTypeDescription = {
|
|
|
|
displayName: 'Reddit',
|
|
|
|
name: 'reddit',
|
|
|
|
icon: 'file:reddit.svg',
|
|
|
|
group: ['transform'],
|
|
|
|
version: 1,
|
|
|
|
subtitle: '={{$parameter["operation"] + ": " + $parameter["resource"]}}',
|
|
|
|
description: 'Consume the Reddit API',
|
|
|
|
defaults: {
|
|
|
|
name: 'Reddit',
|
|
|
|
},
|
|
|
|
inputs: ['main'],
|
|
|
|
outputs: ['main'],
|
|
|
|
credentials: [
|
|
|
|
{
|
|
|
|
name: 'redditOAuth2Api',
|
|
|
|
required: true,
|
|
|
|
displayOptions: {
|
|
|
|
show: {
|
|
|
|
resource: [
|
|
|
|
'postComment',
|
|
|
|
'post',
|
|
|
|
'profile',
|
|
|
|
],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
properties: [
|
|
|
|
{
|
|
|
|
displayName: 'Resource',
|
|
|
|
name: 'resource',
|
|
|
|
type: 'options',
|
|
|
|
options: [
|
|
|
|
{
|
|
|
|
name: 'Post',
|
|
|
|
value: 'post',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Post Comment',
|
|
|
|
value: 'postComment',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Profile',
|
|
|
|
value: 'profile',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Subreddit',
|
|
|
|
value: 'subreddit',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'User',
|
|
|
|
value: 'user',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
default: 'post',
|
|
|
|
description: 'Resource to consume',
|
|
|
|
},
|
|
|
|
...postCommentOperations,
|
|
|
|
...postCommentFields,
|
|
|
|
...profileOperations,
|
|
|
|
...profileFields,
|
|
|
|
...subredditOperations,
|
|
|
|
...subredditFields,
|
|
|
|
...postOperations,
|
|
|
|
...postFields,
|
|
|
|
...userOperations,
|
|
|
|
...userFields,
|
|
|
|
],
|
|
|
|
};
|
|
|
|
|
|
|
|
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
|
|
|
|
const items = this.getInputData();
|
|
|
|
|
|
|
|
const resource = this.getNodeParameter('resource', 0) as string;
|
|
|
|
const operation = this.getNodeParameter('operation', 0) as string;
|
|
|
|
|
|
|
|
let responseData;
|
|
|
|
const returnData: IDataObject[] = [];
|
|
|
|
|
|
|
|
for (let i = 0; i < items.length; i++) {
|
2021-07-19 23:58:54 -07:00
|
|
|
try {
|
|
|
|
// *********************************************************************
|
|
|
|
// post
|
|
|
|
// *********************************************************************
|
2021-02-04 00:37:03 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (resource === 'post') {
|
2021-02-04 00:37:03 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (operation === 'create') {
|
2021-02-04 00:37:03 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
// ----------------------------------
|
|
|
|
// post: create
|
|
|
|
// ----------------------------------
|
2021-02-18 03:46:22 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
// https://www.reddit.com/dev/api/#POST_api_submit
|
2021-02-04 00:37:03 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const qs: IDataObject = {
|
|
|
|
title: this.getNodeParameter('title', i),
|
|
|
|
sr: this.getNodeParameter('subreddit', i),
|
|
|
|
kind: this.getNodeParameter('kind', i),
|
|
|
|
};
|
2021-02-04 00:37:03 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
qs.kind === 'self'
|
|
|
|
? qs.text = this.getNodeParameter('text', i)
|
|
|
|
: qs.url = this.getNodeParameter('url', i);
|
2021-02-04 00:37:03 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (qs.url) {
|
|
|
|
qs.resubmit = this.getNodeParameter('resubmit', i);
|
|
|
|
}
|
2021-02-04 00:37:03 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
responseData = await redditApiRequest.call(this, 'POST', 'api/submit', qs);
|
2021-02-04 00:37:03 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
responseData = responseData.json.data;
|
2021-02-04 00:37:03 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
} else if (operation === 'delete') {
|
2021-02-04 00:37:03 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
// ----------------------------------
|
|
|
|
// post: delete
|
|
|
|
// ----------------------------------
|
2021-02-18 03:46:22 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
// https://www.reddit.com/dev/api/#POST_api_del
|
2021-02-04 00:37:03 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const postTypePrefix = 't3_';
|
2021-02-04 00:37:03 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const qs: IDataObject = {
|
|
|
|
id: postTypePrefix + this.getNodeParameter('postId', i),
|
|
|
|
};
|
2021-02-04 00:37:03 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
await redditApiRequest.call(this, 'POST', 'api/del', qs);
|
2021-02-04 00:37:03 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
responseData = { success: true };
|
2021-02-04 00:37:03 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
} else if (operation === 'get') {
|
2021-02-04 00:37:03 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
// ----------------------------------
|
|
|
|
// post: get
|
|
|
|
// ----------------------------------
|
2021-02-18 03:46:22 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const subreddit = this.getNodeParameter('subreddit', i);
|
|
|
|
const postId = this.getNodeParameter('postId', i) as string;
|
|
|
|
const endpoint = `r/${subreddit}/comments/${postId}.json`;
|
2021-02-04 00:37:03 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
responseData = await redditApiRequest.call(this, 'GET', endpoint, {});
|
|
|
|
responseData = responseData[0].data.children[0].data;
|
2021-02-04 00:37:03 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
} else if (operation === 'getAll') {
|
2021-02-04 00:37:03 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
// ----------------------------------
|
|
|
|
// post: getAll
|
|
|
|
// ----------------------------------
|
2021-02-18 03:46:22 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
// https://www.reddit.com/dev/api/#GET_hot
|
|
|
|
// https://www.reddit.com/dev/api/#GET_new
|
|
|
|
// https://www.reddit.com/dev/api/#GET_rising
|
|
|
|
// https://www.reddit.com/dev/api/#GET_{sort}
|
2021-02-04 00:37:03 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const subreddit = this.getNodeParameter('subreddit', i);
|
|
|
|
let endpoint = `r/${subreddit}.json`;
|
2021-02-04 00:37:03 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const { category } = this.getNodeParameter('filters', i) as { category: string };
|
|
|
|
if (category) {
|
|
|
|
endpoint = `r/${subreddit}/${category}.json`;
|
|
|
|
}
|
2021-02-04 00:37:03 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
responseData = await handleListing.call(this, i, endpoint);
|
2021-02-04 00:37:03 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
} else if (operation === 'search') {
|
2021-02-04 00:37:03 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
// ----------------------------------
|
|
|
|
// post: search
|
|
|
|
// ----------------------------------
|
2021-02-18 03:46:22 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
// https://www.reddit.com/dev/api/#GET_search
|
2021-02-18 03:46:22 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const location = this.getNodeParameter('location', i);
|
2021-02-18 03:46:22 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const qs = {
|
|
|
|
q: this.getNodeParameter('keyword', i),
|
|
|
|
restrict_sr: location === 'subreddit',
|
|
|
|
} as IDataObject;
|
2021-02-18 03:46:22 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const { sort } = this.getNodeParameter('additionalFields', i) as IDataObject;
|
2021-02-18 03:46:22 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (sort) {
|
|
|
|
qs.sort = sort;
|
|
|
|
}
|
2021-02-18 03:46:22 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
let endpoint = '';
|
2021-02-18 03:46:22 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (location === 'allReddit') {
|
|
|
|
endpoint = 'search.json';
|
|
|
|
} else {
|
|
|
|
const subreddit = this.getNodeParameter('subreddit', i);
|
|
|
|
endpoint = `r/${subreddit}/search.json`;
|
|
|
|
}
|
2021-02-18 03:46:22 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
responseData = await handleListing.call(this, i, endpoint, qs);
|
2021-02-18 03:46:22 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const returnAll = this.getNodeParameter('returnAll', 0) as boolean;
|
2021-02-18 03:46:22 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (!returnAll) {
|
|
|
|
const limit = this.getNodeParameter('limit', 0) as number;
|
|
|
|
responseData = responseData.splice(0, limit);
|
|
|
|
}
|
2021-02-18 03:46:22 -08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
} else if (resource === 'postComment') {
|
2021-02-04 00:37:03 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
// *********************************************************************
|
|
|
|
// postComment
|
|
|
|
// *********************************************************************
|
2021-02-04 00:37:03 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (operation === 'create') {
|
2021-02-04 00:37:03 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
// ----------------------------------
|
|
|
|
// postComment: create
|
|
|
|
// ----------------------------------
|
2021-02-18 03:46:22 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
// https://www.reddit.com/dev/api/#POST_api_comment
|
2021-02-04 00:37:03 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const postTypePrefix = 't3_';
|
2021-02-04 00:37:03 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const qs: IDataObject = {
|
|
|
|
text: this.getNodeParameter('commentText', i),
|
|
|
|
thing_id: postTypePrefix + this.getNodeParameter('postId', i),
|
|
|
|
};
|
2021-02-04 00:37:03 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
responseData = await redditApiRequest.call(this, 'POST', 'api/comment', qs);
|
|
|
|
responseData = responseData.json.data.things[0].data;
|
2021-02-04 00:37:03 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
} else if (operation === 'getAll') {
|
2021-02-04 00:37:03 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
// ----------------------------------
|
|
|
|
// postComment: getAll
|
|
|
|
// ----------------------------------
|
2021-02-18 03:46:22 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
// https://www.reddit.com/r/{subrreddit}/comments/{postId}.json
|
2021-02-04 00:37:03 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const subreddit = this.getNodeParameter('subreddit', i);
|
|
|
|
const postId = this.getNodeParameter('postId', i) as string;
|
|
|
|
const endpoint = `r/${subreddit}/comments/${postId}.json`;
|
2021-02-04 00:37:03 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
responseData = await handleListing.call(this, i, endpoint);
|
2021-02-04 00:37:03 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
} else if (operation === 'delete') {
|
2021-02-04 00:37:03 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
// ----------------------------------
|
|
|
|
// postComment: delete
|
|
|
|
// ----------------------------------
|
2021-02-18 03:46:22 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
// https://www.reddit.com/dev/api/#POST_api_del
|
2021-02-04 00:37:03 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const commentTypePrefix = 't1_';
|
2021-02-04 00:37:03 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const qs: IDataObject = {
|
|
|
|
id: commentTypePrefix + this.getNodeParameter('commentId', i),
|
|
|
|
};
|
2021-02-04 00:37:03 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
await redditApiRequest.call(this, 'POST', 'api/del', qs);
|
2021-02-04 00:37:03 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
responseData = { success: true };
|
2021-02-04 00:37:03 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
} else if (operation === 'reply') {
|
2021-02-04 00:37:03 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
// ----------------------------------
|
|
|
|
// postComment: reply
|
|
|
|
// ----------------------------------
|
2021-02-18 03:46:22 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
// https://www.reddit.com/dev/api/#POST_api_comment
|
2021-02-04 00:37:03 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const commentTypePrefix = 't1_';
|
2021-02-04 00:37:03 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const qs: IDataObject = {
|
|
|
|
text: this.getNodeParameter('replyText', i),
|
|
|
|
thing_id: commentTypePrefix + this.getNodeParameter('commentId', i),
|
|
|
|
};
|
2021-02-04 00:37:03 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
responseData = await redditApiRequest.call(this, 'POST', 'api/comment', qs);
|
|
|
|
responseData = responseData.json.data.things[0].data;
|
|
|
|
}
|
2021-02-04 00:37:03 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
} else if (resource === 'profile') {
|
2021-02-04 00:37:03 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
// *********************************************************************
|
|
|
|
// profile
|
|
|
|
// *********************************************************************
|
2021-02-18 03:46:22 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (operation === 'get') {
|
2021-02-04 00:37:03 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
// ----------------------------------
|
|
|
|
// profile: get
|
|
|
|
// ----------------------------------
|
|
|
|
|
|
|
|
// https://www.reddit.com/dev/api/#GET_api_v1_me
|
|
|
|
// https://www.reddit.com/dev/api/#GET_api_v1_me_karma
|
|
|
|
// https://www.reddit.com/dev/api/#GET_api_v1_me_prefs
|
|
|
|
// https://www.reddit.com/dev/api/#GET_api_v1_me_trophies
|
|
|
|
// https://www.reddit.com/dev/api/#GET_prefs_{where}
|
|
|
|
|
|
|
|
const endpoints: { [key: string]: string } = {
|
|
|
|
identity: 'me',
|
|
|
|
blockedUsers: 'me/blocked',
|
|
|
|
friends: 'me/friends',
|
|
|
|
karma: 'me/karma',
|
|
|
|
prefs: 'me/prefs',
|
|
|
|
trophies: 'me/trophies',
|
|
|
|
};
|
|
|
|
|
|
|
|
const details = this.getNodeParameter('details', i) as string;
|
|
|
|
const endpoint = `api/v1/${endpoints[details]}`;
|
|
|
|
responseData = await redditApiRequest.call(this, 'GET', endpoint, {});
|
|
|
|
|
|
|
|
if (details === 'identity') {
|
|
|
|
responseData = responseData.features;
|
|
|
|
} else if (details === 'friends') {
|
|
|
|
responseData = responseData.data.children;
|
|
|
|
if (!responseData.length) {
|
|
|
|
throw new NodeApiError(this.getNode(), responseData);
|
|
|
|
}
|
|
|
|
} else if (details === 'karma') {
|
|
|
|
responseData = responseData.data;
|
|
|
|
if (!responseData.length) {
|
|
|
|
throw new NodeApiError(this.getNode(), responseData);
|
|
|
|
}
|
|
|
|
} else if (details === 'trophies') {
|
|
|
|
responseData = responseData.data.trophies.map((trophy: IDataObject) => trophy.data);
|
2021-02-04 00:37:03 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
} else if (resource === 'subreddit') {
|
2021-02-04 00:37:03 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
// *********************************************************************
|
|
|
|
// subreddit
|
|
|
|
// *********************************************************************
|
2021-02-04 00:37:03 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (operation === 'get') {
|
2021-02-18 03:46:22 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
// ----------------------------------
|
|
|
|
// subreddit: get
|
|
|
|
// ----------------------------------
|
2021-02-04 00:37:03 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
// https://www.reddit.com/dev/api/#GET_r_{subreddit}_about
|
|
|
|
// https://www.reddit.com/dev/api/#GET_r_{subreddit}_about_rules
|
2021-02-04 00:37:03 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const subreddit = this.getNodeParameter('subreddit', i);
|
|
|
|
const content = this.getNodeParameter('content', i) as string;
|
|
|
|
const endpoint = `r/${subreddit}/about/${content}.json`;
|
2021-02-04 00:37:03 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
responseData = await redditApiRequest.call(this, 'GET', endpoint, {});
|
2021-02-04 00:37:03 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (content === 'rules') {
|
|
|
|
responseData = responseData.rules;
|
|
|
|
} else if (content === 'about') {
|
|
|
|
responseData = responseData.data;
|
|
|
|
}
|
2021-02-04 00:37:03 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
} else if (operation === 'getAll') {
|
2021-02-18 03:46:22 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
// ----------------------------------
|
|
|
|
// subreddit: getAll
|
|
|
|
// ----------------------------------
|
2021-02-04 00:37:03 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
// https://www.reddit.com/dev/api/#GET_api_trending_subreddits
|
|
|
|
// https://www.reddit.com/dev/api/#POST_api_search_subreddits
|
|
|
|
// https://www.reddit.com/r/subreddits.json
|
2021-02-04 00:37:03 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const filters = this.getNodeParameter('filters', i) as IDataObject;
|
2021-02-04 00:37:03 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (filters.trending) {
|
|
|
|
const returnAll = this.getNodeParameter('returnAll', 0) as boolean;
|
|
|
|
const endpoint = 'api/trending_subreddits.json';
|
|
|
|
responseData = await redditApiRequest.call(this, 'GET', endpoint, {});
|
|
|
|
responseData = responseData.subreddit_names.map((name: string) => ({ name }));
|
|
|
|
if (returnAll === false) {
|
|
|
|
const limit = this.getNodeParameter('limit', 0) as number;
|
|
|
|
responseData = responseData.splice(0, limit);
|
|
|
|
}
|
2021-02-04 00:37:03 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
} else if (filters.keyword) {
|
|
|
|
const qs: IDataObject = {};
|
|
|
|
qs.query = filters.keyword;
|
2021-02-04 00:37:03 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const endpoint = 'api/search_subreddits.json';
|
|
|
|
responseData = await redditApiRequest.call(this, 'POST', endpoint, qs);
|
2021-02-04 00:37:03 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const returnAll = this.getNodeParameter('returnAll', 0) as boolean;
|
2021-02-04 00:37:03 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (returnAll === false) {
|
|
|
|
const limit = this.getNodeParameter('limit', 0) as number;
|
|
|
|
responseData = responseData.subreddits.splice(0, limit);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
const endpoint = 'r/subreddits.json';
|
|
|
|
responseData = await handleListing.call(this, i, endpoint);
|
2021-02-04 00:37:03 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
} else if (resource === 'user') {
|
2021-02-18 03:46:22 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
// *********************************************************************
|
|
|
|
// user
|
|
|
|
// *********************************************************************
|
2021-02-04 00:37:03 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (operation === 'get') {
|
2021-02-04 00:37:03 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
// ----------------------------------
|
|
|
|
// user: get
|
|
|
|
// ----------------------------------
|
2021-02-04 00:37:03 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
// https://www.reddit.com/dev/api/#GET_user_{username}_{where}
|
2021-02-04 00:37:03 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const username = this.getNodeParameter('username', i) as string;
|
|
|
|
const details = this.getNodeParameter('details', i) as string;
|
|
|
|
const endpoint = `user/${username}/${details}.json`;
|
2021-02-04 00:37:03 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
responseData = details === 'about'
|
|
|
|
? await redditApiRequest.call(this, 'GET', endpoint, {})
|
|
|
|
: await handleListing.call(this, i, endpoint);
|
2021-02-04 00:37:03 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (details === 'about') {
|
|
|
|
responseData = responseData.data;
|
|
|
|
}
|
2021-02-04 00:37:03 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
Array.isArray(responseData)
|
|
|
|
? returnData.push(...responseData)
|
|
|
|
: returnData.push(responseData);
|
|
|
|
} catch (error) {
|
|
|
|
if (this.continueOnFail()) {
|
|
|
|
returnData.push({ error: error.message });
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
throw error;
|
|
|
|
}
|
2021-02-04 00:37:03 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
return [this.helpers.returnJsonArray(returnData)];
|
|
|
|
}
|
|
|
|
}
|