Skip to content

Commit

Permalink
implement caching
Browse files Browse the repository at this point in the history
  • Loading branch information
sharkey300 committed Sep 27, 2024
1 parent d995242 commit 63127c1
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 28 deletions.
38 changes: 11 additions & 27 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import os
import webbrowser
from flask import Flask, render_template, request, jsonify
from flask_caching import Cache

from util.constants import PARENT_DIR
from add_show import add_show
Expand All @@ -15,6 +16,11 @@
else:
app.config['JSON_AS_ASCII'] = False

cache = Cache(app, config={
'CACHE_TYPE': 'simple',
'CACHE_THRESHOLD': 50
})

if not os.path.isdir(PARENT_DIR):
os.mkdir(PARENT_DIR)

Expand All @@ -25,6 +31,7 @@ def index():
return render_template('index.html')

@app.route('/api/read')
@cache.cached(timeout=300, query_string=True)
def read_file():
path = request.args.get('path')
path = path.replace('..', '').replace('//', '/').replace('\\', '/').lstrip('/')
Expand All @@ -36,12 +43,14 @@ def read_file():
return jsonify({'error': str(e)}), 400

@app.route('/api/forums')
@cache.cached(timeout=86400)
def list_forums():
with open('util/forums.json', encoding='utf-8') as f:
forums = json.load(f)
return jsonify(forums)

@app.route('/api/showinfo')
@cache.cached(timeout=86400)
def show_info():
shows = os.listdir(PARENT_DIR)
response = {
Expand All @@ -58,34 +67,8 @@ def show_info():
response.get('ids')[show] = json.load(f)
return jsonify(response)

@app.route('/api/showmap')
def map_files():
shows = os.listdir(PARENT_DIR)
show_map = {}
for show in shows:
with open(f'{PARENT_DIR}/{show}/meta/map.json', encoding='utf-8') as f:
show_map[show] = json.load(f)
return jsonify(show_map)

@app.route('/api/showtitles')
def show_titles():
shows = os.listdir(PARENT_DIR)
titles = {}
for show in shows:
with open(f'{PARENT_DIR}/{show}/meta/title.txt', encoding='utf-8') as f:
titles[show] = f.read()
return jsonify(titles)

@app.route('/api/episode_ids')
def episode_ids():
shows = os.listdir(PARENT_DIR)
ids = {}
for show in shows:
with open(f'{PARENT_DIR}/{show}/meta/ids.json', encoding='utf-8') as f:
ids[show] = json.load(f)
return jsonify(ids)

@app.route('/api/heatmap')
@cache.cached(timeout=300, query_string=True)
def heatmap():
words = request.args.get('words').split(',')
show = request.args.get('show')
Expand All @@ -99,6 +82,7 @@ def heatmap():
return jsonify({'error': str(e)}), 400

@app.route('/api/wordcloud')
@cache.cached(timeout=300, query_string=True)
def wordcloud():
width = request.args.get('width')
height = request.args.get('height')
Expand Down
2 changes: 1 addition & 1 deletion setup.bat
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
echo 'Installing required packages...'
pip install beautifulsoup4 matplotlib numpy pandas requests tqdm spacy spacytextblob wordcloud
pip install beautifulsoup4 matplotlib numpy pandas requests tqdm spacy spacytextblob wordcloud flask flask-config
python -m spacy download en_core_web_sm
echo 'Done installing required packages.'

0 comments on commit 63127c1

Please sign in to comment.