Created
October 1, 2023 18:13
-
-
Save virtuallyunknown/d68b417e1800d1d5cb76616c6f6dbbbc 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
import axios from 'axios'; | |
class RedditClient { | |
constructor({ clientId, clientSecret, refreshToken, userAgent }) { | |
this.accessTokenExpire = new Date(Date.now() - 1000); | |
this.authToken = Buffer.from(`${clientId}:${clientSecret}`).toString('base64'); | |
this.refreshToken = refreshToken; | |
this.userAgent = userAgent; | |
} | |
createParams(params) { | |
return new URLSearchParams( | |
Object.entries(params).map(([key, value]) => [key, value.toString()]) | |
); | |
} | |
async ensureAccessToken() { | |
try { | |
if (!this.accessToken || new Date() > this.accessTokenExpire) { | |
console.log('Acquiring Reddit access token.'); | |
const res = await axios({ | |
url: 'https://www.reddit.com/api/v1/access_token', | |
method: 'post', | |
headers: { | |
'User-Agent': this.userAgent, | |
'Content-Type': 'application/x-www-form-urlencoded', | |
'Authorization': `Basic ${this.authToken}`, | |
}, | |
params: this.createParams({ | |
grant_type: 'refresh_token', | |
refresh_token: this.refreshToken | |
}) | |
}); | |
this.accessToken = res.data.access_token; | |
this.accessTokenExpire = new Date(Date.now() + res.data.expires_in * 1000); | |
} | |
} catch (error) { | |
console.error(error); | |
} | |
} | |
async submitComment(data) { | |
try { | |
await this.ensureAccessToken(); | |
const res = await axios({ | |
url: 'https://oauth.reddit.com/api/comment', | |
method: 'post', | |
headers: { | |
'User-Agent': this.userAgent, | |
'Authorization': `bearer ${this.accessToken}`, | |
'Content-Type': 'application/x-www-form-urlencoded', | |
}, | |
params: this.createParams({ | |
api_type: 'json', | |
text: data.text, | |
thing_id: `t3_${data.thing_id}`, | |
}) | |
}); | |
return res.data.json.data.things[0].data; | |
} catch (error) { | |
console.error(error); | |
} | |
} | |
async distinguishComment(data) { | |
try { | |
await this.ensureAccessToken(); | |
await axios({ | |
url: 'https://oauth.reddit.com/api/distinguish', | |
method: 'post', | |
headers: { | |
'User-Agent': this.userAgent, | |
'Authorization': `bearer ${this.accessToken}`, | |
'Content-Type': 'application/x-www-form-urlencoded', | |
}, | |
params: this.createParams({ | |
api_type: 'json', | |
how: 'yes', | |
id: `t1_${data.id}`, | |
sticky: true | |
}) | |
}); | |
} catch (error) { | |
console.error(error); | |
} | |
} | |
async lockComment(data) { | |
try { | |
await this.ensureAccessToken(); | |
await axios({ | |
url: 'https://oauth.reddit.com/api/lock', | |
method: 'post', | |
headers: { | |
'User-Agent': this.userAgent, | |
'Authorization': `bearer ${this.accessToken}`, | |
'Content-Type': 'application/x-www-form-urlencoded', | |
}, | |
params: this.createParams({ | |
id: `t1_${data.id}` | |
}) | |
}); | |
} catch (error) { | |
console.error(error); | |
} | |
} | |
async submitThread({ title, body }) { | |
try { | |
await this.ensureAccessToken(); | |
await axios({ | |
url: 'https://oauth.reddit.com/api/submit', | |
method: 'post', | |
headers: { | |
'User-Agent': this.userAgent, | |
'Authorization': `bearer ${this.accessToken}`, | |
'Content-Type': 'application/x-www-form-urlencoded', | |
}, | |
params: this.createParams({ | |
api_type: 'json', | |
kind: 'self', | |
sr: 'subreddit', | |
title: title, | |
text: body | |
}) | |
}); | |
} catch (error) { | |
console.error(error); | |
} | |
} | |
} | |
export const Reddit = new RedditClient({ | |
clientId: 'client id', | |
clientSecret: 'client secret', | |
refreshToken: 'refresh token', | |
userAgent: 'your user agent' | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment