2

I'm making a little .srt translator tool that will scrape each text line of a .srt file, translate via google translate, and make the replacements into a new file. I've got everything done except for make the actual replacements. Seems like it should be easy, but I'm struggling with the iteration.

Here's my code:

Set IE = CreateObject("internetexplorer.application")
Set objFSO = CreateObject("Scripting.FileSystemObject")

SourceCaptionFile = objFSO.GetParentFolderName(WScript.ScriptFullName) & "\captions.srt"
OutputCaptionFile = objFSO.GetParentFolderName(WScript.ScriptFullName) & "\NEW_captions.srt"

on error resume next

langURL_es = "https://translate.google.com/#view=home&op=translate&sl=en&tl=es"

'============================================================

Set objFile = objFSO.OpenTextFile(SourceCaptionFile, 1, true) 'for reading

Do until objFile.AtEndOfStream

    if instr(objFile.Readline,">") then
        capText = capText & objFile.ReadLine & vbcrlf
    end if
loop

objFile.Close

'============================================================

'Navigate to translator and get translations
IE.Visible = true
IE.Navigate langURL_es
Do While IE.Busy or IE.ReadyState <> 4: WScript.sleep 100: Loop
Do Until IE.Document.ReadyState = "complete": WScript.sleep 100: Loop

wscript.sleep 1000

capline = split(capText,vbcrlf)

for i = 0 to ubound(capline)

    ie.document.getelementbyid("source").innertext = capline(i)

    Do While IE.Busy or IE.ReadyState <> 4: WScript.sleep 100: Loop
    Do Until IE.Document.ReadyState = "complete": WScript.sleep 100: Loop

    for each div in ie.document.getelementsbytagname("div")
        if div.getAttribute("class") = "result-shield-container tlid-copy-target" then
            captrans = captrans & capline(i-1) & "," & div.innertext & vbcrlf
        end if
    next
next

'============================================================
'**************THIS IS THE SECTION THAT'S NOT WORKING RIGHT**************

'compare translations against captions.srt file and make replacements

Set objFile = objFSO.OpenTextFile(SourceCaptionFile, 1, true) 'for reading

splitfile = split(objfile.readall,vbcrlf)

for each a in splitfile

    arrCaptrans = split(captrans,",")

    for i = 0 to ubound(arrCaptrans)

            if a = arrCaptrans(i) then
                newline = newline & arrCaptrans(i+1) & vbcrlf
            else
                newline = newline & a & vbcrlf
            end if
    next
next

objFile.Close

wscript.echo newline
'============================================================
'Write translated file

Set objTransFile = objFSO.OpenTextFile(OutputCaptionFile, 2, true) 'for writing

objTransFile.write newline
objTransFile.Close


'============================================================

wscript.echo "Done"

Here is the output I'm expecting:

1
00: 00: 06,800 -> 00: 00: 11,040
-¡Tenemos que averiguar cómo abrir esto!

2
00: 00: 11,080 -> 00: 00: 13,040
-¿Qué haces con ese nickel?

3
00: 00: 13,160 -> 00: 00: 20,440
-Por eso necesitamos sacar el dinero del chocolate de aquí.

4
00: 00: 20,440 -> 00: 00: 22,080
-No hay chocolate dentro de eso.

5
00: 00: 22,580 -> 00: 00: 23,960
-Sí hay

Here is the contents of my source "captions.srt" file:

1
00:00:06,800 --> 00:00:11,040
-We have to figure out how to open this!

2
00:00:11,080 --> 00:00:13,040
-What are you doing with that nickel?

3
00:00:13,160 --> 00:00:20,440
-That's why we need to get the chocolate money out of here

4
00:00:20,440 --> 00:00:22,080
-There's no chocolate inside that.

5
00:00:22,580 --> 00:00:23,960
-Yes there is

*EDIT: I'm trying to compare each line of the source .srt file with the string I named "captrans". "captrans" contains rows of "sourcetext,translatedtext". I'm splitting on commas, reading each line of source file and if source file line matches (before comma) captrans then replace with (after comma) captrans. Hope this makes sense...

Thank you for any help you can provide!

Drivium
  • 537
  • 6
  • 24
  • 1
    You are reading the line to test, then you read it again to do something. So if line 1 = dosomething you dosomething to the next line. Put `ReadLine` into a variable, then test and operate on the variable. – Noodles Mar 23 '19 at 21:18
  • @Noodles "for each a in splitfile" should be the same as readline, right? It feeds me each line of the source .srt file. I think my issue lies in the parsing of the captrans string... or in my comparison method. – Drivium Mar 23 '19 at 21:45
  • I believe he is referring to these two lines: `if instr(objFile.Readline,">") then capText = capText & objFile.ReadLine & vbcrlf`. – K.Dᴀᴠɪs Mar 23 '19 at 21:51
  • @K.Dᴀᴠɪs - that section is working ok. That section just takes the text parts (not line numbers or times) and uses those to get translations from the site. The part I'm struggling with is replacing the correct lines from the source file and merging changes into a new file. I updated OP with a little more context (at the bottom). – Drivium Mar 23 '19 at 21:54
  • I commented the problematic section in the code – Drivium Mar 23 '19 at 21:58

0 Answers0