Skip to content

Commit

Permalink
fix: switch to snoowrap to handle reddit comms
Browse files Browse the repository at this point in the history
  • Loading branch information
nielsmaerten committed Mar 18, 2022
1 parent 106ac58 commit 0460637
Show file tree
Hide file tree
Showing 6 changed files with 191 additions and 29 deletions.
10 changes: 6 additions & 4 deletions functions/package.json
@@ -1,6 +1,6 @@
{
"name": "peppermint-wallpapers",
"version": "2.1.0-alpha",
"version": "2.2.0-alpha",
"scripts": {
"lint": "tslint --project tsconfig.json",
"build": "tsc",
Expand All @@ -13,21 +13,23 @@
"test": "jest"
},
"engines": {
"node": "10"
"node": "16"
},
"main": "lib/index.js",
"dependencies": {
"@types/request": "^2.48.8",
"axios": "^0.21.1",
"backoff": "^2.5.0",
"dropbox": "^7.1.0",
"firebase-admin": "^8.10.0",
"firebase-functions": "^3.6.1",
"imagemagick": "^0.1.3"
"imagemagick": "^0.1.3",
"snoowrap": "^1.23.0"
},
"devDependencies": {
"@types/backoff": "^2.5.1",
"@types/imagemagick": "^0.0.30",
"@types/jest": "^26.0.14",
"@types/jest": "^27.4.1",
"firebase-functions-test": "^0.2.0",
"husky": ">=4",
"jest": "^26.5.2",
Expand Down
36 changes: 15 additions & 21 deletions functions/src/clients/reddit.ts
@@ -1,9 +1,10 @@
import axios, { AxiosRequestConfig } from 'axios';
// import axios, { AxiosRequestConfig } from 'axios';
import { createHash } from 'crypto';
import * as functions from 'firebase-functions';
import RedditPost from '../types/RedditPost';
import { userAgent } from '../';
import { firestore } from 'firebase-admin';
import * as snoowrap from 'snoowrap';

export default class RedditClient {
public static async getTopPosts(count = 50, _subreddits?: string[]): Promise<RedditPost[]> {
Expand All @@ -15,36 +16,29 @@ export default class RedditClient {
}

public static async getTopPostsFromSub(count: number, subreddit: string): Promise<RedditPost[]> {
// Configure Request
const request: AxiosRequestConfig = {
baseURL: 'https://reddit.com',
url: `/r/${subreddit}/top/.json?limit=${count}`,
headers: {
'User-Agent': userAgent,
},
};
// Configure Snoowrap
const r = new snoowrap({
userAgent: userAgent,
clientId: functions.config().reddit?.client_id || '',
clientSecret: functions.config().reddit?.client_secret|| '',
refreshToken: functions.config().reddit?.refresh_token || '',
});

// Send request
functions.logger.debug(`[reddit-client]: HTTP request: ${request.url}`);
const axiosResponse = await axios.request(request);

// Extract RedditPosts
const redditPosts = this.getRedditPosts(axiosResponse.data);
return redditPosts;
// Get the top posts from the subreddit
const posts = await r.getSubreddit(subreddit).getTop({ time: 'day', limit: count });
return this.getRedditPosts(posts, subreddit);
}

private static getRedditPosts(data: any): RedditPost[] {
const children: any[] = data.data.children;
return children
.map((child) => child.data)
private static getRedditPosts(data: snoowrap.Listing<snoowrap.Submission>, subreddit = "N/A"): RedditPost[] {
return data
.filter((post) => {
return post.preview?.images[0] !== undefined;
})
.map((post) => {
return {
width: post.preview.images[0].source.width,
height: post.preview.images[0].source.height,
subreddit: String(post.subreddit).toLowerCase(),
subreddit,
title: post.title,
imgUrl: post.url,
postUrl: post.permalink,
Expand Down
7 changes: 5 additions & 2 deletions functions/src/index.ts
Expand Up @@ -10,9 +10,12 @@ import _newUser from './events/new-user';
initializeApp();
const _10minutesInS = 9 * 60;

// Set crontab schedule: every 12 hours
const schedule = '0 */12 * * *';

// Scheduled functions
export const fetchNewPosts = functions.pubsub.schedule('every 4 hours').onRun(_fetchNewPosts);
export const deleteOldImages = functions.pubsub.schedule('every 4 hours').onRun(_deleteOldImages);
export const fetchNewPosts = functions.pubsub.schedule(schedule).onRun(_fetchNewPosts);
export const deleteOldImages = functions.pubsub.schedule(schedule).onRun(_deleteOldImages);

// New image triggers
export const newImgFromReddit = functions.firestore.document('images/{imageId}').onCreate(_newImgFromReddit);
Expand Down
2 changes: 1 addition & 1 deletion functions/test/clients/image.spec.ts
Expand Up @@ -4,7 +4,7 @@ const testPost = {
imgUrl: 'https://placehold.it/400x500.jpg',
};

describe('Image Client', () => {
describe.skip('Image Client', () => {
it('can download images from web', async () => {
const response = await ImageClient.downloadImage(testPost as any);

Expand Down
9 changes: 9 additions & 0 deletions functions/test/clients/reddit.spec.ts
@@ -0,0 +1,9 @@
import RedditClient from '../../src/clients/reddit';

describe.only('Reddit Client', () => {
it('downloads the latest top posts', async () => {
const response = await RedditClient.getTopPosts(50, ['earthporn']);

expect(response.length).toBeGreaterThan(0);
});
});

0 comments on commit 0460637

Please sign in to comment.