Advertisement
borshigida

Untitled

Aug 20th, 2023
854
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.21 KB | None | 0 0
  1. import os
  2. import json
  3.  
  4. import urllib.request
  5.  
  6. from gtts import gTTS
  7.  
  8.  
  9. def request(action, **params):
  10.     return {'action': action, 'params': params, 'version': 6}
  11.  
  12.  
  13. def invoke(action, **params):
  14.     requestJson = json.dumps(request(action, **params)).encode('utf-8')
  15.     response = json.load(urllib.request.urlopen(urllib.request.Request('http://localhost:8765', requestJson)))
  16.     if len(response) != 2:
  17.         raise Exception('response has an unexpected number of fields')
  18.     if 'error' not in response:
  19.         raise Exception('response is missing required error field')
  20.     if 'result' not in response:
  21.         raise Exception('response is missing required result field')
  22.     if response['error'] is not None:
  23.         raise Exception(response['error'])
  24.     return response['result']
  25.  
  26.  
  27. def main():
  28.     downloads_dir = '/Users/borshigida/Downloads'
  29.  
  30.     # invoke('createDeck', deck='test1')
  31.     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"')
  32.     # note_ids = [1599852094954]
  33.     print('got list of note ids: {}'.format(note_ids))
  34.  
  35.     notes_info = invoke('notesInfo', notes=note_ids)
  36.  
  37.     for ind in range(len(note_ids)):
  38.         note_id = note_ids[ind]
  39.         note_info = notes_info[ind]
  40.  
  41.         fields_dict = {
  42.             'Front': note_info['fields']['Front']['value'] + '<br>',
  43.             'Back': note_info['fields']['Back']['value'],
  44.         }
  45.  
  46.         sentence = note_info['fields']['Front']['value']
  47.         print(ind, sentence, end='')
  48.         if 'sound:' in sentence:
  49.             print(' -> skipping', end='')
  50.             print()
  51.             continue
  52.  
  53.         tts = gTTS(sentence, lang='fr')
  54.         audio_filename = sentence + '.mp3'
  55.         audio_filepath = os.path.join(downloads_dir, 'audio_tmp', audio_filename)
  56.         tts.save(audio_filepath)
  57.  
  58.         invoke(
  59.             'updateNoteFields',
  60.             note={
  61.                 'id': note_id,
  62.                 'fields': fields_dict,
  63.                 'audio': [{'filename': sentence + '.mp3', 'path': audio_filepath, 'fields': ['Front']}],
  64.             })
  65.         print()
  66.  
  67.  
  68. if __name__ == '__main__':
  69.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement