Build Your Own Social Media Scheduler with Python
While tools like Buffer or Hootsuite are great, sometimes you need a custom solution. Maybe you want to post based on weather data, stock market trends, or just want to avoid monthly subscription fees for features you don't use.
In this tutorial, we'll build a simple command-line interface (CLI) tool in Python that schedules videos to TikTok, YouTube, and Instagram using the XenonFlare API.
Prerequisites
- Python 3.8+
requestslibrary (pip install requests)- A XenonFlare API Key
The Plan
Our script will:
- Accept a video file path and metadata (title, description).
- Upload the video to XenonFlare.
- Schedule the post for a specific time.
Step 1: The Upload Function
First, we need a helper function to handle the video upload. XenonFlare makes this easy with a standard multipart upload.
import requests
import json
from datetime import datetime, timedelta
API_KEY = "YOUR_XENONFLARE_API_KEY"
BASE_URL = "https://api.xenonflare.com"
def upload_video(file_path):
print(f"Uploading {file_path}...")
url = f"{BASE_URL}/upload"
headers = {"Authorization": f"Bearer {API_KEY}"}
with open(file_path, 'rb') as f:
files = {'file': f}
response = requests.post(url, headers=headers, files=files)
if response.status_code == 200:
data = response.json()
print(f"Upload successful! Video ID: {data['id']}")
return data['url']
else:
raise Exception(f"Upload failed: {response.text}")
Step 2: The Scheduling Function
Now for the core logic. We'll create a function that takes the uploaded video URL and schedules it.
def schedule_post(video_url, title, platforms, scheduled_time):
print(f"Scheduling post to {platforms} at {scheduled_time}...")
url = f"{BASE_URL}/media/videos"
headers = {
"Authorization": f"Bearer {API_KEY}",
"Content-Type": "application/json"
}
payload = {
"video_url": video_url,
"title": title,
"description": "Start your own SaaS with XenonFlare! 🚀",
"platforms": platforms,
"schedule_time": scheduled_time.isoformat()
}
response = requests.post(url, headers=headers, json=payload)
if response.status_code == 201:
print("Successfully scheduled! 🎉")
print(json.dumps(response.json(), indent=2))
else:
print(f"Failed to schedule: {response.text}")
Step 3: Putting It All Together
Let's wrap it in a simple main block to run it.
if __name__ == "__main__":
# Configuration
VIDEO_FILE = "my_viral_video.mp4"
TITLE = "How to code your own tools 🤖"
PLATFORMS = ["tiktok", "youtube", "instagram"]
# Schedule for 24 hours from now
post_time = datetime.utcnow() + timedelta(hours=24)
try:
# 1. Upload
video_url = upload_video(VIDEO_FILE)
# 2. Schedule
schedule_post(video_url, TITLE, PLATFORMS, post_time)
except Exception as e:
print(f"Error: {e}")
Why Build Custom?
By building your own scheduler, you can integrate logic that SaaS tools can't offer:
- Dynamic Captions: Use OpenAI's GPT-4 to generate captions based on the video content right before posting.
- Trend Jacking: Scrape Google Trends and only post when a relevant keyword is trending.
- Watermarking: Use
ffmpegin Python to add a dynamic watermark before uploading.
Conclusion
With less than 50 lines of Python code, you've built a multi-platform social media scheduler. That's the power of the XenonFlare API—it handles the complexity of the social networks so you can focus on the logic.
Ready to start coding? Get your API Key and check out the full API Documentation.
