nagasurendra commited on
Commit
ee51295
·
verified ·
1 Parent(s): 2014ba7

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +69 -0
app.py ADDED
@@ -0,0 +1,69 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from flask import Flask, render_template, request, redirect, url_for, flash, send_from_directory
2
+ import os
3
+ from werkzeug.utils import secure_filename
4
+ from instagrapi import Client
5
+ from PIL import Image
6
+ import time
7
+
8
+ app = Flask(__name__)
9
+ app.config['SECRET_KEY'] = 'your-secret-key'
10
+ app.config['UPLOAD_FOLDER'] = 'uploads'
11
+ app.config['MAX_CONTENT_LENGTH'] = 16 * 1024 * 1024 # 16MB max file size
12
+
13
+ # Ensure upload folder exists
14
+ os.makedirs(app.config['UPLOAD_FOLDER'], exist_ok=True)
15
+
16
+ # Instagram credentials (replace with your own)
17
+ INSTAGRAM_USERNAME = "your_instagram_username"
18
+ INSTAGRAM_PASSWORD = "your_instagram_password"
19
+
20
+ # Initialize instagrapi client
21
+ cl = Client()
22
+ cl.login(INSTAGRAM_USERNAME, INSTAGRAM_PASSWORD)
23
+
24
+ @app.route('/')
25
+ def index():
26
+ return render_template('index.html')
27
+
28
+ @app.route('/upload', methods=['POST'])
29
+ def upload_image():
30
+ if 'image' not in request.files:
31
+ flash('No file part')
32
+ return redirect(url_for('index'))
33
+
34
+ file = request.files['image']
35
+ if file.filename == '':
36
+ flash('No file selected')
37
+ return redirect(url_for('index'))
38
+
39
+ if file:
40
+ filename = secure_filename(f"captured_{int(time.time())}.jpg")
41
+ filepath = os.path.join(app.config['UPLOAD_FOLDER'], filename)
42
+ file.save(filepath)
43
+
44
+ # Convert to JPEG if needed (Instagram requires JPEG)
45
+ img = Image.open(filepath)
46
+ if img.format != 'JPEG':
47
+ img = img.convert('RGB')
48
+ img.save(filepath, 'JPEG')
49
+
50
+ try:
51
+ # Upload to Instagram
52
+ cl.photo_upload(
53
+ path=filepath,
54
+ caption="Uploaded from Flask Camera App"
55
+ )
56
+ flash('Image uploaded to Instagram successfully!')
57
+ except Exception as e:
58
+ flash(f'Error uploading to Instagram: {str(e)}')
59
+
60
+ # Clean up
61
+ os.remove(filepath)
62
+ return redirect(url_for('index'))
63
+
64
+ @app.route('/uploads/<filename>')
65
+ def uploaded_file(filename):
66
+ return send_from_directory(app.config['UPLOAD_FOLDER'], filename)
67
+
68
+ if __name__ == '__main__':
69
+ app.run(debug=True, host='0.0.0.0')