Skip to content

Commit

Permalink
Add save_words_task
Browse files Browse the repository at this point in the history
  • Loading branch information
kimdoori committed Jun 14, 2020
1 parent fe82edf commit 0d0874a
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions word_way/scrapping/word.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,30 @@
from word_way.models import Pronunciation, Sentence, Word, WordSentenceAssoc
from word_way.utils import convert_word_part

__all__ = 'save_word', 'save_word_task',
__all__ = 'save_word', 'save_word_task', 'save_words_task',


@celery.task
def save_word_task(target_word: str):
save_word(target_word, session)


@celery.task
def save_words_task():
"""단어가 없는 발음을 가져와서 단어를 저장하는 테스크"""
subquery = session.query(Word).filter(
Word.pronunciation_id == Pronunciation.id
)
pronunciations = session.query(Pronunciation).filter(
~subquery.exists()
).all()
for pronunciation in pronunciations:
pronunciation_id = save_word(pronunciation.pronunciation, session)
if pronunciation.id != pronunciation_id:
session.delete(pronunciation)
session.commit()


def save_word(
target_word: str, session: Session,
) -> typing.Optional[uuid.UUID]:
Expand All @@ -33,7 +49,7 @@ def save_word(
:type target_word: :class:`str`
:param session: 사용할 세션
:type session: :class:`sqlalchemy.orm.session.Session`
:return: target_word와 발음이 정확히 일치하는 발음 ID
:return: target_word와 발음이 정확히 일치하는 발음 ID, 없다면 None을 반환합니다
:rtype: typing.Optional[uuid.UUID]
"""
Expand Down

0 comments on commit 0d0874a

Please sign in to comment.