File size: 2,103 Bytes
ee51295
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
67f580a
 
ee51295
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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, redirect, url_for, flash, send_from_directory
import os
from werkzeug.utils import secure_filename
from instagrapi import Client
from PIL import Image
import time

app = Flask(__name__)
app.config['SECRET_KEY'] = 'your-secret-key'
app.config['UPLOAD_FOLDER'] = 'uploads'
app.config['MAX_CONTENT_LENGTH'] = 16 * 1024 * 1024  # 16MB max file size

# Ensure upload folder exists
os.makedirs(app.config['UPLOAD_FOLDER'], exist_ok=True)

# Instagram credentials (replace with your own)
INSTAGRAM_USERNAME = ""
INSTAGRAM_PASSWORD = ""

# Initialize instagrapi client
cl = Client()
cl.login(INSTAGRAM_USERNAME, INSTAGRAM_PASSWORD)

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

@app.route('/upload', methods=['POST'])
def upload_image():
    if 'image' not in request.files:
        flash('No file part')
        return redirect(url_for('index'))
    
    file = request.files['image']
    if file.filename == '':
        flash('No file selected')
        return redirect(url_for('index'))
    
    if file:
        filename = secure_filename(f"captured_{int(time.time())}.jpg")
        filepath = os.path.join(app.config['UPLOAD_FOLDER'], filename)
        file.save(filepath)
        
        # Convert to JPEG if needed (Instagram requires JPEG)
        img = Image.open(filepath)
        if img.format != 'JPEG':
            img = img.convert('RGB')
            img.save(filepath, 'JPEG')
        
        try:
            # Upload to Instagram
            cl.photo_upload(
                path=filepath,
                caption="Uploaded from Flask Camera App"
            )
            flash('Image uploaded to Instagram successfully!')
        except Exception as e:
            flash(f'Error uploading to Instagram: {str(e)}')
        
        # Clean up
        os.remove(filepath)
        return redirect(url_for('index'))

@app.route('/uploads/<filename>')
def uploaded_file(filename):
    return send_from_directory(app.config['UPLOAD_FOLDER'], filename)

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