DEV Community

orca forge
orca forge

Posted on Originally published at forge.workstyle.tech

How to Build a Personal Scheduled SNS Video Posting Setup

📝 Originally published (in Japanese) at forge.workstyle.tech.

TL;DR This setup uses a development mode app to post videos to accounts I manage. It separates token handling from posting, checks videos before publishing, and records results. The implementation alone cannot establish where review is not required, so check the current official documentation for permissions and review conditions.

The Short Answer

You can set up an app in development mode to post videos on a schedule to an account you manage. My implementation notes alone aren’t enough to generalize which use cases are available without review, so check the official documentation for permission and review requirements before using this setup with accounts other than your own.

How I Separate Authentication from Posting

In my setup, the authentication process prepares the tokens, and a posting script uploads and publishes the video. A token is a string used for authentication. I use the operating system’s scheduler to run the script every day at 7:00 PM. This article describes a setup that uses an app in development mode to post to an account I manage.

Whether this setup works depends on factors such as account roles and permissions. My implementation notes alone aren’t enough to generalize when it can be used without review. If you want to post to accounts other than your own, check the current official documentation for the required permissions and review requirements.

For authentication, I set up an app and the necessary APIs in the developer dashboard, then connect my account. In my setup, the authentication process uses a short-lived user token, then exchanges it for a user token that’s valid for about 60 days.

The authentication process uses that token to retrieve the pages I manage. It also retrieves the page token and the account ID for the photo-sharing social network linked to the page, then saves them to a secrets file. The posting process reads this file. Token values are never printed to the logs.

If you put tokens directly in the posting code, someone reading the code or logs could see them. Keeping the secrets file separate from the posting process means you can inspect the posting code without exposing authentication credentials.

I handle the short-form social network’s token separately. In my authentication process, I use the token to retrieve account information before trying to extend it. If the extension fails, the process continues and records the result. Don’t assume it follows the same steps as a user token; follow the specifications for each token type.

How to review a video and post copy before publishing

Before calling the API, my auto-publishing script validates its inputs. It checks that the post copy is not empty, that the video file exists, and that the video’s MD5 (a fingerprint calculated from the file contents) matches the value in the approved list. If any check fails, the script skips publishing for that run.

If a record for the same date and destination already exists, rerunning the script won’t publish the same post again. When running it manually for a specific date, I first use dry-run mode to review the content.

node <posting script> --date YYYY-MM-DD --dry
Enter fullscreen mode Exit fullscreen mode

A run with --dry won’t publish anything. You can review the scheduled date, destination, video filename, and post copy character count. To limit the run to specific destinations, pass them to --only.

I do this review first for more than just catching failed API calls. It also stops publication if the video was replaced after approval or if the post copy is still empty. In my code, I use MD5 to check that the video matches the approved one. If you use a different setup, make sure it can still compare the content with the approval at runtime.

How the video publishing process differs by destination

Although each destination has a different API, the publishing process generally consists of three steps: uploading the video, checking its processing status, and publishing it.

For the photo-sharing SNS, the process starts by creating a media container for the Reel. The code retrieves the video’s size, uploads it using a resumable method, checks the container’s status, and then publishes it. After publishing, it retrieves the post details and records them in the log.

For the page-based SNS, the process tells the API to start the upload, sends the video data, and then completes the upload in a published state. It retrieves the publishing status, and if an error occurs, treats that destination’s operation as failed.

For the short-form SNS, the video is not passed directly from a local file. Instead, it is temporarily uploaded to public storage, and a media container is created using its URL. After checking the container’s status and publishing it, the code deletes the temporary object in finally. It attempts to delete the object even if an error occurs before publishing.

Receiving a container ID from the API does not necessarily mean the post has been published. In my code, the processing status is checked at regular intervals, and the publishing API is called only once the container is ready to publish. My implementation checks the status every five seconds. That interval is what the code uses; it does not mean that all APIs require the same interval.

How to Check the Daily 7 PM Run and Weekly Token Refresh

After running the posting script manually and confirming that it works, schedule it to run every day at 7 PM using your operating system’s scheduler. Use the date in Japan Standard Time to determine which day’s scheduled post to load. My shell script also receives the date in Japan Standard Time.

Run the weekly authentication refresh separately from the posting process. My shell script calls the authentication process on Sundays to refresh the token. It also records the result of extending the short-form social network token. To catch refresh failures, check the authentication results as well as the posting logs.

After setting up the scheduled job, run it manually at least once to confirm that it works in the same environment as the scheduler. When my shell script fails, it attempts to send a notification and returns an exit code to the caller. The PATH and working directory can differ depending on the execution environment, so success in your local terminal does not guarantee that the scheduled run will succeed.

How to Tell from Logs Whether a Post Succeeded or Was Duplicated

The posting process records which destinations received a post successfully. The run log stores the result for each date and destination. In my records, posts succeeded on three days, from October 14 through 16. On some days, I posted to all three destinations; on another day, I posted to only two because nothing was scheduled for the third.

I check separately whether the script started and whether the post was published. Errors before posting go to standard error, and successful posts are logged for each destination. I review the logs regularly so I can identify which destinations failed.

Before rerunning the process manually, I check the log for an entry with the same date and destination. If a duplicate-prevention record exists, the process skips that destination. When it’s unclear whether a post was actually published, I don’t keep rerunning the script based on the logs alone. I also check the destination’s status before taking action.

What I learned from this setup is that automated posting involves more than just API calls. Handling credentials separately, checking the video before publication, confirming completion for each destination, and recording the results are all part of the process. Even when using development mode with only my own account, check the scope and permission requirements before using it.

Key takeaways

  • Check whether you are posting to an account you manage, and review the scope and permission requirements in the official documentation.
  • Prepare tokens in the authentication process and confirm they are stored in a secrets file.
  • Use --dry to check the planned date, destinations, video filename, and post text length.
  • Run the script manually in the scheduled environment and review posting logs and token refresh results.
  • Before rerunning, check the logs and destination status to avoid duplicate posts.

Top comments (0)