diff --git a/.circleci/config.yml b/.circleci/config.yml index 0616ae7..9ce2213 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -19,17 +19,51 @@ jobs: - run: sudo chown -R circleci:circleci /usr/local/lib/python2.7/site-packages - restore_cache: keys: - - v1-dependencies-{{ checksum "requirements.txt" }} + - v2-dependencies-{{ .Branch }}-{{ checksum "requirements.txt" }}-{{ checksum ".circleci/config.yml" }} # fallback to using the latest cache if no exact match is found - - v1-dependencies- + - v2-dependencies- + - run: + name: install cache deb file + command: | + if [[ $(find /home/circleci/apt/archives -name "*.deb") == *".deb"* ]];then + sudo dpkg -i /home/circleci/apt/archives/*.deb + fi + - run: + name: install lsb-release and set CLOUD_SDK_REPO + command: | + sudo apt-get -d install lsb-release + if [[ $(find /var/cache/apt/archives -name "*.deb") == *".deb"* ]];then + sudo dpkg -i /var/cache/apt/archives/*.deb + fi + echo 'export CLOUD_SDK_REPO="cloud-sdk-$(lsb_release -c -s)"' >> $BASH_ENV + - run: + name: install gcloud command and python sdk + command: | + echo "deb http://packages.cloud.google.com/apt $CLOUD_SDK_REPO main" | sudo tee -a /etc/apt/sources.list.d/google-cloud-sdk.list + curl https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add - + sudo apt-get update + sudo apt-get -d install google-cloud-sdk google-cloud-sdk-app-engine-python google-cloud-sdk-app-engine-python-extras + if [[ $(find /var/cache/apt/archives -name "*.deb") == *".deb"* ]];then + sudo dpkg -i /var/cache/apt/archives/*.deb + fi + echo 'export PATH=$PATH:/usr/lib/google-cloud-sdk/platform/google_appengine/bin/' >> $BASH_ENV + echo 'export PYTHONPATH=$PYTHONPATH:/usr/lib/google-cloud-sdk/platform/google_appengine/' >> $BASH_ENV - run: name: vendor our dependencies command: pip install -r requirements.txt + - run: + name: copy deb file to cache directory + command: | + if [[ $(find /var/cache/apt/archives -name "*.deb") == *".deb"* ]];then + mkdir -p /home/circleci/apt/archives/ + cp -r /var/cache/apt/archives/*.deb /home/circleci/apt/archives/ + fi - save_cache: - key: v1-dependencies-{{ checksum "requirements.txt" }} + key: v2-dependencies-{{ .Branch }}-{{ checksum "requirements.txt" }}-{{ checksum ".circleci/config.yml" }} paths: - "/usr/local/bin" - "/usr/local/lib/python2.7/site-packages" + - "/home/circleci/apt/archives/" - run: name: Check PEP 8 for flake8 command: flake8 ./ tests --output-file flake8-test-reports @@ -59,9 +93,9 @@ jobs: - checkout - restore_cache: keys: - - v1-dependencies-{{ checksum "requirements.txt" }} + - v2-dependencies-{{ .Branch }}-{{ checksum "requirements.txt" }}-{{ checksum ".circleci/config.yml" }} # fallback to using the latest cache if no exact match is found - - v1-dependencies- + - v2-dependencies- - run: name: Retrieve our secrets from the CircleCI environment command: echo $CLIENT_SECRET | base64 --decode > ${HOME}/client-secret.json diff --git a/infra.py b/infra.py index 72034cf..ea47eff 100644 --- a/infra.py +++ b/infra.py @@ -18,12 +18,53 @@ from __future__ import division from __future__ import print_function +from google.appengine.ext import ndb + import json with open('data/dict.json') as json_file: json_dic = json.load(json_file) +class User(ndb.Model): + words = ndb.TextProperty() + last_word = ndb.TextProperty() + count = ndb.IntegerProperty() + date = ndb.DateTimeProperty(auto_now_add=True) + + +def reset_datastore(user_id): + try: + user = User.get_by_id(user_id) + user.key.delete() + except Exception: + pass + + +def check_word_datastore(user_id, check_word): + try: + user = User.get_by_id(user_id) + words = user.words.split(u',') + if check_word in words: + return False + return True + except Exception: + return True + + +def save_word_datastore(user_id, save_word): + try: + user = User.get_by_id(user_id) + user.words += u',' + save_word + user.count += 1 + except Exception: + user = User(id=user_id) + user.words = save_word + user.count = 1 + user.last_word = save_word + user.put() + + def search_reading_from_dic(search_word): """json形式の辞書ファイルを全探索し、引数の文字列の読みをカタカナで返す @@ -35,7 +76,7 @@ def search_reading_from_dic(search_word): for word in dict_record[u'org']: if word == search_word: return dict_record[u'key'] - return '' + return u'' def search_word_record_from_dic(search_first): diff --git a/main.py b/main.py index 9922637..36a9d20 100644 --- a/main.py +++ b/main.py @@ -45,8 +45,9 @@ def post(self): # responseId = obj['responseId'] # session = obj['session'] queryResult = obj[u'queryResult'] - # originalDetectIntentRequest = obj['originalDetectIntentRequest'] + originalDetectIntentRequest = obj['originalDetectIntentRequest'] intentDisplayName = queryResult[u'intent'][u'displayName'] + userId = originalDetectIntentRequest[u'payload'][u'user'][u'userId'] self.response.headers['Content-Type'] = 'application/json' if intentDisplayName == GOOGLE_ASSISTANT_WELCOME_INTENT: @@ -73,17 +74,23 @@ def post(self): logging.info(queryText) reading = infra.search_reading_from_dic(queryText) if reading: - logging.info(reading) - # FIXME: 暫定実装 - word_record = infra.search_word_record_from_dic( - reading[-1]) - logging.info(word_record) - word = word_record[u'org'][0] - fulfillmentText = word + u'、の、' + word_record[u'end'] - logging.info(word) - obj = { - u'fulfillmentText': fulfillmentText, - } + if infra.check_word_datastore(userId, reading): + infra.save_word_datastore(userId, reading) + logging.info(reading) + # FIXME: 暫定実装 + word_record = infra.search_word_record_from_dic( + reading[-1]) + logging.info(word_record) + word = word_record[u'org'][0] + fulfillmentText = word + u'、の、' + word_record[u'end'] + logging.info(word) + obj = { + u'fulfillmentText': fulfillmentText, + } + else: + obj = { + u'fulfillmentText': u'それは使用済みの言葉です', + } else: obj = { u'fulfillmentText': u'それは知らない言葉です', diff --git a/requirements.txt b/requirements.txt index af7d1a1..8264a89 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,5 @@ flake8 pylint +pyyaml webtest webapp2 diff --git a/tests/test_main.py b/tests/test_main.py index c8cda76..1fd5133 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -18,6 +18,8 @@ from __future__ import division from __future__ import print_function +from google.appengine.ext import testbed + import json import unittest import webtest @@ -26,6 +28,15 @@ class TestWebApp(unittest.TestCase): + def setUp(self): + self.testbed = testbed.Testbed() + self.testbed.activate() + self.testbed.init_datastore_v3_stub() + self.testbed.init_memcache_stub() + + def tearDown(self): + self.testbed.deactivate() + def test_get(self): app = webtest.TestApp(main.app) @@ -47,6 +58,16 @@ def test_post_google_assistant_welcome_intent(self): }, u'queryText': u'test code', u'languageCode': u'ja', + }, + u'originalDetectIntentRequest': + { + u'payload': + { + u'user': + { + u'userId': u'TestId' + } + } } }, ) @@ -69,6 +90,16 @@ def test_post_ask_continue_intent(self): }, u'queryText': u'test code', u'languageCode': u'ja', + }, + u'originalDetectIntentRequest': + { + u'payload': + { + u'user': + { + u'userId': u'TestId' + } + } } }, ) @@ -91,6 +122,16 @@ def test_post_ask_word_intent_1(self): }, u'queryText': u'ASK_WORD_EVENT', u'languageCode': u'ja', + }, + u'originalDetectIntentRequest': + { + u'payload': + { + u'user': + { + u'userId': u'TestId' + } + } } }, ) @@ -112,6 +153,16 @@ def test_post_ask_word_intent_2(self): }, u'queryText': u'りんご', u'languageCode': u'ja', + }, + u'originalDetectIntentRequest': + { + u'payload': + { + u'user': + { + u'userId': u'TestId' + } + } } }, ) @@ -135,6 +186,16 @@ def test_post_ask_word_intent_3(self): }, u'queryText': u'ほげほげ', u'languageCode': u'ja', + }, + u'originalDetectIntentRequest': + { + u'payload': + { + u'user': + { + u'userId': u'TestId' + } + } } }, )