Hey there, fellow developer! Ready to supercharge your social media management with Loomly's API? Let's dive into building a Python integration that'll make your life easier and your social media game stronger.
Before we jump in, make sure you've got:
requests
library (pip install requests
)First things first, let's get you authenticated:
import requests API_KEY = 'your_api_key_here' headers = { 'Authorization': f'Bearer {API_KEY}', 'Content-Type': 'application/json' }
Pro tip: Keep that API key safe! Use environment variables in production.
Let's start with the basics. Here's how you make a GET request:
response = requests.get('https://api.loomly.com/v1/calendars', headers=headers) print(response.json())
And here's a POST request to create a post:
data = { "post": { "calendar_id": 123, "content": "Check out our new product!", "scheduled_at": "2023-06-01T10:00:00Z" } } response = requests.post('https://api.loomly.com/v1/posts', headers=headers, json=data) print(response.json())
Now that you've got the hang of it, let's tackle some core functionalities:
def get_calendars(): response = requests.get('https://api.loomly.com/v1/calendars', headers=headers) return response.json()
def create_post(calendar_id, content, scheduled_at): data = { "post": { "calendar_id": calendar_id, "content": content, "scheduled_at": scheduled_at } } response = requests.post('https://api.loomly.com/v1/posts', headers=headers, json=data) return response.json()
def update_post(post_id, content): data = { "post": { "content": content } } response = requests.patch(f'https://api.loomly.com/v1/posts/{post_id}', headers=headers, json=data) return response.json()
def delete_post(post_id): response = requests.delete(f'https://api.loomly.com/v1/posts/{post_id}', headers=headers) return response.status_code == 204
Ready to level up? Let's explore some advanced features:
def schedule_post(calendar_id, content, scheduled_at): return create_post(calendar_id, content, scheduled_at)
def upload_asset(file_path): with open(file_path, 'rb') as file: files = {'file': file} response = requests.post('https://api.loomly.com/v1/assets', headers=headers, files=files) return response.json()
Always expect the unexpected! Here's a simple error handling wrapper:
def api_request(method, url, **kwargs): try: response = requests.request(method, url, headers=headers, **kwargs) response.raise_for_status() return response.json() except requests.exceptions.RequestException as e: print(f"API request failed: {e}") return None
Remember to respect rate limits and implement exponential backoff for retries.
Let's put it all together with a simple post scheduler:
import datetime def schedule_weekly_post(calendar_id, content, day_of_week, time): today = datetime.date.today() days_ahead = day_of_week - today.weekday() if days_ahead <= 0: days_ahead += 7 next_date = today + datetime.timedelta(days=days_ahead) scheduled_at = datetime.datetime.combine(next_date, time).isoformat() return schedule_post(calendar_id, content, scheduled_at) # Usage schedule_weekly_post(123, "It's Friday! #TGIF", 4, datetime.time(10, 0))
Don't forget to test your integration! Here's a simple unit test to get you started:
import unittest class TestLoomlyIntegration(unittest.TestCase): def test_get_calendars(self): calendars = get_calendars() self.assertIsNotNone(calendars) self.assertIsInstance(calendars, list) if __name__ == '__main__': unittest.main()
And there you have it! You've just built a robust Loomly API integration in Python. With these tools at your disposal, you're ready to automate your social media management like a pro.
Remember, this is just the beginning. Explore the Loomly API documentation for more endpoints and features to integrate. Happy coding, and may your social media presence be ever-growing!