Skip to content

Commit

Permalink
Add Deepl translate support
Browse files Browse the repository at this point in the history
Fix module check, Apikey parse, newline

Add deeplkey transfer to translate script
  • Loading branch information
Toastyice authored and jvlflame committed Sep 26, 2021
1 parent 11dfb2b commit acb5f0a
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 8 deletions.
16 changes: 10 additions & 6 deletions src/Javinizer/Private/Get-TranslatedString.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,29 @@ function Get-TranslatedString {

[String]$Language = 'en',

[ValidateSet('googletrans', 'google_trans_new')]
[String]$TranslateDeeplApiKey,

[ValidateSet('googletrans', 'google_trans_new', 'deepl')]
[String]$Module
)

process {
if ($Module -eq 'google_trans_new') {
$translatePath = Join-Path -Path ((Get-Item $PSScriptRoot).Parent) -ChildPath 'translate_new.py'
} else {
} elseif($Module -eq 'googletrans') {
$translatePath = Join-Path -Path ((Get-Item $PSScriptRoot).Parent) -ChildPath 'translate.py'
} else{
$translatePath = Join-Path -Path ((Get-Item $PSScriptRoot).Parent) -ChildPath 'translate_deepl.py'
}

if ($Language -eq 'en') {
if ($String -match '[\u3040-\u309f]|[\u30a0-\u30ff]|[\uff66-\uff9f]|[\u4e00-\u9faf]') {
if ($null -ne $String -and $String -ne '') {
try {
if ([System.Environment]::OSVersion.Platform -eq 'Win32NT') {
$tempFile = python $translatePath $String $Language
$tempFile = python $translatePath $String $Language $TranslateDeeplApiKey
} elseif ([System.Environment]::OSVersion.Platform -eq 'Unix') {
$tempFile = python3 $translatePath $String $Language
$tempFile = python3 $translatePath $String $Language $TranslateDeeplApiKey
}
$translatedString = Get-Content -Path $tempFile -Encoding utf8 -Raw
} finally {
Expand All @@ -39,9 +43,9 @@ function Get-TranslatedString {
if ($null -ne $String -and $String -ne '') {
try {
if ([System.Environment]::OSVersion.Platform -eq 'Win32NT') {
$tempFile = python $translatePath $String $Language
$tempFile = python $translatePath $String $Language $TranslateDeeplApiKey
} elseif ([System.Environment]::OSVersion.Platform -eq 'Unix') {
$tempFile = python3 $translatePath $String $Language
$tempFile = python3 $translatePath $String $Language $TranslateDeeplApiKey
}
$translatedString = Get-Content -Path $tempFile -Encoding utf8 -Raw
} finally {
Expand Down
1 change: 1 addition & 0 deletions src/Javinizer/Private/Test-JVSettings.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ function Test-JVSettings {
'sort.metadata.nfo.displayname',
'sort.metadata.nfo.format.tagline',
'sort.metadata.nfo.translate.language'
'sort.metadata.nfo.translate.deeplapikey'
) | Test-JVSettingsGroup -Settings $Settings -Type String

$arraySettings = @(
Expand Down
9 changes: 7 additions & 2 deletions src/Javinizer/Public/Get-JVAggregatedData.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,10 @@ function Get-JVAggregatedData {
[Alias('sort.metadata.nfo.translate.language')]
[String]$TranslateLanguage,

[Parameter(ValueFromPipelineByPropertyName = $true, ParameterSetName = 'Setting')]
[Alias('sort.metadata.nfo.translate.deeplapikey')]
[String]$TranslateDeeplApiKey,

[Parameter(ValueFromPipelineByPropertyName = $true, ParameterSetName = 'Setting')]
[Alias('sort.metadata.nfo.translate.keeporiginaldescription')]
[Boolean]$KeepOriginalDescription,
Expand Down Expand Up @@ -229,6 +233,7 @@ function Get-JVAggregatedData {
$Translate = $Settings.'sort.metadata.nfo.translate'
$TranslateFields = $Settings.'sort.metadata.nfo.translate.field'
$TranslateLanguage = $Settings.'sort.metadata.nfo.translate.language'
$TranslateDeeplApiKey = $Settings.'sort.metadata.nfo.translate.deeplapikey'
$KeepOriginalDescription = $Settings.'sort.metadata.nfo.translate.keeporiginaldescription'
$DelimiterFormat = $Settings.'sort.format.delimiter'
$ActressLanguageJa = $Settings.'sort.metadata.nfo.actresslanguageja'
Expand Down Expand Up @@ -715,14 +720,14 @@ function Get-JVAggregatedData {
$translatedObject.PSObject.Properties | ForEach-Object {
if ($_.Name -in $TranslateFields) {
if ($_.Name -eq 'Genre') {
$_.Value = Get-TranslatedString -String ($aggregatedDataObject."$($_.Name)" -join '|') -Language $TranslateLanguage -Module $TranslateModule
$_.Value = Get-TranslatedString -String ($aggregatedDataObject."$($_.Name)" -join '|') -Language $TranslateLanguage -Module $TranslateModule -TranslateDeeplApiKey $TranslateDeeplApiKey
$genres = @()
$rawGenres = $_.Value -split '\|'
foreach ($genre in $rawGenres) {
$genres += ($genre).Trim()
}
} else {
$_.Value = Get-TranslatedString -String $aggregatedDataObject."$($_.Name)" -Language $TranslateLanguage -Module $TranslateModule
$_.Value = Get-TranslatedString -String $aggregatedDataObject."$($_.Name)" -Language $TranslateLanguage -Module $TranslateModule -TranslateDeeplApiKey $TranslateDeeplApiKey
}
if ($null -ne $_.Value -and ($_.Value).Trim() -ne '') {
if ($_.Name -eq 'Genre') {
Expand Down
1 change: 1 addition & 0 deletions src/Javinizer/jvSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@
"sort.metadata.nfo.translate.module": "googletrans",
"sort.metadata.nfo.translate.field": "description",
"sort.metadata.nfo.translate.language": "en",
"sort.metadata.nfo.translate.deeplapikey": "",
"sort.metadata.nfo.translate.keeporiginaldescription": false,
"sort.metadata.nfo.displayname": "[<ID>] <TITLE>",
"sort.metadata.nfo.firstnameorder": false,
Expand Down
23 changes: 23 additions & 0 deletions src/Javinizer/translate_deepl.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import sys
import requests
import tempfile
import os
import json

#Select Url based on key provided (free keys always end in :fx)
baseurl = "https://api-free.deepl.com/v2/translate" if sys.argv[3].endswith(":fx") else "https://api.deepl.com/v2/translate"

url = "{}?auth_key={}&text={}&target_lang={}".format(baseurl, sys.argv[3], sys.argv[1], sys.argv[2])
r = requests.get(url)
j = json.loads(r.text)
n = j['translations'][0]['text']

text = n.encode('utf8')

# Write the translated text to a temporary file to bypass encoding issues when redirecting the text to PowerShell
new_file, filename = tempfile.mkstemp()
os.write(new_file, text)
os.close(new_file)

# Return the path to the temporary file to read it from PowerShell
print(filename)

0 comments on commit acb5f0a

Please sign in to comment.