-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
135 lines (121 loc) · 4.65 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
from flask import Flask, render_template, request, redirect, url_for, jsonify
import sqlite3
from datetime import datetime
import pytz
from collections import Counter
import sys
import io
# LED 관련 임포트를 주석 처리
# import board
# import neopixel
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')
app = Flask(__name__)
# LED 초기화 코드를 주석 처리
# pixels = neopixel.NeoPixel(board.D18, 30, brightness=0.2, auto_write=False)
def init_db():
conn = sqlite3.connect('ideas.db', detect_types=sqlite3.PARSE_DECLTYPES)
conn.text_factory = str
c = conn.cursor()
c.execute('''CREATE TABLE IF NOT EXISTS ideas
(id INTEGER PRIMARY KEY AUTOINCREMENT,
idea TEXT NOT NULL,
category TEXT NOT NULL,
timestamp DATETIME DEFAULT CURRENT_TIMESTAMP)''')
conn.commit()
conn.close()
def get_all_ideas():
conn = sqlite3.connect('ideas.db', detect_types=sqlite3.PARSE_DECLTYPES)
conn.text_factory = str
c = conn.cursor()
c.execute("SELECT * FROM ideas ORDER BY timestamp DESC")
ideas = c.fetchall()
conn.close()
return ideas
def analyze_ideas(ideas):
all_text = ' '.join([idea[1] for idea in ideas])
words = all_text.lower().split()
common_words = Counter(words).most_common(10)
category_counts = Counter([idea[2] for idea in ideas])
time_distribution = Counter([idea[3].split()[1].split(':')[0] for idea in ideas])
return {
'common_words': common_words,
'category_counts': dict(category_counts),
'time_distribution': dict(time_distribution)
}
@app.route('/', methods=['GET', 'POST'])
def index():
if request.method == 'POST':
idea = request.form['idea']
category = request.form['category']
local_time = datetime.now(pytz.timezone('Asia/Seoul'))
conn = sqlite3.connect('ideas.db', detect_types=sqlite3.PARSE_DECLTYPES)
conn.text_factory = str
c = conn.cursor()
c.execute("INSERT INTO ideas (idea, category, timestamp) VALUES (?, ?, ?)",
(idea, category, local_time.strftime('%Y-%m-%d %H:%M:%S')))
conn.commit()
conn.close()
ideas = get_all_ideas()
local_tz = pytz.timezone('Asia/Seoul')
ideas = [(idea[0], idea[1], idea[2],
datetime.strptime(idea[3], '%Y-%m-%d %H:%M:%S').replace(tzinfo=pytz.UTC).astimezone(local_tz).strftime('%Y-%m-%d %H:%M:%S'))
for idea in ideas]
analysis_results = analyze_ideas(ideas)
# LED 패턴 생성 및 적용 코드를 주석 처리
# category_counts = analysis_results['category_counts']
# total_ideas = sum(category_counts.values())
# led_pattern = []
# for category, count in category_counts.items():
# ratio = count / total_ideas if total_ideas > 0 else 0
# if category == 'Environment':
# color = (0, 255, 0) # Green
# elif category == 'Technology':
# color = (0, 0, 255) # Blue
# elif category == 'Society':
# color = (255, 255, 0) # Yellow
# elif category == 'Culture':
# color = (255, 0, 255) # Purple
# else:
# color = (255, 255, 255) # White
# led_pattern.extend([color] * int(30 * ratio))
#
# led_pattern = led_pattern[:30]
# if len(led_pattern) < 30:
# led_pattern.extend([(0, 0, 0)] * (30 - len(led_pattern)))
#
# pixels.fill((0, 0, 0))
# for i, color in enumerate(led_pattern):
# pixels[i] = color
# pixels.show()
return render_template('index.html', ideas=ideas, analysis_results=analysis_results)
@app.route('/delete/<int:id>', methods=['POST'])
def delete_idea(id):
conn = sqlite3.connect('ideas.db', detect_types=sqlite3.PARSE_DECLTYPES)
conn.text_factory = str
c = conn.cursor()
c.execute("DELETE FROM ideas WHERE id=?", (id,))
conn.commit()
conn.close()
return redirect(url_for('index'))
@app.route('/change_led', methods=['POST'])
def change_led():
category = request.json['category']
# LED 변경 코드를 주석 처리
# if category == 'Environment':
# pattern = [(0, 255, 0)] * 30 # Green
# elif category == 'Technology':
# pattern = [(0, 0, 255)] * 30 # Blue
# elif category == 'Society':
# pattern = [(255, 255, 0)] * 30 # Yellow
# elif category == 'Culture':
# pattern = [(255, 0, 255)] * 30 # Purple
# else:
# pattern = [(0, 0, 0)] * 30 # Off
#
# for i, color in enumerate(pattern):
# pixels[i] = color
# pixels.show()
return jsonify({'success': True})
if __name__ == '__main__':
init_db()
app.run(debug=True, host='0.0.0.0', port=5000)