|
| 1 | +import os |
| 2 | +import shutil |
| 3 | + |
| 4 | +from bs4 import BeautifulSoup |
| 5 | +from dotenv import load_dotenv |
| 6 | +from flask import Flask, request |
| 7 | +from git import Repo, rmtree |
| 8 | +from werkzeug.utils import secure_filename |
| 9 | + |
| 10 | +load_dotenv() |
| 11 | + |
| 12 | +app = Flask(__name__) |
| 13 | +app.config['KAARAH_FOLDER'] = 'repo/divrei_torah/kaarah' |
| 14 | +app.config['MAAMAREI_MORDECHAI_FOLDER'] = 'repo/divrei_torah/maamarei_mordechai' |
| 15 | +app.config['ALLOWED_EXTENSIONS'] = {'pdf'} |
| 16 | + |
| 17 | +username = os.getenv('GITHUB_USERNAME') |
| 18 | +password = os.getenv('GITHUB_TOKEN') |
| 19 | +repository = os.getenv('GITHUB_REPO') |
| 20 | +remote = f"https://{username}:{password}@github.com/{username}/{repository}.git" |
| 21 | + |
| 22 | + |
| 23 | +def allowed_file(filename): |
| 24 | + return '.' in filename and \ |
| 25 | + filename.rsplit('.', 1)[1].lower() in app.config['ALLOWED_EXTENSIONS'] |
| 26 | + |
| 27 | + |
| 28 | +# def update_html_file(uploaded_files, titles): |
| 29 | +# for file, title in zip(uploaded_files, titles): |
| 30 | +# if title.startswith('Kaarah'): |
| 31 | +# filename = 'kaarah.html' |
| 32 | +# else: |
| 33 | +# filename = "maamarei_mordechai.html" |
| 34 | +# |
| 35 | +# with open(filename, 'r+', encoding="utf-8") as f: |
| 36 | +# soup = BeautifulSoup(f, 'html.parser') |
| 37 | +# target_ul = soup.find('ul', {'id': 'file-list'}) |
| 38 | +# li_tag = soup.new_tag('li') |
| 39 | +# fname = "/%s" % file.replace('\\', '/') |
| 40 | +# a_tag = soup.new_tag('a', href=fname, target='blank') |
| 41 | +# a_tag.string = title |
| 42 | +# li_tag.append(a_tag) |
| 43 | +# target_ul.append(li_tag) |
| 44 | +# |
| 45 | +# f.seek(0) |
| 46 | +# f.write(str(soup.prettify())) |
| 47 | +# f.truncate() |
| 48 | + |
| 49 | + |
| 50 | +def update_html_file(uploaded_files, titles): |
| 51 | + with Repo.clone_from(remote, "repo") as repo: |
| 52 | + changed_files = [] |
| 53 | + |
| 54 | + for file, title in zip(uploaded_files, titles): |
| 55 | + if title.startswith('Kaarah'): |
| 56 | + fname = 'kaarah.html' |
| 57 | + directory = app.config['KAARAH_FOLDER'] |
| 58 | + else: |
| 59 | + fname = 'maamarei_mordechai.html' |
| 60 | + directory = app.config['MAAMAREI_MORDECHAI_FOLDER'] |
| 61 | + |
| 62 | + if fname not in changed_files: |
| 63 | + changed_files.append(fname) |
| 64 | + filename = "repo/" + fname |
| 65 | + shutil.move(file, os.path.join(directory, os.path.basename(file))) |
| 66 | + |
| 67 | + with open(filename, 'r+', encoding='utf-8') as f: |
| 68 | + soup = BeautifulSoup(f, 'html.parser') |
| 69 | + target_ul = soup.find('ul', {'id': 'file-list'}) |
| 70 | + li_tag = soup.new_tag('li') |
| 71 | + fname = "%s/%s" % (directory.removeprefix('repo'), file.replace('\\', '/')) |
| 72 | + a_tag = soup.new_tag('a', href=fname, target='blank') |
| 73 | + a_tag.string = title |
| 74 | + li_tag.append(a_tag) |
| 75 | + target_ul.append(li_tag) |
| 76 | + |
| 77 | + f.seek(0) |
| 78 | + f.write(str(soup.prettify())) |
| 79 | + f.truncate() |
| 80 | + |
| 81 | + changed_files.append(fname.removeprefix('/')) |
| 82 | + |
| 83 | + kaarah_titles = [title for title in titles if title.startswith('Kaarah')] |
| 84 | + maamarei_titles = [title for title in titles if not title.startswith('Kaarah')] |
| 85 | + |
| 86 | + repo.git.add(changed_files) |
| 87 | + repo.index.commit("%s and %s" % (", ".join(maamarei_titles), ", ".join(kaarah_titles))) |
| 88 | + origin = repo.remote(name="origin") |
| 89 | + origin.push() |
| 90 | + |
| 91 | + rmtree("repo") |
| 92 | + |
| 93 | + |
| 94 | +@app.route('/upload', methods=['POST']) |
| 95 | +def upload_files(): |
| 96 | + uploaded_files = [] |
| 97 | + titles = [] |
| 98 | + file_entries = request.files.getlist('file') |
| 99 | + |
| 100 | + for idx, file_entry in enumerate(file_entries): |
| 101 | + if file_entry and allowed_file(file_entry.filename): |
| 102 | + title_key = f'title_{idx + 1}' |
| 103 | + title = request.form.get(title_key) |
| 104 | + titles.append(title) |
| 105 | + |
| 106 | + filename = secure_filename(file_entry.filename) |
| 107 | + |
| 108 | + file_entry.save(filename) |
| 109 | + uploaded_files.append(filename) |
| 110 | + |
| 111 | + update_html_file(uploaded_files, titles) |
| 112 | + |
| 113 | + return f"Files uploaded: {uploaded_files}\nTitles: {titles}\n\nSuccess" |
| 114 | + |
| 115 | + |
| 116 | +if __name__ == '__main__': |
| 117 | + app.run(host='localhost', port=8080) |
0 commit comments