diff options
| author | navewindre <boneyaard@gmail.com> | 2025-07-18 01:57:19 +0200 |
|---|---|---|
| committer | navewindre <boneyaard@gmail.com> | 2025-07-18 01:57:19 +0200 |
| commit | c795089a41d06fb60e1f5e0c94624ab186ce306c (patch) | |
| tree | e966cb42edb455971195c7b3f51f0f776fffee0e | |
| parent | 01499022f3a1b37f68026afefd46eb26f340b61b (diff) | |
a
| -rw-r--r-- | config/mpv/scripts/jaSubs/jaSubs.py | 181 | ||||
| -rwxr-xr-x | slackware-bootstrap/multilib | 23 |
2 files changed, 147 insertions, 57 deletions
diff --git a/config/mpv/scripts/jaSubs/jaSubs.py b/config/mpv/scripts/jaSubs/jaSubs.py index 809abdf..d68f1a1 100644 --- a/config/mpv/scripts/jaSubs/jaSubs.py +++ b/config/mpv/scripts/jaSubs/jaSubs.py @@ -10,6 +10,8 @@ import threading, queue import calendar, math, base64 import numpy import ast +import hashlib +import json as jsonlib from urllib.parse import quote from json import loads @@ -32,6 +34,7 @@ was_paused = 0 tthread = 0 app = 0 current_text = '' +mutex = threading.Lock() pth = os.path.expanduser('~/.config/mpv/scripts/') os.chdir(pth) @@ -40,27 +43,40 @@ import config as config def katakana_to_hiragana(text): return "".join(chr(ord(c) - 0x60) if "ァ" <= c <= "ン" else c for c in text) +jisho_cache_file="./jisho_cache.json" +if os.path.exists(jisho_cache_file): + with open(jisho_cache_file, 'r', encoding='utf-8') as f: + try: + cache = jsonlib.load(f) + except: + cache = {} +else: + cache = {} + # returns ([[word: reading, translation]..], [morphology = '', gender = '']) # jisho.org def jisho(word): - DOMAIN = 'jisho.org' - VERSION = '1' - + DOMAIN='jisho.org' + VERSION=1 base_url = f'https://{DOMAIN}/api/v{VERSION}' - def get(url, params=None): if params is None: params = {} - response = requests.get( - url, - params=params, - ) + key = hashlib.sha256((url + jsonlib.dumps(params, sort_keys=True)).encode()).hexdigest() + + if key in cache: + json = cache[key] + else: + response = requests.get(url, params=params) + if response.status_code != 200: + raise APIException(response.status_code, response.content.decode()) + + json = response.json() + cache[key] = json - json = response.json() - if response.status_code != 200: - raise APIException(response.status_code, - response.content.decode()) + with open(jisho_cache_file, 'w', encoding='utf-8') as f: + jsonlib.dump(cache, f, ensure_ascii=False, indent=2) try: word = json['data'][0]['japanese'][0]['word'] + ': ' + json['data'][0]['japanese'][0]['reading'] @@ -68,11 +84,10 @@ def jisho(word): translations = json['data'][0]['senses'][0]['english_definitions'] pairs = [[word, '']] for definition in translations: - pairs.append(['', definition]) - + pairs.append(['', definition]) return pairs, [reading, ''] except: - return [['No translation Found', ''], ['', '']] + return [['No translation Found', ''], ['', '']] def search(keyword): url = f'{base_url}/search/words' @@ -215,59 +230,86 @@ class TokenAcquirer: """ return (val % 0x100000000) >> n -# translate.google.com -def google(word): - word = word.replace('\n', ' ').strip() - url = 'https://translate.google.com/translate_a/single?client=t&sl={lang_from}&tl={lang_to}&hl={lang_to}&dt=at&dt=bd&dt=ex&dt=ld&dt=md&dt=qca&dt=rw&dt=rm&dt=ss&dt=t&ie=UTF-8&oe=UTF-8&otf=1&pc=1&ssel=3&tsel=3&kc=2&q={word}'.format(lang_from = config.lang_from, lang_to = config.lang_to, word = quote(word)) +google_cache_path = './google_cache.json' - pairs = [] - fname = 'urls/' + url.replace('/', "-") - try: - if ' ' in word: - raise Exception('skip saving') +# load cache if exists +if os.path.exists(google_cache_path): + with open(google_cache_path, 'r', encoding='utf-8') as f: + try: + google_cache = jsonlib.load(f) + except: + google_cache = {} +else: + google_cache = {} + +def google(word): + word = word.replace('\n', ' ').strip() - p = open(fname).read().split('=====/////-----') + if word in google_cache: + retword = google_cache[word] + return retword, ['', ''] + + url = 'https://translate.google.com/translate_a/single?client=t&sl={lang_from}&tl={lang_to}&hl={lang_to}&dt=at&dt=bd&dt=ex&dt=ld&dt=md&dt=qca&dt=rw&dt=rm&dt=ss&dt=t&ie=UTF-8&oe=UTF-8&otf=1&pc=1&ssel=3&tsel=3&kc=2&q={word}'.format( + lang_from=config.lang_from, + lang_to=config.lang_to, + word=quote(word) + ) + + pairs = [] + fname = 'urls/' + url.replace('/', "-") try: - word_descr = p[1].strip() + if ' ' in word: + raise Exception('skip saving') + + p = open(fname).read().split('=====/////-----') + try: + word_descr = p[1].strip() + except: + word_descr = '' + + for pi in p[0].strip().split('\n\n'): + pi = pi.split('\n') + pairs.append([pi[0], pi[1]]) except: - word_descr = '' + acquirer = TokenAcquirer() + tk = acquirer.do(word) - for pi in p[0].strip().split('\n\n'): - pi = pi.split('\n') - pairs.append([pi[0], pi[1]]) - except: - acquirer = TokenAcquirer() - tk = acquirer.do(word) + url = f'{url}&tk={tk}' + p = requests.get(url, headers={ + 'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.167 Safari/537.36' + }).text + p = loads(p) - url = '{url}&tk={tk}'.format(url = url, tk = tk) - p = requests.get(url, headers={'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.167 Safari/537.36'}).text - p = loads(p) + try: + pairs.append([p[0][0][0], p[0][0][1]]) + except: + pass - try: - pairs.append([p[0][0][0], p[0][0][1]]) - except: - pass + if p[1] is not None: + for translations in p[1]: + for translation in translations[2]: + try: + t1 = translation[5] + ' ' + translation[0] + except: + t1 = translation[0] - if p[1] != None: - for translations in p[1]: - for translation in translations[2]: - try: - t1 = translation[5] + ' ' + translation[0] - except: - t1 = translation[0] + t2 = ', '.join(translation[1]) - t2 = ', '.join(translation[1]) + if not len(t1): + t1 = '-' + if not len(t2): + t2 = '-' - if not len(t1): - t1 = '-' - if not len(t2): - t2 = '-' + pairs.append([t1, t2]) - pairs.append([t1, t2]) + word_descr = '' - word_descr = '' + # store result in cache and save + google_cache[word] = pairs + with open(google_cache_path, 'w', encoding='utf-8') as f: + jsonlib.dump(google_cache, f, ensure_ascii=False, indent=2) - return pairs, ['', ''] + return pairs, ['', ''] def pause_on_popup(): global was_paused @@ -434,10 +476,16 @@ class thread_translations(QObject): QApplication.setOverrideCursor(Qt.WaitCursor) threads = [] + while mutex.locked(): + time.sleep(config.update_time) + + mutex.acquire() for translation_function_name in config.translation_function_names: threads.append(threading.Thread(target = globals()[translation_function_name], args = (word,))) for x in threads: x.start() + + mutex.release() while any(thread.is_alive() for thread in threads): if config.queue_to_translate.qsize(): to_new_word = True @@ -452,7 +500,9 @@ class thread_translations(QObject): if config.block_popup: continue + mutex.acquire() self.get_translations.emit(word, globalX, False) + mutex.release() # drawing layer # because can't calculate outline with precision @@ -781,7 +831,9 @@ class main_class(QWidget): self.clearLayout('subs2') if hasattr(self, 'popup'): + mutex.acquire() self.popup.hide() + mutex.release() # if subtitle consists of one overly long line - split into two if config.split_long_lines_B and len(subs.split('\n')) == 1 and len(subs.split(' ')) > config.split_long_lines_words_min - 1: @@ -876,7 +928,9 @@ class main_class(QWidget): line = 'Google translation failed.' if config.split_long_lines_B and len(line.split('\n')) == 1 and len(line.split(' ')) > config.split_long_lines_words_min - 1: line = split_long_lines(line) + mutex.acquire() self.translation_done.emit(line, True, []) + mutex.release() else: word = self.text translations = [] @@ -885,23 +939,30 @@ class main_class(QWidget): if not pairs: pairs = [['', '[Not found]']] translations.append((pairs, word_descr)) - self.translation_done.emit(word, False, translations) + self.translation_done.emit(word, False, translations) def render_popup(self, text, x_cursor_pos, is_line): global tthread global app global current_text + if mutex.locked(): + return + mutex.acquire(); if len(current_text) and text == current_text and hasattr(self, 'popup') and self.popup.isVisible(): + mutex.release() return if text == '': - if hasattr(self, 'popup'): + if hasattr(self, 'popup') and self.popup.isVisible(): self.popup.hide() + mutex.release() return current_text = text QApplication.setOverrideCursor(Qt.WaitCursor) + def update_popup(result, is_line, data): + mutex.acquire() self.clearLayout('popup') word = text if is_line: @@ -999,11 +1060,17 @@ class main_class(QWidget): app.sendPostedEvents() self.popup.show() QApplication.restoreOverrideCursor() + mutex.release() + + if tthread: + tthread.terminate() + tthread.wait() tthread = self.TranslationThread(text, is_line) tthread.translation_done.connect(update_popup) tthread.start() + mutex.release() def update_screen(): if not mpv_fullscreen_status(): diff --git a/slackware-bootstrap/multilib b/slackware-bootstrap/multilib index 796bf71..234e20c 100755 --- a/slackware-bootstrap/multilib +++ b/slackware-bootstrap/multilib @@ -13,6 +13,28 @@ fi if [[ $iscurrent == 1 ]]; then slpkg --repository="multilib" -i -y compat32-tools + echo "===================== [ compat32 conversion ] =======================" + echo "in order to run 32bit software (e.g. steam), and not just compile it" + echo "you need to convert 64bit system packages to 32bit compatibility" + echo "this script can conver all system packages to 32bit" + echo "=====================================================================" + + doconvert=0 + read -p "do you want to convert all 64-bit packages to 32bit compatibility? [y/n] (y)" doconvert + + if [[ $doconvert == 1 ]]; then + mkdir ~/convert32 + pushd ~/convert32 + /usr/sbin/massconvert.sh -u https://mirror.yandex.ru/slackware/slackware-current/slackware + + for dir in $(ls -d *-compat32); do + pushd $dir + upgradepkg --install-new *.t?z + popd + done + + popd + fi fi if [[ $iscurrent == 0 ]]; then @@ -22,5 +44,6 @@ if [[ $iscurrent == 0 ]]; then upgradepkg --install-new slackware64-compat32/*-compat32/*.t?z fi + cd $oldpwd touch ~/.multilib-done |
