Exemplo n.º 1
0
    def translate(self, flash_card: FlashCard):
        result = self.translate_client.translate_text(
            Text=flash_card.source_phrase,
            SourceLanguageCode=flash_card.source_lang,
            TargetLanguageCode=self.target_language_code)

        translated_text = result.get('TranslatedText')
        flash_card.dest_voice = self.voice_id
        flash_card.dest_phrase = translated_text
        flash_card.dest_lang = self.target_language_code
        return flash_card
Exemplo n.º 2
0
    def write_translation(self, flash_card: FlashCard) -> bool:
        try:
            data = flash_card.to_string()
            key = os.path.join(flash_card.url, self.flashcard_file)
            response = self.s3_client.put_object(Bucket=self.s3_bucket,
                                                 Key=key,
                                                 Body=data,
                                                 ACL='public-read')
            presigned: str = self.s3_client.generate_presigned_url(
                'get_object',
                Params={
                    'Bucket': self.s3_bucket,
                    'Key': flash_card.url
                },
                ExpiresIn=3600)
            if presigned and len(presigned) > 0:
                return True

            return False

        except ClientError as e:
            # AllAccessDisabled error == bucket not found
            # NoSuchKey or InvalidRequest error == (dest bucket/obj == src bucket/obj)
            logging.error(e)
            return False
        except Exception as ex:
            # AllAccessDisabled error == bucket not found
            # NoSuchKey or InvalidRequest error == (dest bucket/obj == src bucket/obj)
            logging.error(ex)
            return False
Exemplo n.º 3
0
 def write_translation(self, flash_card: FlashCard) -> bool:
     dir_name = os.path.join(self.root_directory, flash_card.url)
     file_name = os.path.join(dir_name, self.flashcard_file)
     if not os.path.exists(dir_name):
         os.makedirs(dir_name)
     with open(file_name, "w") as fp:
         fp.write(flash_card.to_string())
     return True
Exemplo n.º 4
0
 def store_audio(self, flash_card: FlashCard) -> str:
     try:
         response = self.synthesiser.synthesize_speech(flash_card)
         url = self.storage_manager.write_audio(
             flash_card=flash_card, buffer=response['AudioStream'].read())
         flash_card.dest_mp3_url = url
         return url
     except Exception as e:
         print(e)
     return ""
Exemplo n.º 5
0
    def read_translation(self, flash_card: FlashCard) -> str:
        file_name = os.path.join(self.root_directory, flash_card.url,
                                 self.flashcard_file)
        if not os.path.exists(file_name):
            return ""
        with open(file_name, "r") as fp:
            text = fp.read()
            json_flash = text

        flash_dict = json.loads(json_flash)
        id_value = flash_dict['id']
        source_phrase = flash_dict['source_phrase']
        dest_phrase = flash_dict['dest_phrase']
        if flash_card.id != id_value or flash_card.source_phrase != source_phrase:
            return ""
        flash_card.dest_phrase = dest_phrase
        return dest_phrase
Exemplo n.º 6
0
 def query_dynamic_cards(self, offset, page_size) -> List[FlashCard]:
     # 0=conjugation_id, 1=phrase_id, 2=verb, 3=tense, 4=pronoun, 5=conjugation,  6=phrase, rn
     limits = f""" limit {page_size} offset {offset}"""
     sql = self.sql_statement + limits
     print(f"QUERY: {limits}")
     cur = self.connection.cursor()
     cur.execute(sql)
     query_rows = cur.fetchall()
     response = []
     for row in query_rows:
         flashCard = FlashCard(conjugated=row[5],
                               infinitive=row[2],
                               tense=row[3],
                               source_phrase=row[6],
                               source_lang="en",
                               conjugation_id=row[0],
                               expression_id=row[1],
                               pronoun=row[4])
         response.append(flashCard)
     return response
Exemplo n.º 7
0
    def insert_translation(self, flash_card: FlashCard):
        # see if it's already translated and stored in storage, if not, store it
        if self.storage_manager.check_file_exists(
                flash_card=flash_card,
                base_name=self.storage_manager.flashcard_file):
            self.storage_manager.read_translation(flash_card)
        else:
            flash_card = self.translator.translate(flash_card)
            self.storage_manager.write_translation(flash_card)

        # see if the synthesized vocal is there, otherwise create it
        if self.storage_manager.check_file_exists(
                flash_card=flash_card,
                base_name=self.storage_manager.mp3_file):
            url = self.storage_manager.get_audio_url(flash_card=flash_card)
            flash_card.dest_mp3_url = url
        else:
            self.store_audio(flash_card=flash_card)

        # store the updated flash card in a deck
        self.deck.append(flash_card)
Exemplo n.º 8
0
    def read_translation(self, flash_card: FlashCard) -> str:
        file_name = os.path.join(flash_card.url, self.flashcard_file)
        if not self.check_exists(file_name=file_name):
            return ""

        try:
            object_data = self.s3_client.get_object(Bucket=self.s3_bucket,
                                                    Key=file_name)
            filedata = object_data['Body'].read()
            json_flash = filedata.decode('utf-8')
            flash_dict = json.loads(json_flash)
            id_value = flash_dict['id']
            source_phrase = flash_dict['source_phrase']
            dest_phrase = flash_dict['dest_phrase']
            if flash_card.id != id_value or flash_card.source_phrase != source_phrase:
                return ""
            flash_card.dest_phrase = dest_phrase
            return dest_phrase

        except ClientError as e:
            # AllAccessDisabled error == bucket not found
            # NoSuchKey or InvalidRequest error == (dest bucket/obj == src bucket/obj)
            logging.error(e)
            return ""