Conversion_Kitchen_Code/kitchen_counter/conversion/translation/external_conversion.py

434 lines
19 KiB
Python
Raw Normal View History

2024-04-27 09:33:09 +00:00
import sys
2024-05-10 10:51:20 +00:00
import os
import boto3
2024-04-27 09:33:09 +00:00
import pandas as pd
import datetime
2024-06-12 08:55:41 +00:00
import time
2024-05-10 10:51:20 +00:00
import json
import tempfile
2024-04-27 09:33:09 +00:00
# django
from django.core.files.base import ContentFile
# all external imports
from scriptAudit.models import States
2024-05-10 10:51:20 +00:00
from centralisedFileSystem.models import Script, File
2024-04-27 09:33:09 +00:00
from scriptAudit.models import ScriptAuditModel
from mnfapp.models import MNFScriptDatabase, SampleScript, ScriptTranslations
from users.models import UserCredentialsForBlockchain,BlockchainUserInfo
from lpp.models import LPPTASKDatabase
from centralizePayment.views import auto_refund
from juggernaut.views import update_juggernaut
from juggernaut.models import JuggernautPackage
2024-04-27 09:33:09 +00:00
from auto_email.views import sendmail
from lpp.views import task_assigner
2024-05-10 10:51:20 +00:00
from utils import filesystem, utilities
2024-04-27 09:33:09 +00:00
from conversion.translation.newconversion import ScriptTranslation
from scriptAudit.mnf_script_audit import NeutralAudit
from Blockchain2.DataStorage import uploadDataToIPFSNode
from Blockchain2.decryption import decryptionOfPrivate, decryptionOfUrl, download_file_System,viewDecryptionOfUrl,hash_decrypation
from Blockchain2.Conversion import UploadConversionData, getConversion, deleteConversion,scriptGetUserprojectIds
from Blockchain2.blockchainsetting import OWNER_KEY
from Blockchain2.block_user_info import user_info
from lpp.certificate.createCertificate import certificateGenrate
2025-03-25 06:50:10 +00:00
from django.conf import settings
2024-04-27 09:33:09 +00:00
from MNF.settings import BasePath
basePath = BasePath()
"""Converts a screenplay file"""
class Conversion:
def __init__(self, **conversion_params):
self.audit_user = conversion_params['audit_user']
2024-04-27 09:33:09 +00:00
self.user = conversion_params['user']
self.file_path = conversion_params['file_path']
self.original_script_object = MNFScriptDatabase.objects.get(
script_id=conversion_params['original_script_id'])
self.translated_script_object = ScriptTranslations.objects.get(
translation_id=conversion_params['translated_script_id'])
self.sample_id = conversion_params['sample_id']
self.existing_script = conversion_params['existing_script']
self.iteration_no = conversion_params['iteration_no']
self.juggernaut_pages_deduction = conversion_params["juggernaut_pages_deduction"]
self.language_set = conversion_params["language_set"]
self.amount_without_subscrption = conversion_params["amount_without_subscrption"]
self.response = {}
print("Conversion Process Initializing")
self.full_dialogue_option_choices = [True if value.strip() == "True" else False for value in
str(self.translated_script_object.full_dialogue_option_choices).split(",")]
# Getting all choices of user regarding Words of Dialogues Translation
self.sentence_dialogue_option_choices = [True if value.strip() == "True" else False for value in
str(self.translated_script_object.sentence_dialogue_option_choices).split(",")]
# Getting all choices of user regarding translation and transliteration choices of slug, speaker, etc.
self.other_option_choices = [True if value.strip() == "True" else False for value in
str(self.translated_script_object.other_option_choices).split(",")]
self.name_script = (str(((self.original_script_object.script.name).split("/"))[-1]).split("."))[0]
self.file_extension = (str(self.original_script_object.script.name).split("."))[-1]
def convert(self):
"""Audit Code starts here"""
print("Conversion Auditing is starting")
original_stdout = sys.stdout
2025-05-01 10:08:58 +00:00
script1 = str(self.file_path)
doc = open(script1, 'rb').read()
file = ContentFile(
doc,
(script1.split("/"))[-1],
)
language_code = "en"
result = filesystem.new_screenplay_without_audit_in_background(
self.audit_user,
self.audit_user.username,
str(self.name_script),
file,
"script-original",
language_code,
)
audit_id = result.get("script", {}).get("id")
ScriptAuditModel.objects.update_or_create(
script=Script.objects.get(
id=audit_id
),
defaults={"status": States.STARTED}
)
audit = NeutralAudit(audit_id, True)
status = ScriptAuditModel.objects.get(
script=Script.objects.get(
id=audit_id
2024-04-27 09:33:09 +00:00
)
2025-05-01 10:08:58 +00:00
)
try:
if self.file_extension == "fdx":
audit.audit_fdx()
else:
audit.audit()
status.status = "SUCCESS"
status.save()
to_email = [self.user.email]
email_code = 'SB1'
except Exception as e:
print("Error of Audit is:", e)
status.status = "FAILURE"
status.save()
to_email = [self.user.email]
email_code = 'SB2'
print(
"Script Audit failed due to some internal error."
" If you have made any payment for conversion "
"that will be refunded")
to = self.user.email
key_value = {
"User": self.user.username,
}
sendmail(to_email=[to], email_code="PP21", key_value=key_value)
# auto refund code
2024-04-27 09:33:09 +00:00
try:
2025-05-01 10:08:58 +00:00
auto_refund(self.translated_script_object.central_payment_id)
2024-04-27 09:33:09 +00:00
except Exception as e:
2025-05-01 10:08:58 +00:00
print("No Refund -> ", e)
return self.response
2024-04-27 09:33:09 +00:00
2025-05-01 10:08:58 +00:00
file_path_ = filesystem.get_file_path(audit_id, "script-csv")
script_data = MNFScriptDatabase.objects.get(script_id=self.original_script_object.script_id)
script_data.audit_id = str(audit_id)
script_data.save()
try:
df = pd.read_csv(file_path_, encoding="utf-8")
except UnicodeError:
df = pd.read_csv(file_path_, encoding="utf-16")
self.list_of_lists = df.values.tolist()
print("Audit Done")
2024-04-27 09:33:09 +00:00
sys.stdout = original_stdout
print("Audit Done")
self.translated_script_object.status = "Audited"
self.translated_script_object.save()
"""Translation Code Starts here"""
print("list of lists -> ", self.list_of_lists)
dataframe = self.list_of_lists
args = [self.full_dialogue_option_choices, self.sentence_dialogue_option_choices, self.other_option_choices]
2025-05-01 10:08:58 +00:00
print("printing args")
print("args[0] - full_dialogue_option_choices:", args[0])
print("args[1] - sentence_dialogue_option_choices:", args[1])
print("args[2] - other_option_choices:", args[2])
2024-04-27 09:33:09 +00:00
kwargs = {
"user": self.translated_script_object.user_id,
"iteration_no": self.iteration_no,
'dataframe': dataframe,
"non_dial_src_lang": self.original_script_object.nondial_src_language,
"non_dial_src_script": self.original_script_object.nondial_src_script,
"non_dial_dest_lang": self.translated_script_object.nondial_dest_language,
"non_dial_dest_script": self.translated_script_object.nondial_dest_script,
"dial_src_lang": self.original_script_object.dial_src_language,
"dial_src_script": self.original_script_object.dial_src_script,
"dial_dest_lang": self.translated_script_object.dial_dest_language,
"dial_dest_script": self.translated_script_object.dial_dest_script,
"translation_id": self.translated_script_object.translation_id,
2025-05-01 10:08:58 +00:00
"script_id": self.original_script_object.script_id,
"csv_path":file_path_,
"audit_id": audit_id,
2024-04-27 09:33:09 +00:00
}
2025-05-01 10:08:58 +00:00
print("printing kwargs")
for key, value in kwargs.items():
print(f"{key}: {value}")
2024-04-27 09:33:09 +00:00
2025-05-01 10:08:58 +00:00
2024-04-27 09:33:09 +00:00
try:
ScriptTranslation(*args, **kwargs)
except Exception as e:
self.translated_script_object.status = "Failed"
self.translated_script_object.error_desc = str(e)
self.translated_script_object.save()
print("Error in Conversion is:", e)
print("Script translation failed due to some code error. If you have made any payment for conversion"
" that will be refunded")
# email for failure
to = self.user.email
key_value = {
"User": self.user.username,
}
sendmail(to_email=[to], email_code="PP21", key_value=key_value)
# auto refund code
try:
auto_refund(self.translated_script_object.central_payment_id)
except Exception as e:
print("No Refund due to error", e)
return self.response
"""Translation Code Ends Here"""
sys.stdout = original_stdout
self.translated_script_object = ScriptTranslations.objects.get(
translation_id=self.translated_script_object.translation_id)
"""Translation Completion mail here"""
2024-08-03 12:24:21 +00:00
if not self.translated_script_object.lpp:
to = self.user.email
key_value = {
"User": self.user.username,
2025-05-01 10:08:58 +00:00
"Title": self.original_script_object.script_title,
"translated language":self.translated_script_object.dial_dest_language,
2024-08-03 12:24:21 +00:00
}
sendmail(to_email=[to], email_code="PP18", key_value=key_value)
2024-04-27 09:33:09 +00:00
2025-05-01 10:08:58 +00:00
2024-04-27 09:33:09 +00:00
# adding file to s3 output folder
object_name = "OUTPUT/" + (self.translated_script_object.translated_script_path.split("/"))[-1]
filee = basePath + self.translated_script_object.translated_script_path
2025-05-01 10:08:58 +00:00
bucket = "conversion-kitchen"
s3_client = boto3.client('s3',
aws_access_key_id="AKIAQVLBBGCB45RMLKVW",
aws_secret_access_key="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",
region_name="ap-south-1"
)
try:
response = s3_client.upload_file(filee, bucket, object_name)
s3_url = f"https://{bucket}.s3.ap-south-1.amazonaws.com/{object_name}"
print(response)
except Exception as e:
self.translated_script_object.error_desc = "Error in s3 output upload is " + str(e)
self.translated_script_object.save()
print("Error in s3 output upload is", e)
2024-04-27 09:33:09 +00:00
"""Vetting Process if Choosen"""
# sys.stdout = original_stdout
if self.translated_script_object.lpp:
X = LPPTASKDatabase()
X.user_id = self.translated_script_object.user_id
X.usernote = "Kindly check if the translated file is correct as per the Uploaded Document!"
X.translated_script = self.translated_script_object
X.generated_from = "conversion"
temp_amount = self.amount_without_subscrption
X.amoutgiventolpp_action = round((temp_amount / 2), 2)
X.amoutgiventolpp_dialogue = round((temp_amount / 2), 2)
X.save()
task_assigner(int(X.task_id), "conversion")
2024-04-30 12:29:42 +00:00
else:
"""Blockchain Upload Starts here"""
# sys.stdout = original_stdout
scriptconversion = {}
try:
print("trying blockchain 1")
current_datetime = datetime.datetime.now()
if UserCredentialsForBlockchain.objects.filter(user=self.user).exists():
obj = self.translated_script_object
obj2 = self.original_script_object
temp1 = str(obj2.script)
temp2 = str(obj.translated_script_path)
uploaded_script = f"{basePath}/media/{temp1}"
translated_scripttt = f"{basePath}{temp2}"
print("blockchain script conversion 3", uploaded_script, translated_scripttt)
with open(uploaded_script, 'rb') as f:
hash = uploadDataToIPFSNode(f)
scriptconversion["original_scriptFile_hash"] = hash
scriptconversion["original_scriptFile_path"] = uploaded_script
scriptconversion["date_at"] = current_datetime.strftime("%Y-%m-%d %H:%M:%S")
print("blockchain script conversion 4")
with open(translated_scripttt, 'rb') as f1:
hash = uploadDataToIPFSNode(f1)
scriptconversion["translated_scriptFile_hash"] = hash
scriptconversion["translated_scriptFile_path"] = translated_scripttt
print("blockchain script conversion 5")
2024-05-10 10:51:20 +00:00
2025-05-01 10:08:58 +00:00
# if obj.converted_audit_id:
print("audit blockchain")
print("blockchain script conversion 5.1")
# blockchain upload of csv-json data
try:
2024-05-24 06:10:17 +00:00
adit_id = obj.converted_audit_id
2025-05-01 10:08:58 +00:00
print(f"## aaaaaaaadit_id----> {adit_id}")
except:
print("audit id not found")
print(f"## audit id ---> {audit_id}")
file_to_audit = File.objects.get(
script=audit_id,
type="script-csv"
)
csv_script_path = file_to_audit.file.path
print(f"## csv_script_path ----> {csv_script_path}")
df = pd.read_csv(csv_script_path)
df = df.loc[:, ["content", "script_element"]]
script_json: dict = json.loads(utilities.csv_to_json(df))
with tempfile.TemporaryDirectory() as temp_dir:
print("Temporary directory created:", temp_dir)
temp_filename = os.path.join(temp_dir, 'script_json_file.json')
print(temp_filename)
with open(temp_filename, 'w') as json_file:
json.dump(script_json, json_file, indent=4)
script_json = {}
script_path1 = temp_filename
# script_size = file_to_audit_docx.file.size
with open(script_path1, 'rb') as _file:
hash2 = uploadDataToIPFSNode(_file)
print(f"## hash2 ----> {hash2}")
script_json["script_file_path"] = script_path1
script_json["script_file"] = hash2
script_json["type"] = "script-json"
script_json["screenplay_name"] = str(self.name_script)
scriptconversion["script-json"] = script_json
print("blockchain script conversion 5.2")
2024-05-10 10:51:20 +00:00
2024-04-30 12:29:42 +00:00
blockchain_obj = UserCredentialsForBlockchain.objects.get(user=self.user)
UserId = blockchain_obj.user_id
Project = obj.translation_id
Data = str(scriptconversion)
userPrivateKey = blockchain_obj.privateKey
userkey = decryptionOfPrivate(userPrivateKey)
tx_id, tx_gas_fee = UploadConversionData(OWNER_KEY, blockchain_obj.publicKey, UserId, str(Project),
Data)
2024-05-02 07:48:38 +00:00
2024-04-30 12:29:42 +00:00
print("Tx id -> ", tx_id,tx_gas_fee)
2024-05-02 07:48:38 +00:00
print(type(tx_id))
2025-05-01 10:08:58 +00:00
2024-04-30 12:29:42 +00:00
try:
user_infos = user_info(tx_hash=tx_id, service="Conversion", gas_fee=tx_gas_fee,
script_name=self.name_script)
addition_result = user_infos.update_info(self)
print("Blockchain Result -> ",addition_result)
except Exception as e:
print("Error:", e)
print("blockchain script conversion 6")
2025-03-25 06:50:10 +00:00
# certificatepath = certificateGenrate(self.user.username, "script conversion", hash)
certificatepath = certificateGenrate(self.user.username, "script conversion", hash, projectname=self.name_script, matic = tx_gas_fee)
2024-04-30 12:29:42 +00:00
hash = hash_decrypation(hash)
to_email = [self.user.email]
email_code = 'BL1'
2025-05-01 10:08:58 +00:00
2024-04-30 12:29:42 +00:00
key_value = {
2025-03-25 06:50:10 +00:00
"Product Name": "script conversion",
"Profile url": f"{settings.SITE_DOMAIN}/memberpage/#/personaldetails",
"User url": f"{settings.SITE_DOMAIN}/memberpage/#/user/{self.user.id}/personaldetails",
"Product output card url": f"{settings.SITE_DOMAIN}/conversion/view_conversion/",
2024-04-30 12:29:42 +00:00
}
sendmail(to_email=to_email, email_code=email_code,key_value=key_value, filePath=certificatepath)
2024-04-30 12:29:42 +00:00
except Exception as e:
self.translated_script_object.error_desc = "Error in blockchain is " + str(e)
self.translated_script_object.save()
2024-04-30 12:29:42 +00:00
print("Error in blockchain is", e)
"""Blockchain Upload Ends here"""
2024-04-27 09:33:09 +00:00
2024-05-02 07:48:38 +00:00
2024-04-27 09:33:09 +00:00
"""Process Completed"""
self.translated_script_object = ScriptTranslations.objects.get(
translation_id=self.translated_script_object.translation_id)
2024-04-27 09:33:09 +00:00
self.translated_script_object.status = "Completed"
2024-05-02 07:48:38 +00:00
try:
self.translated_script_object.blockchain_txhash = tx_id
self.translated_script_object.translated_script_pdf = s3_url
2024-05-02 07:48:38 +00:00
except:
self.translated_script_object.translated_script_pdf = s3_url
2024-04-27 09:33:09 +00:00
self.translated_script_object.save()
return self.response
# from pathlib import Path
# basePath = Path(__file__).resolve().parent.parent
# translation_script = ScriptTranslations.objects.get(translation_id="")
#
# conversion_params = {
# "user": translation_script.user_id,
# "file_path": str(basePath) + "/media/" + translation_script.script_link_id.script.name,
# "original_script_object": translation_script.script_link_id,
# "translated_script_object": translation_script,
# "sample_id": None,
# "existing_scrip": None
# }
2024-04-30 04:59:37 +00:00
# obj = Conversion(**conversion_params)