Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed support for filename matching #20

Merged
merged 1 commit into from
Aug 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion data_extractor/pyprocess.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ function process() {
resultElement().style.display = "none";

const file = fileInput().files[0];
const filename = file.name
const reader = file.stream().getReader();
const sendToWorker = ({ done, value }) => {
if (done) {
Expand All @@ -60,7 +61,7 @@ function process() {
reader.read().then(sendToWorker);
};
console.log("process: send event: initData");
pyWorker.postMessage({ eventType: "initData", size: file.size });
pyWorker.postMessage({ eventType: "initData", filename: filename, size: file.size });
reader.read().then(sendToWorker);
}

15 changes: 13 additions & 2 deletions data_extractor/pyworker.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,39 @@ loadPyodide({ indexURL: "https://cdn.jsdelivr.net/pyodide/v0.19.0/full/" }).then
});

let file = undefined
var filename = undefined

onmessage = (event) => {
const { eventType } = event.data;
if (eventType === "loadScript") {
self.pyodide.runPython(event.data.script)
} else if (eventType === "initData") {
file = self.pyodide.FS.open("user-data", "w")
filename = event.data.filename
file = self.pyodide.FS.open(filename, "w")
} else if (eventType === "data") {
self.pyodide.FS.write(file, event.data.chunk, 0, event.data.chunk.length)
} else if (eventType === "processData") {
const result = self.pyodide.runPython(`
def _process_data():
import json
import html
result = process(open("user-data", "rb"))
import pandas as pd

result = process("${filename}")

if not result:
data_frame = pd.DataFrame()
data_frame["Messages"] = pd.Series(["Unfortunately, no data could be extracted from the selected file."], name="Messages")
result = [{"id": "important_feedback", "title": "Important feedback", "data_frame": data_frame}]

data_output = []
html_output = []
for data in result:
html_output.append(f"""<h1 class="text-title4 font-title4 sm:text-title3 sm:font-title3 mt-12 mb-6 text-grey1">{html.escape(data['title'])}</h1>""")
df = data['data_frame']
html_output.append(df.to_html(classes=["data-donation-extraction-results"], justify="left"))
data_output.append({"id": data["id"], "data_frame": df.to_json()})

return {
"html": "\\n".join(html_output),
"data": json.dumps(data_output),
Expand Down
2 changes: 1 addition & 1 deletion data_extractor/whatsapp_chat/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -693,7 +693,7 @@ def process(file_data):
try:
zfile = zipfile.ZipFile(file_data)
except:
if FILE_RE.match(file_data.name):
if FILE_RE.match(file_data):
tfile = open(file_data, encoding="utf8")
chat = parse_chat(log_error, tfile.read())

Expand Down