krmanik / Anki-Chinese-Vocabulary-Generator

Enter only simplified characters and create word meaning with Traditional, Pinyin, Meaning, Audio and example sentences
Other
30 stars 6 forks source link

Exceptions generated, could not get output.txt #3

Closed extenlabs closed 3 years ago

extenlabs commented 3 years ago

I've got these exceptions and could not generate any output.txt.

user@user Anki-Chinese-Vocabulary-Generator-master % python3 main_for_mac.py

Audio not found in data folder, fetching online
Audio not found in data folder, fetching online
Audio not found in data folder, fetching online
json not found in data folder, fetching online

Exception in Tkinter callback

Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/tkinter/__init__.py", line 1885, in __call__
    return self.func(*args)
  File "/Users/user/Downloads/Anki-Chinese-Vocabulary-Generator-master/main_for_mac.py", line 477, in import_file
    self.insert_meaning(l.strip())
  File "/Users/user/Downloads/Anki-Chinese-Vocabulary-Generator-master/main_for_mac.py", line 518, in insert_meaning
    t = translator.translate(ch_sim, src='zh-cn', dest="en")
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/googletrans/client.py", line 182, in translate
    data = self._translate(text, dest, src, kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/googletrans/client.py", line 78, in _translate
    token = self.token_acquirer.do(text)
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/googletrans/gtoken.py", line 194, in do
    self._update()
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/googletrans/gtoken.py", line 62, in _update
code = self.RE_TKK.search(r.text).group(1).replace('var ', '')

AttributeError: 'NoneType' object has no attribute 'group'

Thanks a lot for your help!

krmanik commented 3 years ago

View this may help https://stackoverflow.com/questions/52455774/googletrans-stopped-working-with-error-nonetype-object-has-no-attribute-group

extenlabs commented 3 years ago

I had the previous version of googletrans, reinstalled all dependencies, including googletrans (pip3 install googletrans==4.0.0rc1), and it worked. Thank you very much for the tool, it saved me a lot of time.

Can you create something similar for Japanese? Downloading audio is not an issue but linking it with other data to import everything to Anki is another matter.

krmanik commented 3 years ago

I am also planning similar tool for Japanese. In meantime you can use this api.

https://jisho.org/api/v1/search/words?keyword=

If words.txt file contains following japanese characters

学
好
但

Then using following scripts, reading and meaning can be fetched.

import requests,  json

base_url = "https://jisho.org/api/v1/search/words?keyword="

words_file = "words.txt"

save_meaning = "meaning.txt"

m_file = open(save_meaning, "w", encoding="utf-8")

with open(words_file, "r", encoding="utf-8") as f:
    for line in f:
        url = base_url + line
        response = requests.get(url)
        print(line)
        if response.status_code == 200:
            data = response.json()

            reading = data["data"][0]["japanese"][0]["reading"]
            print (reading)

            meaning = data["data"][0]["senses"][0]["english_definitions"]
            print (meaning)

            print("\n")

            # word, reading, meaning
            w_r_m = line.rstrip() + "\t" + str(reading) + "\t" + ", ".join(meaning) + "\n"
            m_file.write(w_r_m)
        #print(response)

m_file.close()
f.close()

After fetching data, it get written to meaning.txt file

学   がく  learning, scholarship, study, erudition, knowledge, education
好   こう  good
但   ただ  ordinary, common, usual

Audio can also be linked to output result.

extenlabs commented 3 years ago

Awesome, thank you! I could not fetch the audio but generated some data. It is a great starting point. Now it's time to study 🙂.