Skip to content

Commit e637089

Browse files
Working server and client
0 parents  commit e637089

File tree

4 files changed

+232
-0
lines changed

4 files changed

+232
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.env
2+
venv/
3+
.idea/

client.py

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
import sys
2+
3+
import requests
4+
from PyQt5.QtWidgets import QApplication, QMainWindow, QWidget, QVBoxLayout, QHBoxLayout, QLabel, QLineEdit, \
5+
QPushButton, QFileDialog, QMessageBox
6+
7+
8+
class FileEntryWidget(QWidget):
9+
def __init__(self, file_path, main_window):
10+
super().__init__()
11+
12+
self.file_path = file_path
13+
self.main_window = main_window
14+
15+
self.file_label = QLabel()
16+
self.file_label.setText(file_path.split("/")[-1])
17+
18+
self.title_entry = QLineEdit()
19+
20+
self.remove_button = QPushButton("Remove")
21+
self.remove_button.clicked.connect(self.remove_self)
22+
23+
layout = QHBoxLayout()
24+
layout.addWidget(self.file_label)
25+
layout.addWidget(self.title_entry)
26+
layout.addWidget(self.remove_button)
27+
28+
self.setLayout(layout)
29+
30+
def remove_self(self):
31+
self.main_window.remove_file_entry(self) # Notify the main window to remove this entry
32+
self.setParent(None)
33+
self.deleteLater()
34+
35+
36+
class MainWindow(QMainWindow):
37+
def __init__(self):
38+
super().__init__()
39+
40+
self.file_entries = []
41+
42+
self.setWindowTitle("File Upload")
43+
self.resize(400, 300)
44+
45+
central_widget = QWidget(self)
46+
self.setCentralWidget(central_widget)
47+
48+
main_layout = QVBoxLayout(central_widget)
49+
50+
self.select_button = QPushButton("Select Files")
51+
self.select_button.clicked.connect(self.select_files)
52+
53+
self.files_layout = QVBoxLayout()
54+
55+
self.submit_button = QPushButton("Submit")
56+
self.submit_button.clicked.connect(self.upload_files)
57+
58+
main_layout.addWidget(self.select_button)
59+
main_layout.addLayout(self.files_layout)
60+
main_layout.addWidget(self.submit_button)
61+
62+
def select_files(self):
63+
file_dialog = QFileDialog()
64+
file_dialog.setFileMode(QFileDialog.ExistingFiles)
65+
file_dialog.setNameFilter("PDF Files (*.pdf)")
66+
if file_dialog.exec_():
67+
file_paths = file_dialog.selectedFiles()
68+
for file_path in file_paths:
69+
self.add_file_entry(file_path)
70+
71+
def add_file_entry(self, file_path):
72+
file_entry_widget = FileEntryWidget(file_path, self)
73+
self.files_layout.addWidget(file_entry_widget)
74+
self.file_entries.append(file_entry_widget)
75+
76+
def remove_file_entry(self, file_entry):
77+
self.file_entries.remove(file_entry)
78+
79+
def upload_files(self):
80+
file_entries_data = []
81+
files = []
82+
for file_entry in self.file_entries:
83+
file_path = file_entry.file_path
84+
file_name = file_path.split("/")[-1]
85+
title = file_entry.title_entry.text()
86+
file_entries_data.append((file_path, file_name, title))
87+
with open(file_path, 'rb') as file:
88+
file_content = file.read()
89+
files.append(('file', (file_name, file_content, 'application/pdf')))
90+
91+
data = {f'title_{i + 1}': title for i, (_, _, title) in enumerate(file_entries_data)}
92+
93+
response = requests.post('http://localhost:8080/upload', files=files, data=data)
94+
95+
if response.status_code == 200:
96+
QMessageBox.information(self, "Success", "Files uploaded successfully")
97+
else:
98+
QMessageBox.critical(self, "Error", f"Error uploading files: {response.text}")
99+
100+
101+
if __name__ == "__main__":
102+
app = QApplication(sys.argv)
103+
main_window = MainWindow()
104+
main_window.show()
105+
sys.exit(app.exec_())

requirements.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Flask
2+
beautifulsoup4
3+
requests
4+
Werkzeug
5+
PyQt5
6+
GitPython
7+
python-dotenv

server.py

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
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

Comments
 (0)