Skip to content

Automated posting of Reddit posts to X tweets using Firebase functions

License

Notifications You must be signed in to change notification settings

petarov/reddit2x

Repository files navigation

Reddit2X

A set of cloud functions that help automate a synchronization workflow from Reddit to X (Twitter) or Xwitter. You can use this to have a subreddit's posts automatically get posted to your Xwitter profile.

How does it work?

It uses Firestore and Functions to synchronize and publish posts on regular intervals.

There are 3 functions in total:

  • xpublish: publishes new x posts
  • redditdownloadandsave: downloads the last reddit.maxPosts posts from Reddit and saves the new ones to Firebase as unpublished
  • trasherdeleteoldposts: removes reddit posts older than reddit.postTTLDays from Firebase to save on space

Setup

Go to ./functions in the repo and copy config.json.template to config.json.

Configure the cron schedule for each function under cron or just leave the defaults.

Firebase

Go to console.firebase.google.com and create a new project.

Create the firebase.collectionName first. An index is required. You can check the firestore.indexes.json file or create it yourself.

    {
      "collectionGroup": "<your-collection-name>",
      "queryScope": "COLLECTION",
      "fields": [
        {
          "fieldPath": "is_on_x",
          "order": "ASCENDING"
        },
        {
          "fieldPath": "created_at",
          "order": "ASCENDING"
        }
      ]
    }

Note the firebase.postTTLDays config parameter - if the subbreddit is not quite active, you'd like to keep this value high. Posts that are cleaned up from Firebase will be recongnized as new the next time redditdownloadandsave runs.

Reddit

Create a new Reddit app and add reddit.clientId, reddit.clientSecret and reddit.userAgent to your config.json file. A Reddit access token will be created automatically and renewed every 24 hours.

See Reddit Data API Wiki for more details.

You can limit which posts are considered for posting and which not: reddit.minUpvoteRatio is checked first and then reddit.minUpvotes. Posts with insufficient upvote ratio or upvotes will be skipped.

Xwitter

Xwitter is a real bummer to set up. You need to use the new OAuth 2.0 authentication scheme to get an access token that is only valid for about 2 hours. You will also get a refresh token with which you can get a new access token.

Go to developer.twitter.com/ and create a new app.

Go to User authentication settings in the app and make sure the Type of App is set to Web App, Automated App or Bot. Use http://localhost:3000/callback as value for the Callback URI / Redirect URL field in App Info.

Go to Keys and tokens in the app ane generate your OAuth 2.0 Client ID and Client Secret. Copy those two to your config.json file.

Run node x-auth.js to initiate the access and refresh tokens generation process. Check the messages in the CLI on how to proceed. You'll need to manually open the url presented in the console in a browser in order to approve your app at Xwitter. After that, Xwitter will call the localhost url above to send back the tokens. This means you'll need to return to the console window and copy the accessToken, refreshToken, accessTokenCreateTime and accessTokenExpiresIn to your config.json file to finish the setup.

Note: The xpublish function checks for the validity of the access token before posting, however be advised that you should not post in intervals bigger than 2 hours. There seems to be a bug at Xwitter that prevents refresh tokens from working after the access token has expired.

Do not set a value greater than 140 for twitter.maxPostLen. The API does not support larger posts, yet.

Deploy

Install the google-cloud-sdk.

To deploy the functions to Firebase run:

firebase deploy --only functions

License

MIT