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

Add test sample output #8

Merged
merged 2 commits into from Sep 16, 2016
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
44 changes: 44 additions & 0 deletions test.js
Expand Up @@ -5,11 +5,55 @@ fetchSubreddit(['worldnews', 'sports'])
.then((urls) => console.log(urls))
.catch((err) => console.error(err));

/** OUTPUT:

[
{
"subreddit": "worldnews",
"urls": [
"http://www.mirror.co.uk/news/world-news/north-korea-begs-world-aid-8847251",
...
"http://www.bbc.com/news/world-asia-china-37370201"
]
},
{
"subreddit": "sports",
"urls": [
"http://i.imgur.com/ZP2Xgbf.jpg",
...
"http://www.bbc.com/sport/cycling/37371057"
]
}
]

*/

/** ---------- ---------- ---------- ---------- ---------- ---------- ---------- */

fetchRandomSubredditName(2)
.then((res) => pretty(res))
.then((res) => console.log(res))
.catch((err) => console.error(err));

/** OUTPUT:

[
{
"json": "https://www.reddit.com/r/titanfall/.json",
"name": "titanfall",
"url": "https://www.reddit.com/r/titanfall/"
},
{
"json": "https://www.reddit.com/r/Gunpla/.json",
"name": "Gunpla",
"url": "https://www.reddit.com/r/Gunpla/"
}
]

*/

/** ---------- ---------- ---------- ---------- ---------- ---------- ---------- */

// fetchRandomNSFWSubredditName()
// .then((res) => pretty(res))
// .then((res) => console.log(res))
Expand Down
77 changes: 73 additions & 4 deletions test2.js
Expand Up @@ -2,8 +2,77 @@ const { fetchSubreddit, fetchRandomSubredditName, pretty } = require('./index');

// Fetch 2 random subreddits via /r/random, then grab each subreddits's JSON feed.
fetchRandomSubredditName(2)
.then((res) => res.map(({name}) => name))
.then((subreddits) => fetchSubreddit(subreddits))
.then((res) => pretty(res))
.then((res) => console.log(res))
.then((subreddits) => fetchSubreddit(subreddits.map(({name}) => name)))
.then((subreddits) => pretty(subreddits))
.then((output) => console.log(output))
.catch((err) => console.error(err));

/** OUTPUT:

[
{
"subreddit": "swift",
"urls": [
"https://www.reddit.com/r/swift/comments/52oeqb/whats_everyone_working_on_this_week_9132016/",
"https://vapor.codes",
"https://www.reddit.com/r/swift/comments/53243l/alarm_clock_ios10/",
"https://swiftweekly.github.io/issue-38/",
"https://robots.thoughtbot.com/introducing-perform-easy-dependency-injection-for-storyboard-segues",
"https://www.hackingwithswift.com/read",
"https://www.reddit.com/r/swift/comments/5337k0/question_modelling_logic_conditional_types/",
"https://www.reddit.com/r/swift/comments/5337e5/macbook_or_pc_laptop_vmware/",
"https://www.reddit.com/r/swift/comments/5332dl/firebase_360_release_notes/",
"https://swiftnews.curated.co/issues/101#start",
"https://www.reddit.com/r/swift/comments/531fqf/fastlane/",
"https://www.reddit.com/r/swift/comments/531pqg/when_will_realm_be_available_for_swift_3/",
"https://www.reddit.com/r/swift/comments/530u1k/sorting_an_array_of_objects/",
"https://www.reddit.com/r/swift/comments/52yp67/iad_discontinued_what_to_do/",
"https://www.reddit.com/r/swift/comments/52w3qy/swift_2_swift_3/",
"https://www.reddit.com/r/swift/comments/52w2gq/removal_of_noreturn_in_swift_30_broke_my_use_case/",
"https://www.reddit.com/r/swift/comments/52xt8w/opinions_on_swift_playgrounds_on_ipad/",
"https://www.reddit.com/r/swift/comments/52z8x5/lookin_4_a_site_of_swift_projectstutorials/",
"https://www.reddit.com/r/swift/comments/52wa88/good_resources_for_learning_swift_language/",
"https://www.reddit.com/r/swift/comments/52xc72/swift_23_swift_3_gamekit_get_the_default/",
"https://www.reddit.com/r/swift/comments/52v1g3/json_encoding_nested_dictionaries_problem/",
"https://oleb.net/blog/2016/09/swift-3-ranges/",
"https://www.reddit.com/r/swift/comments/52ubys/custom_objects/",
"https://swift.org/blog/swift-3-0-released/",
"https://www.reddit.com/r/swift/comments/52vejm/question_working_with_bluetooth_scanner/",
"https://www.reddit.com/r/swift/comments/52u142/question_how_to_create_beautiful_alerts/"
]
},
{
"subreddit": "NarutoFanfiction",
"urls": [
"https://www.reddit.com/r/NarutoFanfiction/comments/4e4b2h/story_ideas_thread/",
"https://www.reddit.com/r/NarutoFanfiction/comments/531kis/round_6_sacrifice_by_scanndalus/",
"https://www.reddit.com/r/NarutoFanfiction/comments/532xig/what_kind_of_author_do_you_want_to_be/",
"https://www.reddit.com/r/NarutoFanfiction/comments/531rnb/a_wild_parody_appears_read_tales_of_a_gutsy_ninja/",
"https://www.reddit.com/r/NarutoFanfiction/comments/5328f7/are_there_any_fics_where_naruto_solves_his/",
"https://www.reddit.com/r/NarutoFanfiction/comments/530ax5/leader_naruto_fic/",
"https://www.reddit.com/r/NarutoFanfiction/comments/52z9cu/eyes_of_pain_chapter_3/",
"https://www.reddit.com/r/NarutoFanfiction/comments/52xz3b/id_like_some_feedback_game_of_thrones_x_naruto/",
"https://www.reddit.com/r/NarutoFanfiction/comments/52wzem/bijuu_unbound/",
"https://www.reddit.com/r/NarutoFanfiction/comments/52yst7/sasuke_focus_stories/",
"https://www.reddit.com/r/NarutoFanfiction/comments/52v5ju/whats_the_weirdest_crossover_fic_youve_ever_seen/",
"https://www.reddit.com/r/NarutoFanfiction/comments/52uy7h/would_a_naruto_that_trains_when_he_is_young_and/",
"https://www.reddit.com/r/NarutoFanfiction/comments/52qlas/cheering_for_the_villain/",
"https://www.reddit.com/r/NarutoFanfiction/comments/52rv42/ive_had_a_really_weird_idea_for_a_fic_i_was/",
"https://www.reddit.com/r/NarutoFanfiction/comments/52q0uz/wp_danzo_goes_full_trump_in_konoha/",
"https://www.reddit.com/r/NarutoFanfiction/comments/52pscp/ive_updated_homegrown_garden_tails/",
"https://www.reddit.com/r/NarutoFanfiction/comments/52oyhr/he_will_surpass_a_bunch_of_derivative_literary/",
"https://www.reddit.com/r/NarutoFanfiction/comments/52nr3f/what_takes_you_out_of_fight_scenes_what_are_the/",
"https://www.reddit.com/r/NarutoFanfiction/comments/52ne7m/im_looking_for_stories_where_the_sandaime_teaches/",
"https://www.reddit.com/r/NarutoFanfiction/comments/52ol82/any_good_akatsukiakatsuchi_fics/",
"https://www.reddit.com/r/NarutoFanfiction/comments/52n3x4/so_theres_a_difference_between_good_cliffhangers/",
"https://www.reddit.com/r/NarutoFanfiction/comments/52mv1t/im_looking_for_fics_where_naruto_ends_up_getting/",
"https://www.reddit.com/r/NarutoFanfiction/comments/52o497/what_do_you_love_the_most_about_writing_in_general/",
"https://www.reddit.com/r/NarutoFanfiction/comments/52likv/followup_to_subreddit_wiki/",
"https://www.reddit.com/r/NarutoFanfiction/comments/52mvoa/fic_request_any_stories_where_naruto_gets_turned/",
"https://www.reddit.com/r/NarutoFanfiction/comments/52nyrv/naruto_character_reading_or_watches/",
"https://www.reddit.com/r/NarutoFanfiction/comments/52jnrh/tme_too_much_exposition/"
]
}
]

*/
68 changes: 68 additions & 0 deletions test3.js
@@ -0,0 +1,68 @@
const urlParse = require('url').parse;

const { fetchSubreddit, fetchRandomSubredditName, pretty } = require('./index');

// Fetch 2 random subreddits via /r/random, then grab each subreddits's JSON feed.
fetchRandomSubredditName(2)
// Pluck out just the `name` property from each subreddit.
.then((subreddits) => subreddits.map(({name}) => name))
// Fetch each subreddit's JSON feed.
.then((subreddits) => fetchSubreddit(subreddits))
// Run a reducer on the fetched URLs and group by hostname.
.then((subreddits) => subreddits.map(({subreddit, urls}) => {
return {
subreddit,
urls: urls.reduce(_subredditReducer, {})
};
}))
// Optional: Reduce all subreddits into a single object.
.then((subreddits) => subreddits.reduce(_summaryReducer, {}))
.then((subreddits) => pretty(subreddits))
.then((output) => console.log(output))
.catch((err) => console.error(err));

function _subredditReducer(prev, curr) {
const hostname = urlParse(curr).hostname;
prev[hostname] = prev[hostname] || 0;
prev[hostname] += 1;
return prev;
}

function _summaryReducer(prev, {urls}) {
Object.keys(urls).forEach((hostname) => {
prev[hostname] = prev[hostname] || 0;
prev[hostname] += urls[hostname];
});
return prev;
}

/** OUTPUT:

[
{
"subreddit": "Disney_Infinity",
"urls": {
"www.reddit.com": 17,
"imgur.com": 9,
"www.ign.com": 1
}
},
{
"subreddit": "AlienBlue",
"urls": {
"www.reddit.com": 25,
"i.reddituploads.com": 1
}
}
]

Or, if you run the optional extra reducer above, all the subreddits are merged into a single object with combined counts:

{
"www.reddit.com": 42,
"imgur.com": 9,
"www.ign.com": 1,
"i.reddituploads.com": 1
}

*/