API Code API Code API Code
App.py App.py App.py
from flask import Flask, Response, jsonify, render_template, abort, send_file from flask_limiter import Limiter from flask_limiter.util import get_remote_address from program import send_options, send_program, send_json_options from flask_cors import CORS app = Flask("AboutMeAPI", static_url_path='/static') limiter = Limiter(app) CORS(app) limiter._key_func = get_remote_address @app.route("/", methods=['GET']) def main_page(): return render_template('home.html') @app.route("/<path:page_name>", methods=['GET']) @limiter.limit("10/minute") def get_page(page_name): try: return render_template(f"{page_name}.html") except Exception as e: return abort(404) def send_resume(): filename = "Resume.html" with open(filename, 'r') as file: file_contents = file.read() return file_contents def send_readme(): filename = "ReadMe.txt" with open(filename, 'r') as f: content = f.read() return content @app.route("/api/options", methods=['GET']) @limiter.limit("5/minute") def get_options(): return Response(send_options(), mimetype="text/plain") @app.route("/api/options/<option>", methods=['GET']) @limiter.limit("5/minute") def get_program(option): if option.lower() == "readme.txt": return Response(send_readme(), mimetype="text/plain") elif option.lower() == "resume.html": return Response(send_resume(), mimetype="text/plain") else: content = send_program(option) return Response(content, mimetype="text/plain") @app.route("/api/resume", methods=['GET']) @limiter.limit("5/minute") def get_resume_html(): return send_file("Resume.pdf") @app.route("/api/optionsjson", methods=['GET']) @limiter.limit("5/minute") def get_json_options(): data = send_json_options() return jsonify(data) @app.route("/api/options/page/<option>", methods=['GET']) @limiter.limit("5/minute") def get_code_page(option): return render_template('APICodeTemplate.html', code_text=send_program(option), program_title=option) if __name__ == '__main__': app.run()
Program.py Program.py Program.py
import os from flask import abort def send_json_options(): return get_option_dict() def get_option_dict(): path = "Files/" contents = {} biggerPrograms = {} programsInClass = [] programsInBigProgram = [] for file in os.listdir(path): file_path = os.path.join(path, file) for files in os.listdir(file_path): second_path = os.path.join(file_path, files) if os.path.isfile(second_path): programsInClass.append(files) else: for f in os.listdir(second_path): programsInBigProgram.append(f) biggerPrograms[files] = programsInBigProgram programsInBigProgram = [] if bool(biggerPrograms): contents[file] = [programsInClass, biggerPrograms] else: contents[file] = [programsInClass] biggerPrograms = {} programsInClass = [] return contents def send_options(): contents = get_option_dict() return format_into_string(contents) def format_into_string(my_dict): menu = "My Programs and Resume:\n\nResume.html\n\nReadMe.txt\n" for class_name in my_dict.keys(): menu += "\n" + class_name + ":\n" for files in my_dict[class_name]: if isinstance(files, dict): for program_name in files.keys(): menu += "\n\t" + program_name + ":\n" for program in files[program_name]: menu += "\t\t" + program + "\n" else: for program in files: menu += "\t" + program + "\n" menu += "\nType the full file name (.txt files will provide more information about the programs): " return menu def send_program(option): path = "Files/" content = "" for root, dirname, files in os.walk(path): for file in files: if(file.lower() == option.lower()): file_path = os.path.join(root, file) with open(file_path, 'r') as f: content = f.read() return content return abort(404)