Gathering detailed insights and metrics for reddit-explorer
Gathering detailed insights and metrics for reddit-explorer
Gathering detailed insights and metrics for reddit-explorer
Gathering detailed insights and metrics for reddit-explorer
npm install reddit-explorer
Typescript
Module System
TypeScript (99.86%)
JavaScript (0.14%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
MIT License
4 Stars
61 Commits
1 Forks
1 Watchers
12 Branches
1 Contributors
Updated on Jan 13, 2025
Latest Version
1.5.0-alpha
Package Id
reddit-explorer@1.5.0-alpha
Unpacked Size
119.13 kB
Size
30.50 kB
File Count
219
Published on
Feb 05, 2024
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
2
5
reddit-explorer is a reddit client that allows you to fetch subreddit content easily. It only has 2 dependencies (typescript & axios).
To see what has been done on each version, you can consult the CHANGELOG.
npm install reddit-explorer
or
yarn add reddit-explorer
You have to pass your app credentials, see how to generate some:
At the bottom of the page click "create another app...", fill all the fields and then click "create app". You now see your clientId and secret.
For the moment reddit-explorer has 4 features:
access_token
string
I'm actively working on other features that are coming soon :)
1import { createRedditClient, SortingMethod, TimeRange, PostFilter, moreThanNComments } from "reddit-explorer" 2 3const reddit = createRedditClient({ 4 clientId: "<clientId>", 5 secret: "<secret>", 6})
You usually do not need this method, but can be useful if you want to see the authentication detail.
1reddit.getAccessToken() 2 3// { 4// "access_token": "-QFrCA5GkksABCEibAakz2fQbzUE62A", <-- You can use this an put it in the next bearer token's request 5// "token_type": "bearer", 6// "device_id": "5f615d62-fa4c-11eb-b8bc-0242ac130003", 7// "expires_in": 86400, 8// "scope": "*" 9// }
1reddit.getSubredditNames({ query: "photo" }) 2 .then((res) => console.log("photo", res)) 3 4// photo { 5// names: [ 6// 'photo', 7// 'photoshopbattles', 8// 'photography', 9// 'photocritique', 10// 'photoshop', 11// 'PhotoshopRequest', 12// 'PhotoshopTutorials', 13// 'photographs', 14// 'photomarket', 15// 'Photography101' 16// ] 17// }
There are three ways to achieve that:
1import { createRedditClient } from "reddit-explorer" 2 3const reddit = createRedditClient({ 4 clientId: "<clientId>", 5 secret: "<secret>", 6 matureContent: true, 7})
reddit.getSubredditNames({ query: "photo", include_over_18: true })
.then((res) => console.log("photo", res))
reddit.config.setMatureContent(true)
You have to pass a name and a SortingMethod
. Sorting method can be: Hot
, New
, Random
, Rising
, Top
or
Controversial
.
If the sorting method is Top
or Controversial
, you can also pass a TimeRange
. Time range can be: Hour
, Day
,
Week
, Month
, Year
, All
.
The package offers the type SubredditData
which is the type of all the [Object]
below (see at the end of the readme).
1reddit 2 .getSubreddit({ 3 name: "meme", 4 sortMethod: SortingMethod.New, 5 }) 6 .then((res) => console.log("meme", res)) 7 8// meme { 9// kind: 'Listing', 10// data: { 11// after: 't3_u9osas', 12// dist: 25, 13// modhash: '', 14// geo_filter: '', 15// children: [ 16// [Object], [Object], [Object], 17// [Object], [Object], [Object], 18// [Object], [Object], [Object], 19// [Object], [Object], [Object], 20// [Object], [Object], [Object], 21// [Object], [Object], [Object], 22// [Object], [Object], [Object], 23// [Object], [Object], [Object], 24// [Object] 25// ], 26// before: null 27// } 28// } 29 30// Example with a TimeRange: 31 32reddit 33 .getSubreddit({ 34 name: "meme", 35 sortMethod: SortingMethod.Top, 36 t: TimeRange.Day, 37 }) 38 .then((res) => console.log("meme", res))
If you want to keep only specific posts in the response, you can use postFilters
when creating the client. This
attribute accepts an array of functions, each one taking a SubredditData
into parameter and returning a boolean
,
(see the type PostFilter
). The package also comes with multiple PostFilterCreator
's, which are basically functions
that return a PostFilter
, such as moreThanNComments(100)
below:
1const moreThan2Crossposts: PostFilter = (subredditData) => subredditData.num_crossposts > 2
2
3const clientShowingOnlyPopularPosts = createRedditClient({
4 clientId: secrets.clientId,
5 secret: secrets.secret,
6 postFilters: [moreThanNComments(100), moreThan2Crossposts],
7})
The API can only load a max amount of 100 posts (the default is 25). To load the next posts, you have to pass the after
param.
You can find the value of the after
in the previous response of the request you made, in response.after
. It contains
the name
(SubredditData.name
) of the last post in the previous call.
1reddit
2 .getSubreddit({
3 name: "meme",
4 sortMethod: SortingMethod.New,
5 after: "t3_u9osas", // response.after of the previous call
6 })
7 .then((res) => console.log("meme", res))
If you do not want to memorize the after
attribute of the last response yourself, you can let the package handle it for
you, using getSubredditIterator
:
1const memeSubredditIterator = reddit.getSubredditIterator({ 2 name: "meme", 3 sortMethod: SortingMethod.New, 4 limit: 5, 5}) 6 7const memeResults0To4 = await memeSubredditIterator.next() 8const memeResults5To9 = await memeSubredditIterator.next() 9 10console.log("memeResults0To4", memeResults0To4.value) 11console.log("memeResults5To9", memeResults5To9.value) 12 13// memeResults0To4 { 14// kind: 'Listing', 15// data: { 16// after: 't3_uaslor', 17// dist: 5, 18// modhash: '', 19// geo_filter: '', 20// children: [ [Object], [Object], [Object], [Object], [Object] ], 21// before: null 22// } 23// } 24// memeResults5To9 { 25// kind: 'Listing', 26// data: { 27// after: 't3_uas9sy', 28// dist: 5, 29// modhash: '', 30// geo_filter: '', 31// children: [ [Object], [Object], [Object], [Object], [Object] ], 32// before: null 33// } 34// }
Instead of passing a string
to the name
argument, you can pass a string[]
to it:
1const res = await client.getSubreddit({ 2 sortMethod: SortingMethod.Top, 3 name: ["news", "meme"], 4})
createRedditSimpleClient
allows to get only the essential. This client will return a modified response containing only:
For the moment, the simple client only supports getSubreddit
and getSubredditNames
.
1const reddit = createRedditSimpleClient({ 2 clientId: "<clientId>", 3 secret: "<clientSecret>", 4}) 5 6const result = await reddit.getSubreddit({ 7 sortMethod: SortingMethod.Hot, 8 name: "memes", 9 fields: ["title", "url"], 10})
In the above example, I picked only the fields that I needed, here is the response:
1{ 2 before: null, 3 after: 't3_1aihq82', 4 data: [ 5 { 6 title: 'r/Memes is looking for new moderators! Interested? Fill out our application!', 7 url: 'https://docs.google.com/forms/d/e/1FAIpQLSfBlrL6LVOktwIdGubvbJ7REeh9vANiBTIpUecW63PHINQECg/viewform' 8 }, 9 { 10 title: 'Worst days in University', 11 url: 'https://i.redd.it/u8hlua9lbkgc1.jpeg' 12 }, 13 // some other results... 14 ], 15}
SubredditData
typeThis type offers the possibility to handle the response more easily (response.children[0]
):
1export type SubredditData = { 2 approved_at_utc: any 3 subreddit: string 4 selftext: string 5 author_fullname: string 6 saved: boolean 7 mod_reason_title: any 8 gilded: number 9 clicked: boolean 10 title: string 11 link_flair_richtext: any[] 12 subreddit_name_prefixed: string 13 hidden: boolean 14 pwls: number 15 link_flair_css_class: any 16 downs: number 17 thumbnail_height: number 18 top_awarded_type: any 19 hide_score: boolean 20 name: string 21 quarantine: boolean 22 link_flair_text_color: string 23 upvote_ratio: number 24 author_flair_background_color: any 25 subreddit_type: string 26 ups: number 27 total_awards_received: number 28 media_embed: null | { 29 content: IFrame 30 width: number 31 scrolling: boolean 32 height: number 33 } 34 thumbnail_width: number 35 author_flair_template_id: any 36 is_original_content: boolean 37 user_reports: any[] 38 secure_media: null | { 39 type: string 40 oembed: { 41 provider_url: string 42 version: string 43 title: string 44 type: string 45 thumbnail_width: number 46 height: number 47 width: number 48 html: IFrame 49 author_name: string 50 provider_name: string 51 thumbnail_url: string 52 thumbnail_height: number 53 author_url: string 54 } 55 } 56 is_reddit_media_domain: boolean 57 is_meta: boolean 58 category: any 59 secure_media_embed: null | { 60 content: IFrame 61 width: number 62 scrolling: boolean 63 media_domain_url: Url 64 height: number 65 } 66 link_flair_text: string 67 can_mod_post: boolean 68 score: number 69 approved_by: any 70 is_created_from_ads_ui: boolean 71 author_premium: boolean 72 thumbnail: Url 73 edited: boolean 74 author_flair_css_class: any 75 author_flair_richtext: any[] 76 gildings: { 77 gid_1: number 78 } 79 post_hint: string 80 content_categories: any 81 is_self: boolean 82 mod_note: any 83 crosspost_parent_list?: SubredditData[] 84 created: number 85 link_flair_type: string 86 wls: number 87 removed_by_category: any 88 banned_by: any 89 author_flair_type: string 90 domain: Url 91 allow_live_comments: boolean 92 selftext_html: any 93 likes: any 94 suggested_sort: any 95 banned_at_utc: any 96 url_overridden_by_dest: Url 97 view_count: any 98 archived: boolean 99 no_follow: boolean 100 is_crosspostable: boolean 101 pinned: boolean 102 over_18: boolean 103 preview: { 104 images: [ 105 { 106 source: { 107 url: Url 108 width: number 109 height: number 110 } 111 resolutions: { 112 url: Url 113 width: number 114 height: number 115 }[] 116 variants: { 117 obfuscated: { 118 source: { 119 url: Url 120 width: number 121 height: number 122 } 123 resolutions: { 124 url: Url 125 width: number 126 height: number 127 }[] 128 } 129 nsfw: { 130 source: { 131 url: Url 132 width: number 133 height: number 134 } 135 resolutions: { 136 url: Url 137 width: number 138 height: number 139 }[] 140 } 141 } 142 id: string 143 } 144 ] 145 reddit_video_preview: { 146 bitrate_kbps: number 147 fallback_url: string 148 height: number 149 width: number 150 scrubber_media_url: string 151 dash_url: string 152 duration: number 153 hls_url: string 154 is_gif: boolean 155 transcoding_status: string 156 } 157 enabled: boolean 158 } 159 all_awardings: any[] 160 awarders: any[] 161 media_only: boolean 162 can_gild: boolean 163 spoiler: boolean 164 locked: boolean 165 author_flair_text: null 166 treatment_tags: [] 167 visited: boolean 168 removed_by: null 169 num_reports: null 170 distinguished: null 171 subreddit_id: string 172 mod_reason_by: null 173 removal_reason: null 174 link_flair_background_color: string 175 id: string 176 is_robot_indexable: boolean 177 report_reasons: null 178 author: string 179 discussion_type: null 180 num_comments: number 181 send_replies: boolean 182 whitelist_status: string 183 contest_mode: boolean 184 mod_reports: [] 185 author_patreon_flair: boolean 186 crosspost_parent?: string 187 author_flair_text_color: null 188 permalink: string 189 parent_whitelist_status: string 190 stickied: boolean 191 url: Url 192 subreddit_subscribers: number 193 created_utc: number 194 num_crossposts: number 195 media: null | { 196 type: string 197 oembed: { 198 provider_url: Url 199 version: string 200 title: string 201 type: string 202 thumbnail_width: number 203 height: number 204 width: number 205 html: IFrame 206 author_name: string 207 provider_name: string 208 thumbnail_url: Url 209 thumbnail_height: number 210 author_url: Url 211 } 212 } 213 is_video: boolean 214 sr_detail: { 215 default_set: boolean 216 banner_img: string 217 restrict_posting: boolean 218 user_is_banned?: any 219 free_form_reports: boolean 220 community_icon?: any 221 show_media: boolean 222 description: string 223 user_is_muted?: any 224 display_name: string 225 header_img: string 226 title: string 227 previous_names: any[] 228 user_is_moderator?: any 229 over_18: boolean 230 icon_size: number[] 231 primary_color: string 232 icon_img: string 233 icon_color: string 234 submit_link_label: string 235 header_size: number[] 236 restrict_commenting: boolean 237 subscribers: number 238 submit_text_label: string 239 link_flair_position: string 240 display_name_prefixed: string 241 key_color: string 242 name: string 243 created: number 244 url: string 245 quarantine: boolean 246 created_utc: number 247 banner_size: number[] 248 user_is_contributor?: any 249 accept_followers: boolean 250 public_description: string 251 link_flair_enabled: boolean 252 disable_contributor_requests: boolean 253 subreddit_type: string 254 user_is_subscriber?: any 255 } 256}
Here you'll find the official documentation of the reddit official endpoints that this package uses:
No vulnerabilities found.
No security vulnerabilities found.