Created
June 30, 2020 19:39
-
-
Save JeffreyQ/e97bd4913313e353fb96ed781796014e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fetch = require('node-fetch'); | |
const _ = require('lodash'); | |
function format(str) { | |
// console.log(str) | |
const decoded = decodeURIComponent(str); | |
// replace hex representations of forward slash and apostrophes | |
const formatted = decoded.replace(///g, '/').replace(/'/g, `'`); | |
return formatted; | |
} | |
function recursiveFormat(str) { | |
} | |
function deepMap(obj, iterator) { | |
// console.log(obj) | |
return _.transform(obj, function(result, val, key) { | |
console.log(val, key, _.isObject(val) && key) | |
result[key] = _.isObject(val) && key === "text" ? | |
deepMap(val, iterator) : | |
iterator.call(val); | |
}); | |
} | |
_.mixin({ | |
deepMap: deepMap | |
}); | |
const a = { | |
Author: 'Alex3917', | |
Title: null, | |
Text: '<p>If the success of your startup depends on getting a partnership with one company, you're probably doomed. Successful products are ones that create value for users, and value comes from startups. Creating a product where half the value comes from the startup and half comes from an established company usually leads to a product that's only 50% valuable.</p><p>If you don't believe me, ask Sam Altman at Loopt whether he'd still do a startup that depended on partnering with cellphone companies if he had to do it over again.</p><p>The only thing worse than pinning your chances on a big company creating the value for your product is pinning your chances on a big company providing distribution. Channels are for meeting demand, not creating it.</p>', | |
Url: null, | |
Comments: [ | |
{ | |
id: 1236, | |
created_at: '2007-02-26T17:29:06.000Z', | |
created_at_i: 1172510946, | |
type: 'comment', | |
author: 'pg', | |
title: null, | |
url: null, | |
text: '<p>I'm sure anyone who has to deal with cell carriers would say it was a pain in the ass. But that pain also kills off a lot of competitors. Much of Loopt's success is traceable to the fact that they were one of the few groups, if not the only one, who were both great hackers and willing to endure endless meetings with cell carriers.</p>', | |
points: null, | |
parent_id: 1234, | |
story_id: 1227, | |
children: [ | |
{ | |
id: 1239, | |
created_at: '2007-02-26T17:49:10.000Z', | |
created_at_i: 1172512150, | |
type: 'comment', | |
author: 'Alex3917', | |
title: null, | |
url: null, | |
text: '<p>It's sure one hell of a sustainable competitive advantage if you have the cojones to pull it off. That being said, I'd rather spend my time thinking about how I'm going to get my users laid rather than how to appeal to the self interest of a non-rational corporate entity.</p><p>update: That is, I work for a big company and you come to me and tell me that you're going to make my company a hundred million dollars. I say, gee, that's great, if this works out then I'll get my name on the wall and maybe a small bonus. And if it doesn't then I'll get fired, I won't be able to pay the mortgage and my wife will divorce me. So whatever your question, the answer is no. To make it work without a huge reputation and network and existing relationships you need to rely on a lot of luck.</p><p>My general outlook on this is that if you read Horatio Alger, the general formula is luck, pluck, and virtue. Which is great, but what's even better is if you can minimize luck and make your bread-and-butter off just pluck and virtue. Then whatever luck comes your way is just icing on the cake.</p>', | |
points: null, | |
parent_id: 1236, | |
story_id: 1227, | |
children: [], | |
options: [] | |
} | |
], | |
options: [] | |
} | |
] | |
} | |
const res = _.deepMap(a, format) | |
console.log(a, res) | |
async function hackernewsGetPostDataActionFunction(actionInputs, context) { | |
if (!actionInputs.hackerNewsStoryUrlOrId) { | |
return context.fail({ | |
message: "Missing Story Url or ID", | |
errorType: context.status.ERROR_INVALID_INPUT | |
}); | |
} | |
const hnPostUrlOrId = actionInputs.hackerNewsStoryUrlOrId; | |
// Check if Url is neither a HN Url nor a Post ID | |
if (!hnPostUrlOrId.includes('news.ycombinator.com') && isNaN(hnPostUrlOrId)) { | |
return context.fail({ | |
message: "Please include a Hacker News post url or ID.", | |
errorType: context.status.ERROR_INVALID_INPUT | |
}); | |
} | |
// HN Urls are of the form: https://news.ycombinator.com/item?id=23659871 | |
const hnPostId = !isNaN(hnPostUrlOrId) ? hnPostUrlOrId : hnPostUrlOrId.split('=')[1]; | |
let hnPostAPIUrl = `http://hn.algolia.com/api/v1/items/${hnPostId}`; | |
const algoliaResponse = await fetch(hnPostAPIUrl); | |
let hnPostJson = await algoliaResponse.json(); | |
if (hnPostJson.status == 404) { | |
return context.success({ | |
textPreview: "📖 Post does not exist", | |
}); | |
} | |
const data = { | |
'Author': hnPostJson.author, | |
'Title': hnPostJson.title, | |
'Text': hnPostJson.text, | |
'Url': hnPostJson.url, | |
'Comments': hnPostJson.children, | |
'Type': hnPostJson.type | |
}; | |
// Preview post title, or post type and author if title is null. For example, "comment by user1234" | |
const textPreview = hnPostJson.title ? hnPostJson.title : `${hnPostJson.type} by ${hnPostJson.author}`; | |
return context.success({ | |
data: data, | |
textPreview: `📖 ${textPreview}` | |
}); | |
} | |
module.exports = hackernewsGetPostDataActionFunction; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment