forked from AP200408/Courserizer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
96 lines (76 loc) · 3.02 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
import os
from dotenv import load_dotenv
from flask import Flask, render_template, request, redirect, url_for, flash, send_file
from main import CourseScraper
from flask_mail import Mail, Message
load_dotenv()
app = Flask(__name__, static_url_path='/static')
app.secret_key = os.getenv("SECRET_KEY")
app.config['UPLOAD_FOLDER'] = 'uploads'
app.config['MAIL_SERVER'] = 'smtp.gmail.com'
app.config['MAIL_PORT'] = 587
app.config['MAIL_USE_TLS'] = True
app.config['MAIL_USERNAME'] = os.getenv("EMAIL_USERNAME")
app.config['MAIL_PASSWORD'] = os.getenv("EMAIL_PASSWORD")
app.config['MAIL_DEFAULT_SENDER'] = '[email protected]'
mail = Mail(app)
@app.route('/')
def index():
return render_template('index.html')
@app.route('/about')
def about():
return render_template('aboutus.html')
def send_email(name, email, message):
try:
msg = Message('Contact Form Submission', sender=os.getenv("MAIL_USERNAME"), recipients=[os.getenv("RECIPIENT_EMAIL")])
msg.body = f"Name: {name}\nEmail: {email}\nMessage: {message}"
mail.send(msg)
return True
except Exception as e:
flash(f"An error occurred while sending email: {e}", 'error') # Provide feedback to the user
return False
@app.route('/contact', methods=['GET', 'POST'])
def contact():
if request.method == "POST":
name = request.form.get("name")
email = request.form.get("email")
message = request.form.get("message")
if send_email(name, email, message):
flash("Your message has been sent successfully!", 'success') # Provide feedback to the user
else:
flash("There was an error sending your message. Please try again later.", 'error') # Provide feedback to the user
return redirect(url_for('index'))
return render_template("contactus.html")
@app.route('/service')
def service():
return render_template('service.html')
@app.route('/use')
def how_to_use():
return render_template('howtouse.html')
@app.route('/myapp', methods=['GET', 'POST'])
def myapp():
if request.method == "POST":
url = request.form.get('url')
name = request.form.get('name')
course_scraper = CourseScraper(url, name)
course_scraper.scrape_course_info()
file_path = course_scraper.file_path
new_file_path = os.path.splitext(file_path)[0] + '.txt'
os.rename(file_path, new_file_path)
return render_template('result.html', file_path=new_file_path)
else:
return render_template('app.html')
@app.route('/download', methods=['POST'])
def download():
file_path = request.form.get('file_path')
file_name = request.form.get('file_name')
if file_path:
if file_name:
download_name = f'{file_name}.txt'
else:
download_name = 'downloaded_file.txt'
return send_file(file_path, as_attachment=True, mimetype='text/plain', download_name=f'{download_name}')
else:
return "File path not provided."
if __name__ == "__main__":
app.run(debug=True)