Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/petarov/reddit2x
Automated posting of Reddit posts to X tweets using Firebase functions
https://github.com/petarov/reddit2x
auto-tweet automate-tweets bot reddit subreddit twitter twitter-bot x-twitter xwitter
Last synced: 3 days ago
JSON representation
Automated posting of Reddit posts to X tweets using Firebase functions
- Host: GitHub
- URL: https://github.com/petarov/reddit2x
- Owner: petarov
- License: mit
- Created: 2024-01-29T21:27:54.000Z (11 months ago)
- Default Branch: main
- Last Pushed: 2024-07-16T09:02:53.000Z (5 months ago)
- Last Synced: 2024-12-22T01:40:30.553Z (3 days ago)
- Topics: auto-tweet, automate-tweets, bot, reddit, subreddit, twitter, twitter-bot, x-twitter, xwitter
- Language: JavaScript
- Homepage:
- Size: 155 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
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](https://crontab.guru) for each function under `cron` or just leave the defaults.
## Firebase
Go to [console.firebase.google.com](https://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.
```json
{
"collectionGroup": "",
"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.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](https://support.reddithelp.com/hc/en-us/articles/16160319875092-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/](https://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](https://cloud.google.com/sdk).
To deploy the functions to Firebase run:
firebase deploy --only functions
# License
[MIT](LICENSE)