Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- import json
- import urllib.request
- from gtts import gTTS
- def request(action, **params):
- return {'action': action, 'params': params, 'version': 6}
- def invoke(action, **params):
- requestJson = json.dumps(request(action, **params)).encode('utf-8')
- response = json.load(urllib.request.urlopen(urllib.request.Request('http://localhost:8765', requestJson)))
- if len(response) != 2:
- raise Exception('response has an unexpected number of fields')
- if 'error' not in response:
- raise Exception('response is missing required error field')
- if 'result' not in response:
- raise Exception('response is missing required result field')
- if response['error'] is not None:
- raise Exception(response['error'])
- return response['result']
- def main():
- downloads_dir = '/Users/borshigida/Downloads'
- # invoke('createDeck', deck='test1')
- note_ids = invoke('findNotes', query='"deck:5000 Most Common French Words::[A. 3] Read & Speak Training (Where the Real Learning Happens)::I) Basic::[1] Read"')
- # note_ids = [1599852094954]
- print('got list of note ids: {}'.format(note_ids))
- notes_info = invoke('notesInfo', notes=note_ids)
- for ind in range(len(note_ids)):
- note_id = note_ids[ind]
- note_info = notes_info[ind]
- fields_dict = {
- 'Front': note_info['fields']['Front']['value'] + '<br>',
- 'Back': note_info['fields']['Back']['value'],
- }
- sentence = note_info['fields']['Front']['value']
- print(ind, sentence, end='')
- if 'sound:' in sentence:
- print(' -> skipping', end='')
- print()
- continue
- tts = gTTS(sentence, lang='fr')
- audio_filename = sentence + '.mp3'
- audio_filepath = os.path.join(downloads_dir, 'audio_tmp', audio_filename)
- tts.save(audio_filepath)
- invoke(
- 'updateNoteFields',
- note={
- 'id': note_id,
- 'fields': fields_dict,
- 'audio': [{'filename': sentence + '.mp3', 'path': audio_filepath, 'fields': ['Front']}],
- })
- print()
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement