#!/usr/bin/env python3
# -*- coding: UTF-8 -*-
#author:Wos
#according your language to change the "to_language" parameter
#support youtube subtitle translator
#This script follows the principle of converting non-Chinese to Chinese and Chinese to English when translating
import readline
import requests
import pyperclip
import os
import time
import sys
import base64
import json

BASE_DIR = os.path.dirname(os.path.abspath(__file__))
html_file = os.path.join(BASE_DIR, "translator.html")

ss = [] #result list
count = 0 #translator segment count
idx = 0 #index of serverlist
tp = ""
lenstr = ""
frlang = "English"
tolang = "Chinese (Simplified)"
serverlist = [
              'https://translate.tiekoetter.com/',
              'https://st.privacydev.net/',
              'https://simplytranslate.pussthecat.org/'
              ]

#Determine whether the string contains Chinese, paving the way for subsequent translation methods.
def is_contains_chinese(s):
    cnt = 0
    for i in s:
        if u'\u4e00' <= i <= u'\u9fff':
            cnt += 1

    if cnt > int(len(s)/4):
        return True
    else:
        return False
def usage():
    print("copy youtube link or text,then run the script without parameter")
    print("if length of text smaller than 5000,it will display directly in terminal without translator.html file")
    print("python trans.py (translate from clipboard)")
    print("python trans.py [file-name]  (translate from a file)")
    print("python trans.py show (display previous result,only about long content translation)")
    print("python trans.py -i(interactive mode)")


def fanyi(s):
    global ss,idx,lenstr
    res = ""
    premain = serverlist[idx]
    url = premain+"?engine=google"
    data = {
         "from_language":frlang,
         "to_language":tolang,
         "input":s
        }
    while True:
        try:
            res = requests.post(url,data=data,timeout=(5,7))
            break
        except:
            if idx < len(serverlist)-1:
                idx += 1
            else:
                idx = 0
            return fanyi(s)
    try:
        content=str(res.text.split("placeholder=\"Translation\" readonly>")[1].split("</textarea>")[0])
    except:
        if idx < len(serverlist)-1:
            idx += 1
        else:
            idx = 0
        return fanyi(s)
    if str(content).strip().startswith("body{overflow"):
        print("Translate Except Content:"+premain+"\n"+str(s))
    elif lenstr == "small":
        print(content)
        pyperclip.copy(content)
        lenstr = ""
    else:
#        print("Translator From :"+premain)
        ss.append(content)

def multi_translator(s):
    global count
    if len(s) < 5000:
#        print("segment "+str(count))
        fanyi(s)
        return
    else:
        dl = str(s).split("\n")
        s_ = ""
        i = 0
        while i < len(dl):
            s_ ="\n".join(dl[:i])
            if len(s_) < 5000:
                i += 1
                continue
            else:
#                print("segment "+str(count))
                count += 1
                if tp == "":
                    s_ ="\n".join(dl[:i-1])
                    fanyi(s_)
                    s_ ="\n".join(dl[i-1:])
                    return multi_translator(s_)
                else:
                    s_ =" ".join(dl[:i-1])
                    fanyi(s_)
                    s_ =" ".join(dl[i-1:])
                    return multi_translator(s_)













def save_to_html():
    global ss,first
    if lenstr == "":
        file_obj = open(str(html_file),'w',encoding='UTF-8', newline='')
        file_obj.write("<html>\n<head>\n<base target=\"_blank\">\n<meta charset=\"UTF-8\">\n")
        file_obj.write("<style>body{background-color:#000000;color:#ffffff;font-size:1.4em;}pre{white-space: pre-wrap;word-wrap: break-word;}</style>\n")
        file_obj.write("</head>\n")
        file_obj.write("<body><pre>\n")    
        for i in ss:
            if str(i) != "":
                 file_obj.write(str(i)+"\n")
        file_obj.write("</pre></body>\n</html>")
        file_obj.close()
        cmdline = ('sh -c \'surf -s -a -d -T -g -n -x %s 2>/dev/null &\''%(html_file))
        os.system(cmdline)
    else:
        for i in ss:
            if str(i) != "":
                print(i)
if __name__ == '__main__':
    try:
        if sys.argv[1] == "show" and len(sys.argv) == 2:
            cmdline = ('sh -c \'surf -s -a -d -T -g -n -x %s 2>/dev/null &\''%(html_file))
            os.system(cmdline)
            sys.exit()
        elif sys.argv[1] == "-h" and len(sys.argv) == 2:
            usage()
            sys.exit()
        elif sys.argv[1] == "-i" and len(sys.argv) == 2:
            os.system('clear')
            print("Interactive mode, you can input any language, the default non-Chinese is converted to Chinese, Chinese is converted to English, and the translation will be copied to the clipboard")
            while True:
                text = input("\n>")
                if str(text).strip() == "exit()":
                    sys.exit()
                elif str(text).strip() == "":
                    os.system('clear')
                    print("Interactive mode, you can input any language, the default non-Chinese is converted to Chinese, Chinese is converted to English, and the translation will be copied to the clipboard")
                    continue
                if len(text) < 5000:
                    lenstr = "small"
                if is_contains_chinese(text):
                    frlang = "Chinese (Simplified)"
                    tolang = "English"
                else:
                    frlang = "Auto"
                    tolang = "Chinese (Simplified)"
                multi_translator(text)


        elif sys.argv[1] != "":
            if os.path.exists(sys.argv[1]):
                cmdline = ("xclip %s -selection clipboard"%(sys.argv[1]))
                os.system(cmdline)
                text = pyperclip.paste()
                if len(text) < 5000:
                    lenstr = "small"
                if is_contains_chinese(text):
                    frlang = "Chinese (Simplified)"
                    tolang = "English"
                else:
                    frlang = "Auto"
                    tolang = "Chinese (Simplified)"
                multi_translator(text)
                save_to_html()
            else:
                text = sys.argv[1]
                if len(text) < 5000:
                    lenstr = "small"
                if is_contains_chinese(text):
                    frlang = "Chinese (Simplified)"
                    tolang = "English"
                else:
                    frlang = "Auto"
                    tolang = "Chinese (Simplified)"
                multi_translator(text)
                save_to_html()

    except: 
        if len(sys.argv) == 1: 
            os.system("clear")
            text = pyperclip.paste()
#            print("translator is running...")
            if str(text).startswith("https://") and len(text) < 50:
                vid = ""
                if "watch?v=" in str(text):
                    vid = str(text).split("watch?v=")[1].split("&")[0]
                elif "embed/" in str(text):
                    vid = str(text).split("embed/")[1].split("?")[0]
                elif str(text).startswith("https://youtu.be/") or str(text).startswith("https://yewtu.be"):
                    vid = str(text).split("/")[-1]
                tp = "ytb"
                vid = str(text).split("watch?v=")[1].split("&")[0]
                url= "https://downsub.com/?url=https://www.youtube.com/watch?v="+str(vid)
                res = requests.get(url)
                txt1 = (res.text.split("context=\'")[1].split("'")[0])
                txt2 = (str(base64.b64decode(txt1)).split("\"id\":\"")[1].split("\"")[0])
                url2 = "https://get-info.downsub.com/"+str(txt2)
                res2 = requests.get(url2).json()
                vname = res2['title']
                ensub = res2['subtitles'][0]['url']
                st = "[English (auto-generated)] "
                url3 = "https://subtitle2.downsub.com/?title="+str(st)+str(vname).strip()+" [DownSub.com]&url="+str(ensub)+"&type=txt"
                subtxt = requests.get(url3).text
                multi_translator(subtxt)
                save_to_html()
            elif str(text).strip() != "":
                if len(text) < 5000:
                    lenstr = "small"
                if is_contains_chinese(text):
                    frlang = "Chinese (Simplified)"
                    tolang = "English"
                else:
                    frlang = "Auto"
                    tolang = "Chinese (Simplified)"

                multi_translator(text)
                save_to_html()
            else:
                usage()
                sys.exit()