Spaces:
Running
Running
| const axios = require('axios'); | |
| class PostProxyPoster { | |
| constructor() { | |
| this.apiKey = process.env.POSTPROXY_API_KEY; | |
| this.baseUrl = 'https://api.postproxy.dev/v1'; | |
| } | |
| async getProfiles() { | |
| try { | |
| const response = await axios.get(`${this.baseUrl}/profiles`, { | |
| headers: { 'Authorization': `Bearer ${this.apiKey}` } | |
| }); | |
| return { success: true, profiles: response.data }; | |
| } catch (error) { | |
| console.error('[PostProxy] Get profiles failed:', error.response?.data || error.message); | |
| return { success: false, error: error.message }; | |
| } | |
| } | |
| async createPost({ content, mediaUrls = [], profiles = [] }) { | |
| try { | |
| const payload = { | |
| body: content, | |
| media: mediaUrls, | |
| profiles: profiles // Array of profile IDs or slugs like 'linkedin', 'twitter' | |
| }; | |
| const response = await axios.post(`${this.baseUrl}/publish`, payload, { | |
| headers: { | |
| 'Authorization': `Bearer ${this.apiKey}`, | |
| 'Content-Type': 'application/json' | |
| } | |
| }); | |
| return { success: true, postId: response.data.id, data: response.data }; | |
| } catch (error) { | |
| console.error('[PostProxy] Create post failed:', error.response?.data || error.message); | |
| return { success: false, error: error.message }; | |
| } | |
| } | |
| async getPostAnalytics(postId) { | |
| try { | |
| const response = await axios.get(`${this.baseUrl}/posts/${postId}/analytics`, { | |
| headers: { 'Authorization': `Bearer ${this.apiKey}` } | |
| }); | |
| const d = response.data; | |
| return { | |
| success: true, | |
| analytics: { | |
| likes: d.likes || d.reactions || 0, | |
| comments: d.comments || d.replies || 0, | |
| shares: d.shares || d.reposts || d.retweets || 0, | |
| reach: d.reach || d.impressions || 0, | |
| impressions: d.impressions || 0 | |
| } | |
| }; | |
| } catch (error) { | |
| console.error('[PostProxy] Get analytics failed:', error.response?.data || error.message); | |
| return { success: false, error: error.message }; | |
| } | |
| } | |
| } | |
| module.exports = new PostProxyPoster(); | |