File size: 2,780 Bytes
44b508a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
from flask import Flask, render_template, request, jsonify
import requests
import base64
import os

app = Flask(__name__)

# Instagram Graph API settings
ACCESS_TOKEN = "EACN93e8btl8BOzqCVifPdgeg6U8ikAgJZACLFU0mrBZBuwT9iF21nFOLLF8RnYfbHgaUuLkoddZC16pe9hkntYmHfnMLADH730oHfmQ0DjKTX4cAC8e7p6UpqdfHADjeFZCkRbXfMTRZCzv7wMSqSpeEBGxOd0PREr6qsenXc0IZATCw01QNQZAff2hhlBV4oP1"
INSTAGRAM_ACCOUNT_ID = "YOUR_INSTAGRAM_ACCOUNT_ID"  # Replace with your Instagram Business Account ID

@app.route('/')
def index():
    return render_template('index.html')

@app.route('/upload', methods=['POST'])
def upload_image():
    try:
        # Get the base64 image data from the frontend
        image_data = request.form['image']
        image_data = image_data.split(',')[1]  # Remove the data:image/jpeg;base64, prefix
        image_binary = base64.b64decode(image_data)

        # Save the image temporarily
        temp_image_path = 'temp_image.jpg'
        with open(temp_image_path, 'wb') as f:
            f.write(image_binary)

        # Step 1: Upload image to Instagram as a media object
        media_url = f"https://graph.facebook.com/v20.0/{INSTAGRAM_ACCOUNT_ID}/media"
        media_payload = {
            'image_url': 'https://your-image-hosting-url/temp_image.jpg',  # You need to host the image publicly
            'caption': 'Uploaded via Camera App #HuggingFace #Instagram',
            'access_token': ACCESS_TOKEN
        }
        
        # For Hugging Face, you may need to upload the image to a temporary public URL
        # For simplicity, assuming the image is accessible; in production, use a service like S3 or a temporary file server
        response = requests.post(media_url, data=media_payload)
        response_data = response.json()

        if 'id' not in response_data:
            return jsonify({'error': 'Failed to create media object', 'details': response_data}), 500

        media_id = response_data['id']

        # Step 2: Publish the media object
        publish_url = f"https://graph.facebook.com/v20.0/{INSTAGRAM_ACCOUNT_ID}/media_publish"
        publish_payload = {
            'creation_id': media_id,
            'access_token': ACCESS_TOKEN
        }
        publish_response = requests.post(publish_url, data=publish_payload)
        publish_data = publish_response.json()

        # Clean up temporary file
        if os.path.exists(temp_image_path):
            os.remove(temp_image_path)

        if 'id' in publish_data:
            return jsonify({'message': 'Image uploaded to Instagram successfully!'})
        else:
            return jsonify({'error': 'Failed to publish media', 'details': publish_data}), 500

    except Exception as e:
        return jsonify({'error': str(e)}), 500

if __name__ == '__main__':
    app.run(debug=True)