improved folder structure and much faster code
This commit is contained in:
parent
5f98aa0ed8
commit
77ef8df43f
103
scripts/main.go
103
scripts/main.go
|
@ -1,103 +0,0 @@
|
||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"bufio"
|
|
||||||
"bytes"
|
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
|
||||||
"io"
|
|
||||||
"log"
|
|
||||||
"net/http"
|
|
||||||
"os"
|
|
||||||
"os/exec"
|
|
||||||
)
|
|
||||||
|
|
||||||
type Message struct {
|
|
||||||
Message string `json:"message"`
|
|
||||||
From string `json:"from"`
|
|
||||||
To string `json:"to"`
|
|
||||||
}
|
|
||||||
|
|
||||||
func getAllPackages() {
|
|
||||||
TurnOn := exec.Command("python", "scripts/python/package_mangement/note_all_packages.py")
|
|
||||||
// Create a buffer to capture the standard output.
|
|
||||||
var out bytes.Buffer
|
|
||||||
TurnOn.Stdout = &out
|
|
||||||
|
|
||||||
// Execute the command.
|
|
||||||
err := TurnOn.Run()
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Retrieve and print the standard output from the command.
|
|
||||||
fmt.Println("Output:", out.String())
|
|
||||||
}
|
|
||||||
func handleRequest(w http.ResponseWriter, r *http.Request) {
|
|
||||||
// Read the request body
|
|
||||||
body, err := io.ReadAll(r.Body)
|
|
||||||
if err != nil {
|
|
||||||
http.Error(w, "Error reading request body: "+err.Error(), http.StatusInternalServerError)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
defer r.Body.Close()
|
|
||||||
|
|
||||||
// Unmarshal the JSON data
|
|
||||||
var msg Message
|
|
||||||
err = json.Unmarshal(body, &msg)
|
|
||||||
if err != nil {
|
|
||||||
http.Error(w, "Error unmarshalling JSON: "+err.Error(), http.StatusBadRequest)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check if From and To fields are not longer than 2 letters
|
|
||||||
if len(msg.From) > 2 || len(msg.To) > 2 {
|
|
||||||
http.Error(w, "From and To fields should not be longer than 2 letters.", http.StatusBadRequest)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// Start the Python command in the background
|
|
||||||
cmd := exec.Command("python", "scripts/python/main.py", msg.Message, msg.From, msg.To)
|
|
||||||
output, err := cmd.StdoutPipe()
|
|
||||||
if err != nil {
|
|
||||||
http.Error(w, "Error starting Python command: "+err.Error(), http.StatusInternalServerError)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
err = cmd.Start()
|
|
||||||
if err != nil {
|
|
||||||
http.Error(w, "Error starting Python command: "+err.Error(), http.StatusInternalServerError)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// Read the output of the Python command and send it to the client
|
|
||||||
scanner := bufio.NewScanner(output)
|
|
||||||
for scanner.Scan() {
|
|
||||||
fmt.Fprintln(w, scanner.Text())
|
|
||||||
}
|
|
||||||
|
|
||||||
// Wait for the command to complete
|
|
||||||
err = cmd.Wait()
|
|
||||||
if err != nil {
|
|
||||||
http.Error(w, "Error waiting for Python command: "+err.Error(), http.StatusInternalServerError)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
getAllPackages()
|
|
||||||
// Define the HTTP handler function
|
|
||||||
http.HandleFunc("/api", handleRequest)
|
|
||||||
|
|
||||||
// Get the port number from the environment variable or use a default value
|
|
||||||
port := "11435"
|
|
||||||
if p := os.Getenv("PORT"); p != "" {
|
|
||||||
port = p
|
|
||||||
}
|
|
||||||
|
|
||||||
// Start the HTTP server
|
|
||||||
fmt.Printf("Listening on :%s...\n", port)
|
|
||||||
err := http.ListenAndServe(":"+port, nil)
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,18 +0,0 @@
|
||||||
import json
|
|
||||||
from datetime import datetime, timezone
|
|
||||||
|
|
||||||
def create_respone(translated, from_code, to_code, error):
|
|
||||||
# Creating a dictionary that matches the structure of your JSON
|
|
||||||
data = {
|
|
||||||
"from": from_code,
|
|
||||||
"to": to_code,
|
|
||||||
"created_at": datetime.now(timezone.utc).strftime("%Y-%m-%dT%H:%M:%S.%f%z"),
|
|
||||||
"response": translated,
|
|
||||||
"error": error
|
|
||||||
}
|
|
||||||
json_string = json.dumps(data, indent=4)
|
|
||||||
|
|
||||||
# Printing the JSON string to the console
|
|
||||||
print(json_string)
|
|
||||||
|
|
||||||
|
|
|
@ -1,31 +0,0 @@
|
||||||
import sys
|
|
||||||
from translate import translate
|
|
||||||
from package_mangement.downlaod_languages import install_language
|
|
||||||
from package_mangement.check_packages import check_languages_installed
|
|
||||||
from create_response import create_respone
|
|
||||||
|
|
||||||
def main():
|
|
||||||
message = sys.argv[1]
|
|
||||||
from_lang = sys.argv[2]
|
|
||||||
to_lang = sys.argv[3]
|
|
||||||
|
|
||||||
# Check if languages are installed
|
|
||||||
is_installed = check_languages_installed(from_lang, to_lang)
|
|
||||||
if not is_installed:
|
|
||||||
# Attempt to install the languages
|
|
||||||
installed = install_language(from_lang, to_lang)
|
|
||||||
if installed != 0:
|
|
||||||
# Installation failed
|
|
||||||
error = f"No available package for translating from {from_lang} to {to_lang}"
|
|
||||||
create_respone("None", from_lang, to_lang, error)
|
|
||||||
return # Exit the function early
|
|
||||||
|
|
||||||
# Languages are installed or installation succeeded
|
|
||||||
translated = translate(message, from_lang, to_lang)
|
|
||||||
if translated:
|
|
||||||
create_respone(translated, from_lang, to_lang, error = "None")
|
|
||||||
else:
|
|
||||||
create_respone("None", from_lang, to_lang, error = "Could not translate it.")
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
main()
|
|
|
@ -1,18 +0,0 @@
|
||||||
import csv
|
|
||||||
|
|
||||||
def check_languages_installed(from_code, to_code):
|
|
||||||
try:
|
|
||||||
# Assuming the CSV file is named 'languages.csv' and located in the same directory
|
|
||||||
with open('data/installed_packages.csv', mode='r') as csv_file:
|
|
||||||
csv_reader = csv.reader(csv_file)
|
|
||||||
for row in csv_reader:
|
|
||||||
# Check if the current row matches the criteria
|
|
||||||
if len(row) >= 2 and row[0] == from_code and row[1] == to_code:
|
|
||||||
return True
|
|
||||||
return False
|
|
||||||
|
|
||||||
except FileNotFoundError:
|
|
||||||
return 1
|
|
||||||
except Exception:
|
|
||||||
return 1
|
|
||||||
|
|
|
@ -1,32 +0,0 @@
|
||||||
import argostranslate.translate
|
|
||||||
import argostranslate.package
|
|
||||||
from package_mangement.note_all_packages import write_installed_packages_to_csv
|
|
||||||
|
|
||||||
|
|
||||||
def install_language(from_code, to_code):
|
|
||||||
try:
|
|
||||||
argostranslate.package.update_package_index()
|
|
||||||
available_packages = argostranslate.package.get_available_packages()
|
|
||||||
# Convert the filter result into a list
|
|
||||||
filtered_packages = list(
|
|
||||||
filter(
|
|
||||||
lambda x: x.from_code == from_code and x.to_code == to_code, available_packages
|
|
||||||
)
|
|
||||||
)
|
|
||||||
# Check if the filtered list is empty
|
|
||||||
if not filtered_packages:
|
|
||||||
print(f"No available package for translating from {from_code} to {to_code}")
|
|
||||||
return 1
|
|
||||||
|
|
||||||
# If we have at least one package, proceed to download and install
|
|
||||||
available_package = filtered_packages[0]
|
|
||||||
download_path = available_package.download()
|
|
||||||
argostranslate.package.install_from_path(download_path)
|
|
||||||
|
|
||||||
# After successful installation, append package details to the CSV file
|
|
||||||
write_installed_packages_to_csv()
|
|
||||||
|
|
||||||
return 0
|
|
||||||
except Exception as e:
|
|
||||||
print(f"An error occurred: {e}")
|
|
||||||
return e
|
|
|
@ -1,22 +0,0 @@
|
||||||
import argostranslate.package
|
|
||||||
import csv
|
|
||||||
|
|
||||||
# Function to write installed package details into a CSV file
|
|
||||||
def write_installed_packages_to_csv(filename='data/installed_packages.csv'):
|
|
||||||
# Fetch all installed packages
|
|
||||||
installed_packages = argostranslate.package.get_installed_packages()
|
|
||||||
# Open/Create a CSV file to write into
|
|
||||||
with open(filename, mode='w', newline='', encoding='utf-8') as file:
|
|
||||||
writer = csv.writer(file)
|
|
||||||
|
|
||||||
# Write package details row by row
|
|
||||||
for package in installed_packages:
|
|
||||||
from_language = package.from_code
|
|
||||||
to_language = package.to_code
|
|
||||||
package_name = package.from_name
|
|
||||||
|
|
||||||
writer.writerow([from_language, to_language, package_name])
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
write_installed_packages_to_csv()
|
|
||||||
|
|
|
@ -1,8 +0,0 @@
|
||||||
import argostranslate.package
|
|
||||||
import argostranslate.translate
|
|
||||||
|
|
||||||
def translate(message, from_lang, to_lang):
|
|
||||||
translation = argostranslate.translate.translate(message, from_lang, to_lang)
|
|
||||||
return translation
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue