-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
157 lines (119 loc) · 5.03 KB
/
app.py
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
from flask import Flask, render_template, request, redirect, url_for, flash, session, jsonify
import os
import dotenv
from PIL import Image, ImageOps
from instagrapi import Client
import threading
import time
from datetime import datetime, timedelta
app = Flask(__name__)
dotenv.load_dotenv()
app.secret_key = os.getenv('FLASK_SECRET_KEY')
instagram_username = os.getenv('INSTAGRAM_USERNAME')
instagram_password = os.getenv('INSTAGRAM_PASSWORD')
app.config['UPLOAD_FOLDER'] = 'upload/'
app.config['ALLOWED_EXTENSIONS'] = {'png', 'jpg', 'jpeg', 'gif'}
if not os.path.exists(app.config['UPLOAD_FOLDER']):
os.makedirs(app.config['UPLOAD_FOLDER'])
# 전역 변수로 로그인 상태 관리
g_instagram_client = None
g_last_login_time = None
def login_instagram():
global g_instagram_client, g_last_login_time
client = Client()
try:
client.login(instagram_username, instagram_password)
g_instagram_client = client
g_last_login_time = datetime.now()
print("Logged in to Instagram")
except Exception as e:
print(f"Login failed: {e}")
g_instagram_client = None
g_last_login_time = None
def check_login_status():
global g_instagram_client, g_last_login_time
while True:
current_time = datetime.now()
# 24시간 간격으로 로그인 상태 확인
if g_last_login_time is None or (current_time - g_last_login_time) > timedelta(days=1):
print("Checking login status...")
if g_instagram_client is None or not g_instagram_client.account_info():
print("Login expired. Attempting to re-login...")
login_instagram()
else:
g_last_login_time = current_time
print("Login is still valid.")
time.sleep(3600 * 10) # 10시간 간격으로 함수 실행
# 비동기로 로그인 작업 수행 및 주기적 체크 시작
threading.Thread(target=login_instagram, daemon=True).start()
threading.Thread(target=check_login_status, daemon=True).start()
def allowed_file(filename):
return '.' in filename and filename.rsplit('.', 1)[1].lower() in app.config['ALLOWED_EXTENSIONS']
def process_image(image_path):
image = Image.open(image_path)
if image.size[0] != image.size[1]:
image = ImageOps.pad(
image, (max(image.size), max(image.size)), color='black')
return image
def upload_instagram(client, caption, image_path):
client.photo_upload(image_path, caption)
def uploader(client, book_info, content, image_path):
processed_image = process_image(image_path)
# 유저가 책 정보를 입력한 경우
if book_info:
caption = f"{content}\n\n{book_info}"
else:
caption = content
processed_image_path = os.path.join(app.config['UPLOAD_FOLDER'],
f"processed_{os.path.basename(image_path)}")
processed_image.save(processed_image_path)
upload_instagram(client, caption, processed_image_path)
@app.route('/login_status', methods=['GET'])
def login_status():
global g_instagram_client, g_last_login_time
if g_instagram_client and g_last_login_time:
return jsonify({
'status': 'logged_in',
'last_login': g_last_login_time.isoformat()
})
else:
return jsonify({'status': 'logged_out'})
@app.route('/', methods=['GET'])
def index():
# 세션에 저장된 최근 3개의 book_info를 불러옴
recent_book_infos = session.get('recent_book_infos', [])
return render_template('upload.html', recent_book_infos=recent_book_infos)
@app.route('/', methods=['POST'])
def upload():
global g_instagram_client
if not g_instagram_client:
return jsonify({'status': 'error', 'message': "Not logged in to Instagram"}), 403
# flash("Login failed. Try again.", "error")
# return redirect(url_for('index'))
book_info = request.form['book_info']
content = request.form['content']
image = request.files['image']
# 필수 정보를 입력하지 않은 경우
if not content:
flash("Content is required.", "error")
return redirect(url_for('index'))
if not image or not allowed_file(image.filename):
flash("Image is required.", "error")
return redirect(url_for('index'))
# 세션에 book_info를 저장 (최대 3개만 유지)
recent_book_infos = session.get('recent_book_infos', [])
if book_info not in recent_book_infos:
recent_book_infos.insert(0, book_info)
if len(recent_book_infos) > 3:
recent_book_infos.pop()
session['recent_book_infos'] = recent_book_infos
if image and allowed_file(image.filename):
filename = image.filename
image_path = os.path.join(app.config['UPLOAD_FOLDER'], filename)
image.save(image_path)
uploader(g_instagram_client, book_info, content, image_path)
flash("Upload success!", "success")
return redirect(url_for('index'))
return redirect(url_for('index'))
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000, debug=False)