Skip to content

Commit

Permalink
add word audio, pitch, and clipboard functionaltiy
Browse files Browse the repository at this point in the history
  • Loading branch information
mathewthe2 committed Jul 3, 2022
1 parent c69b8e8 commit ac21bbb
Show file tree
Hide file tree
Showing 19 changed files with 500 additions and 372 deletions.
1 change: 1 addition & 0 deletions requirements/base.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ Pillow==8.4.0
PyYAML==6.0
pyinstaller==4.10
pynput==1.7.6
pyperclip==1.8.2
QtAwesome==1.1.1
174 changes: 0 additions & 174 deletions src/main/python/anki/anki_bridge.py

This file was deleted.

22 changes: 20 additions & 2 deletions src/main/python/anki/anki_connect.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@
import logging
import urllib.request
import time
import yaml
from threading import Thread
from .anki_model import AnkiModel
# from .word_audio import get_jpod_audio_base64'
from util.word_audio import get_jpod_audio_base64

def request(action, params):
return {'action': action, 'params': params, 'version': 6}
Expand Down Expand Up @@ -102,17 +101,28 @@ def set_deck(self, deck_name):

def create_anki_note(self, note_data):
if not self.anki_settings:
logging.error("Anki settings not configured")
return
field_value_map = self.anki_settings.get_field_value_map(self.model)
if not field_value_map:
logging.error("Anki field value map not configured")
return

fields = {}
screenshot_field = None
word_audio_field = None
for field, value in field_value_map.items():
if value.lower() == 'screenshot':
if 'screenshot' in note_data:
screenshot_field = field
elif value.lower() == 'word_audio':
if 'expression' in note_data and 'reading' in note_data:
note_data['word_audio'] = get_jpod_audio_base64(note_data['expression'], note_data['reading'])
word_audio_field = field
elif value.lower() == 'pitch':
if 'expression' in note_data and 'reading' in note_data:
if self.anki_settings.pitch:
fields[field] = self.anki_settings.pitch.get_pitch(note_data['expression'], note_data['reading'])
else:
fields[field] = note_data[value.lower()]

Expand All @@ -132,6 +142,14 @@ def create_anki_note(self, note_data):
screenshot_field
]
}]
if word_audio_field:
note['audio'] = [{
"data": note_data['word_audio'],
"filename": '_{}.mp3'.format(time.time()),
"fields": [
word_audio_field
]
}]
result = self.invoke('addNote', note=note)
return result

Expand Down
2 changes: 2 additions & 0 deletions src/main/python/anki/anki_settings.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import yaml
from japanese.pitch import Pitch

class AnkiSettings():
def __init__(self, appctxt):
self.anki_models_path = appctxt.get_resource('anki/user_models.yaml')
self.anki_defaults_path = appctxt.get_resource('anki/user_defaults.yaml')
self.active_model = None
self.active_field_value_map = None
self.pitch = Pitch(appctxt.get_resource('rikaisama/pitch_accents.sqlite'))

def get_default_deck_model(self):
with open(self.anki_defaults_path, 'r') as stream:
Expand Down
6 changes: 6 additions & 0 deletions src/main/python/call_handler.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@

from PyQt5.QtCore import QObject, pyqtSlot, QVariant
from japanese.pitch import Pitch
from util.word_audio import get_jpod_audio_url
import json
import pyperclip

# Bridge between PyQt and Web
class CallHandler(QObject):
Expand All @@ -23,6 +25,10 @@ def send_to_anki(self, args):
print('created')
print(result)

@pyqtSlot(QVariant)
def copy_to_clipboard(self, text):
pyperclip.copy(text)

@pyqtSlot(QVariant, result=str)
def get_pitch(self, args):
pitch_dictionary = Pitch(self.appctxt.get_resource('rikaisama/pitch_accents.sqlite'))
Expand Down
23 changes: 23 additions & 0 deletions src/main/python/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from configparser import ConfigParser

class Config():
def __init__(self, appctxt):
self.config_file = appctxt.get_resource('config.ini')
self.config_object = ConfigParser()

def read(self, section_name, key):
self.config_object.read(self.config_file, encoding='utf-8')
section = self.config_object[section_name]
return section[key]

def write(self, section_name, to_update_dict):
self.config_object.read(self.config_file, encoding='utf-8')
section = self.config_object[section_name]

# Update the key value
for key, value in to_update_dict.items():
section[key] = value

# Write changes back to file
with open(self.config_file, 'w', encoding='utf-8') as conf:
self.config_object.write(conf)
Loading

0 comments on commit ac21bbb

Please sign in to comment.