-
Notifications
You must be signed in to change notification settings - Fork 0
/
app-old.py
295 lines (251 loc) · 10.9 KB
/
app-old.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
import io
import os
import base64
from flask import Flask, render_template, request, redirect, url_for, flash, send_file
import sqlite3
import datetime
from werkzeug.utils import secure_filename
app = Flask(__name__)
app.static_folder = 'static' # Set the static folder to 'static'
app.config['SECRET_KEY'] = 'your_secret_key'
app.config['UPLOAD_FOLDER'] = 'static/videos'
app.config['ALLOWED_EXTENSIONS'] = {'mp4'}
app.config['DATABASE'] = 'instance/blog2.db'
DATABASE = app.config['DATABASE']
def logit(argvs):
argv = argvs
log_file = "app_log.txt" # Path to your log file
timestamp = datetime.datetime.now().strftime("%A_%b-%d-%Y_%H-%M-%S")
with open(log_file, "a") as log:
log.write(f"{timestamp}: {argv}\n")
def allowed_file(filename):
return '.' in filename and filename.rsplit('.', 1)[1].lower() in app.config['ALLOWED_EXTENSIONS']
@app.route('/upload_video/<int:post_id>', methods=['POST'])
def upload_video(post_id):
if 'videoFile' not in request.files:
flash('No file part')
return redirect(request.url)
file = request.files['videoFile']
if file.filename == '':
flash('No selected file')
return redirect(request.url)
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
# Update the database with the filename
update_video_filename(post_id, filename)
flash('Video uploaded successfully')
return redirect(url_for('post', post_id=post_id))
else:
flash('Allowed file types are .mp4')
return redirect(request.url)
def update_video_filename(post_id, filename):
with sqlite3.connect(DATABASE) as conn:
cursor = conn.cursor()
cursor.execute('UPDATE post SET video_filename = ? WHERE id = ?', (filename, post_id))
conn.commit()
# Initialize SQLite database if not exists
def init_db():
with sqlite3.connect(DATABASE) as conn:
cursor = conn.cursor()
cursor.execute('''
CREATE TABLE IF NOT EXISTS post (
id INTEGER PRIMARY KEY AUTOINCREMENT,
title TEXT NOT NULL,
content TEXT NOT NULL,
video_filename TEXT NULL,
image BLOB
)
''')
conn.commit()
# Function to fetch a single post by ID
@app.route('/post/<int:post_id>', methods=['GET', 'POST'])
def post(post_id):
if request.method == 'POST':
return upload_video(post_id)
post = get_post(post_id)
if not post:
flash('Post not found')
return redirect(url_for('home'))
image_data = get_image_data(post_id)
video_filename = post[4] if post[4] else None # Adjust index based on your database schema
return render_template('post.html', post=post, image_data=image_data, video_filename=video_filename)
def get_post(post_id):
with sqlite3.connect(DATABASE) as conn:
cursor = conn.cursor()
cursor.execute('SELECT id, title, content, image, video_filename FROM post WHERE id = ?', (post_id,))
post = cursor.fetchone()
return post
# Function to fetch all posts
def get_posts(limit=None):
with sqlite3.connect(DATABASE) as conn:
cursor = conn.cursor()
if limit:
cursor.execute('SELECT id, title, content, image, video_filename FROM post ORDER BY id DESC LIMIT ?', (limit,))
else:
cursor.execute('SELECT id, title, content, image, video_filename FROM post ORDER BY id DESC')
posts = cursor.fetchall()
return posts
# Function to fetch image data
def get_image(post_id):
with sqlite3.connect(DATABASE) as conn:
cursor = conn.cursor()
cursor.execute('SELECT image FROM post WHERE id = ?', (post_id,))
post = cursor.fetchone()
if post and post[0]:
return post[0] # Return base64 encoded image data
return None
@app.route('/')
def home():
posts = get_posts(limit=6)
for post in posts:
logit(post[3])# Limit to last 4 posts
return render_template('home.html', posts=posts)
@app.route('/new', methods=['GET', 'POST'])
def new_post():
if request.method == 'POST':
title = request.form['title']
content = request.form['content']
image = request.files['image'].read() if 'image' in request.files and request.files['image'].filename != '' else None
if image:
image = base64.b64encode(image).decode('utf-8')
with sqlite3.connect(DATABASE) as conn:
cursor = conn.cursor()
cursor.execute('INSERT INTO post (title, content, image) VALUES (?, ?, ?)', (title, content, image))
conn.commit()
flash('Post created successfully!', 'success')
return redirect(url_for('home'))
return render_template('new_post.html')
@app.route('/edit/<int:post_id>', methods=['GET', 'POST'])
def edit_post(post_id):
post = get_post(post_id)
if request.method == 'POST':
title = request.form['title']
content = request.form['content']
image_data = get_image(post_id) # Get the current image data
if 'image' in request.files and request.files['image'].filename != '':
image = request.files['image'].read()
image_data = base64.b64encode(image).decode('utf-8') # Update with new image data
with sqlite3.connect(DATABASE) as conn:
cursor = conn.cursor()
cursor.execute('UPDATE post SET title = ?, content = ?, image = ? WHERE id = ?', (title, content, image_data, post_id))
conn.commit()
flash('Post updated successfully!', 'success')
return redirect(url_for('post', post_id=post_id))
return render_template('edit_post.html', post=post)
@app.route('/contents')
def contents():
posts = get_posts()
contents_data = []
for post in posts:
excerpt = post[2][:300] + '...' if len(post[2]) > 300 else post[2] # Assuming content is in the third column (index 2)
contents_data.append({
'id': post[0],
'title': post[1],
'excerpt': excerpt
})
return render_template('contents.html', contents_data=contents_data)
@app.route('/delete/<int:post_id>', methods=['POST'])
def delete_post(post_id):
with sqlite3.connect(DATABASE) as conn:
cursor = conn.cursor()
cursor.execute('DELETE FROM post WHERE id = ?', (post_id,))
conn.commit()
flash('Post deleted successfully!', 'success')
return redirect(url_for('home'))
def load_txt_files(directory):
init_db() # Initialize the SQLite database if not already created
conn = sqlite3.connect(DATABASE)
cursor = conn.cursor()
try:
for filename in os.listdir(directory):
if filename.endswith('.txt'):
filepath = os.path.join(directory, filename)
with open(filepath, 'r', encoding='utf-8') as file:
title = os.path.splitext(filename)[0]
content = file.read()
cursor.execute('SELECT id FROM post WHERE title = ?', (title,))
existing_post = cursor.fetchone()
if not existing_post:
cursor.execute('INSERT INTO post (title, content) VALUES (?, ?)', (title, content))
conn.commit()
print(f'Added post: {title}')
else:
print(f'Skipped existing post: {title}')
except sqlite3.Error as e:
print(f'SQLite error: {e}')
finally:
conn.close()
@app.route('/search', methods=['GET', 'POST'])
def search():
if request.method == 'POST':
search_terms = request.form['search_terms']
conn = sqlite3.connect(DATABASE)
cursor = conn.cursor()
# Define the search terms
search_terms = search_terms.split(",") # Split by comma to get individual search terms
results = []
# Construct the WHERE clause for the SQL query to filter rows based on all search terms
where_conditions = []
for term in search_terms:
where_conditions.append(f"content LIKE ?")
where_clause = " AND ".join(where_conditions)
# Create a tuple of search terms with wildcard characters for the SQL query
search_terms_tuple = tuple(f"%{term.strip()}%" for term in search_terms)
# Execute the SELECT query with the constructed WHERE clause
query = f"SELECT ROWID, title, content, image, video_filename FROM post WHERE {where_clause}"
rows = cursor.execute(query, search_terms_tuple)
for row in rows:
results.append((row[0], row[1], row[2], row[3], row[4]))
conn.close()
return render_template('search.html', results=results)
return render_template('search.html', results=[])
def get_image_data(post_id):
with sqlite3.connect(DATABASE) as conn:
cursor = conn.cursor()
cursor.execute('SELECT image FROM post WHERE id = ?', (post_id,))
post = cursor.fetchone()
if post and post[0]:
return base64.b64decode(post[0]) # Decode base64 to binary
else:
return None
@app.route('/post/<int:post_id>', methods=['GET', 'POST'])
def show_post(post_id):
if request.method == 'POST':
if 'videoFile' not in request.files:
flash('No file part')
return redirect(request.url)
file = request.files['videoFile']
if file.filename == '':
flash('No selected file')
return redirect(request.url)
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
with sqlite3.connect(DATABASE) as conn:
cursor = conn.cursor()
cursor.execute('UPDATE post SET video_filename = ? WHERE id = ?', (filename, post_id))
conn.commit()
flash('Video uploaded successfully')
return redirect(url_for('show_post', post_id=post_id))
with sqlite3.connect(DATABASE) as conn:
cursor = conn.cursor()
cursor.execute('SELECT id, title, content, image, video_filename FROM post WHERE id = ?', (post_id,))
post = cursor.fetchone()
if not post:
flash('Post not found')
return redirect(url_for('home'))
image_data = base64.b64decode(post[3]) if post[3] else None
video_filename = post[4] if post[4] else None
return render_template('post.html', post=post, image_data=image_data, video_filename=video_filename)
@app.route('/image/<int:post_id>')
def view_image(post_id):
image_data = get_image_data(post_id)
if image_data:
return send_file(io.BytesIO(image_data), mimetype='image/jpeg')
else:
return "No image found", 404
if __name__ == '__main__':
directory = 'static/TEXT'
load_txt_files(directory)
app.run(debug=True)