2016/07/31

Testing Out Google's Natural Language API

Since today is the last day of the Google Natural Language API's free public beta I thought I'd give it a little spin. One of the applications for the API listed on google's promotional page was analyzing product reviews... which reminded me that late last year I made a Slack webhook that extracts Google Play and App Store reviews and happen to still have a stockpile of those laying around in a database of mine, so what better sample data to use for this experiment?

Apple and Google provide rating data (1 to 5 stars) so I know what percentages of the users gave unfavourable reviews, but it would be a good idea to try to narrow down some of the things users are complaining about. Perhaps that's something we can tackle with this API? Let's try it.

My sample data is stored in a database with the following structure:


sqlite> .schema
CREATE TABLE review (
  id INT(11) NOT NULL,
  title VARCHAR(255) NULL,
  content TEXT,
  rating TINYINT(3) NOT NULL DEFAULT 0,
  device_type TINYINT(3) NOT NULL,
  device_name VARCHAR(255) NULL,
  author_name VARCHAR(255) NULL,
  author_uri VARCHAR(255) NULL,
  created DATETIME NULL,
  updated DATETIME NOT NULL,
  acquired DATETIME NOT NULL,
  PRIMARY KEY (id, device_type)
);
CREATE INDEX updated_idx on review(updated);
CREATE INDEX rating_idx on review(rating);
.

At this point I don't really care about platform (although I certainly could further break down my user samples by device type if I was so inclined), so I'm just going to collect the comments from users who gave a distinctly bad rating (of 1 or 2 stars) to feed to the API with a simple query like this:


SELECT content FROM review WHERE rating < 3;
.

If I throw the main body of the review text at Google's API and see if it'll come up with some salient keywords (and how often they are brought up) perhaps it'll give us a better clue what it is the users are complaining about.

So first we need to authenticate with the API.

Creating a service key file for authentication is straight forward enough, so I'll just link to the documentation here and once you have one of those all you need to do is use the gcloud command to authenticate and print an access-token.


$ gcloud auth activate-service-account --key-file=kinmedai-cb03d32572c2.json
Activated service account credentials for: [user@projectname.iam.gserviceaccount.com]
$ gcloud auth print-access-token
[[output omitted]]
.

Now that I'm ready to access the API, I decided to create a script in Go to do the dirty work for me. So the first step is to use the sample json payload and response body data from the getting started docs to generate structs in Go. Writing structs by hand is a pain, so I used JSON to Go to generate the bulk of it and then tweaked it a bit like so:

The request structure:


type EntityRequest struct {
  EncodingType string                `json:"encodingType"`
  Document     EntityRequestDocument `json:"document"`
}

type EntityRequestDocument struct {
  TypeName string `json:"type"`
  Content  string `json:"content"`
  Language string `json:"language"`
}
.

And the response structure:


type EntityResponse struct {
  Entities []DetectedEntity `json:"entities"`
  Language string           `json:"language"`
}

type DetectedEntity struct {
  Name       string          `json:"name"`
  EntityType string          `json:"type"`
  Salience   float64         `json:"salience"`
  Mentions   []EntityMention `json:"mentions"`
  Metadata   struct {
    WikipediaUrl string `json:wikipedia_url"`
  } `json:"metadata"`
}

type EntityMention struct {
  Text struct {
    Content     string `json:"content"`
    BeginOffset string `json:"beginOffset"`
  } `json:"text"`
}
.

Now that I know what kind of data I'll be dealing with I can start building my request.


func createEntityRequests() []*EntityRequest {
  dbh := getDBH()
  rows, err := dbh.Query(`SELECT content FROM review WHERE rating < 3`)
  if err != nil {
    log.Fatal(err)
  }

  var entities []*EntityRequest

  for rows.Next() {
    var comment string
    err = rows.Scan(&comment)
    if err != nil {
      log.Fatal(err)
    }

    // Google Play lets users submit ratings with no comments (stars only ratings) so skip those
    if len(comment) == 0 {
      continue
    }

    entityRequest := &EntityRequest{
      EncodingType: "UTF8",
      Document: EntityRequestDocument{
        TypeName: "PLAIN_TEXT",
        Content:  comment,
        Language: "JA",
      },
    }
    entities = append(entities, entityRequest)
  }

  return entities
}
.

Next I'll need to create a function that posts to the entities analysis API. Again, the quickstart docs summarize this process very clearly, but it's a basic HTTP post request with a json payload and the access token we got from gcloud set in the Authorization header.

I'm planning on passing the token directly from standard input so I can pipe my script with gcloud, but more on that later. First the request:


func postEntity(accessToken string, entityRequest *EntityRequest) []byte {
  jsonEntity, _ := json.Marshal(entityRequest)
  req, err := http.NewRequest("POST", ENTITIES_URL, bytes.NewBuffer(jsonEntity))
  if err != nil {
    log.Fatal(err)
  }
  req.Header.Set("Content-Type", "application/json")
  req.Header.Set("Authorization", "Bearer "+accessToken)

  client := &http.Client{}
  res, err := client.Do(req)
  if err != nil {
    log.Fatal(err)
  }
  defer res.Body.Close()

  body, err := ioutil.ReadAll(res.Body)

  if res.StatusCode != http.StatusOK || err != nil {
    log.Fatal(res.Status)
    log.Fatal(err)
  }

  return body
}
.

Now you might have noticed that this is not the most efficient way to get feedback since I am sending one request per review. At the moment I only have 220 reviews for my test data, so it's not a big deal, but if I was actually planning on using this on any regular kind of basis it could potentially be very expensive (and also slow) to do it this way. Since we don't need to associate any other of the data with the content we could potentially amalgamate several reviews into one body of text and send the data in a batch.

However at this point I'm not even 100% sure this experiment is going to produce any kind of meaningful result, so for the time being I'm going to analyze each review individually. Better to make sure it works before spending time optimizing it, right?

Anyway, let's piece the rest of the script together. I know I'm going to have to pass the script my API access token as well as the path to the db (I'm using sqlite3), so my interface is going to look something like this:


$ gcloud auth print-access-token | go run kinmedai.go -d /path/to/sqlitedbname.db
.

So for my main block I'm going to grab my two parameters via stdin and flags and build the request payloads. Then I'll post each payload to the API and parse any entities from the http response body into the detectedEntities map that'll be used to count how many times a specific term was referenced:


func main() {
  flag.Parse()

  var accessToken string
  fmt.Scan(&accessToken)

  entityRequests := createEntityRequests()
  detectedEntities := make(map[string]int)

  for i := 0; i < len(entityRequests); i++ {
    var entityResponse EntityResponse
    body := postEntity(accessToken, entityRequests[i])
    json.Unmarshal(body, &entityResponse)

    for j := 0; j < len(entityResponse.Entities); j++ {
      entity := entityResponse.Entities[j]
      detectedEntities[entity.Name] = detectedEntities[entity.Name] + 1
    }
  }

  for k, v := range detectedEntities {
    fmt.Printf("%s: %d\n", k, v)
  }
}
.

Yikes! That's a lot of for loops! But as I said I'm still in the experimental phase so I'm not going to worry about how fast this runs just yet.

And the output looks something like this (did I mention I was parsing Japanese reviews?):


Wi-Fi: 2
GOOGLE: 1
TT: 1
DL: 1
2MWXZH4T: 1
アンインストール: 2
ぼく友: 1
ガラポン: 1
2.7.2: 1
GYH7AFSY: 1
某都: 1
Fuck this shit I: 1
掛布: 1
星飛雄馬: 1
ガチャ: 12
ガチャゲー: 1
C8YENNZG: 1
やめた: 3
ゴミゲー: 1
めちゃ運: 1
っ・ω: 1
ゴールド: 1
ガチャ.: 1
間違いない(* ̄ー ̄: 1
WB5Z2JQX: 1
甲子園: 3
平安: 1
パワプロ: 1
.

So it looks like the results are a bit hit and miss. Things like invitation codes, emoji, etc that probably shouldn't be actual keywords are showing up as entities. However it looks like at least 15 (if you count "ガチャ", "ガチャ.", "ガチャゲー" and "ガラポン" as the same result) out of our sample of 220 users are complaining about the gacha system in this particular app.

I can't exactly say this is a groundbreaking discovery or anything. Having read most of the reviews for the app over the last few months (since my webhook delivers new reviews to my team's slack daily) I pretty much knew that many of the complaints hinged around users not getting the drops they wanted, but I guess this helps quantify it a little better?

Either way it looks like Google's entity search is mostly based around pinpointing terms that can be found on Wikipedia... whereas for an app like the one I run it'd be more useful to do a keyword search for game specific terminology if the goal is to pinpoint features the users might be frustrated about.

But now that I've seen what this API is capable of I'm already starting to think of better applications for this technology (like analyzing followers' tweets to see what other games and manga they are talking about).

I skipped over some details (such as connecting to the db and other things that don't really pertain to using the API), but I've uploaded the script to a gist so feel free to reference it in its entirety in case I lost you at any point.

2014/12/25

ペッパー開発体験ワークショップ行ってみた

この間、今年のqiitaのアドベントカレンダーに「Pepper」カレンダーのをみて、「ペッパーってそもそもまだ販売されてないし、買えたとしても滅茶苦茶高いでしょ?」って思って、実際誰がペッパーの開発しているのか見たくてクリックしてみたのですが。なんと秋葉原で無料で開発体験ができるスペースがあること知りました。(今年はもうほぼ終了と思いますが、来年もやるようです)

丁度今仕事休みで暇ですし、無料ですから行ってみました!

ペッパーくんといいツーショット撮ってもらいましたね!


※以下のブログは主に「ペッパー体験ワークショップに興味あるけど、よくわからない」って人向けです。開発方法について特になにも書いてないです。


ワークショップの名前自体に「開発」って単語が入っていますが、実際「基本編」って書いてあるセッションは少なくとも非エンジニアも全然参加できます。Aldebaranさんのソフト(Choreographe)を使って、ドラッグアンドドロップ操作などでペッパーくんのプログラム作れますので、興味ある人はぜひ友達など連れてみてください。
プログラミングがやりたい方はむしろ「上級編」や「ハッカソン」などに参加したほうが良さそうです。

ChoreographeのUIがこういう感じです:

体験スペースではノートパソコンはテーブルごとに用意してあるので、特になにも持っていかなくてもOKなのですが、自分のノートパソコンの持ち込む場合、事前にChoreographeをインストールすることができます。
ダウンロード方法が大変わかりづらいのですが、Aldebaranのコミュニティーサイトでアカウント作ってからこのページに「Software」のタブが現れます。

アカウント作成が面倒な場合、体験スペースにインストーラーが入っているUSBもありますので、早めに行ってUSBからインストールしてもOKだと思います。

公式サイトにワークショップの内容についてあまり書いてないので、ワークショップの内容をざっくり紹介させてください:


【12/15】Pepper開発体験ワークショップ(SDK基本編 #1)

こういったものやりました:

  • ポーズとジェスチャー&timelineを使ってタイミングを合わせる
  • 音声合成機能をつかって喋らせる
  • 発話認識とSwitch Caseを使った反応
  • タッチパネル認識

ダンスのデモも見せてもらいました:


【12/23】Pepper開発体験ワークショップ(SDK基本編 #2)

こういったものやりました:

  • タブレットに画像や動画を表示する
  • Move toやMove around機能で体を動かせる
  • 顔追跡
  • 喋りながらジェスチャーをつける

基本的に時間あまりないので、項目ごとにとても簡単なプログラムを作る余裕しかないですが、まあまあ楽しいです。
最後にペッパーくんにこんなのやってもらいました:

機会あれば、ぜひ皆さんも行ってみてください

2014/12/08

Riot API: 画像のバッチダウンロード

この記事はRiot APIのAdvent Calendar 2014の8日目の記事です。

前回はプロフィールアイコンのurlをCDNから取得するところまでデモしましたが、最新の画像をバッチで一気にダウンロード、使いやすくなるようにファイルをローカルで保存するのをやってみます。


1)チャンピオンのリストを取得

バージョン取得などは前回とほぼ同じので、説明を飛ばします:

#!/usr/bin/env python

import os
import requests
import shutil

DD_URL = 'http://ddragon.leagueoflegends.com'

res = requests.get(DD_URL + '/realms/na.json')
res.raise_for_status()

version = res.json()['n']['champion']

champion_info_url= DD_URL + '/cdn/' + version + '/data/en_US/champion.json'
res = requests.get(champion_info_url)
res.raise_for_status()

data = res.json()['data']
 

ブラウザでチャンピオンjsonデーターを確認するとわかり安いと思いますが、全チャンピオンの名前や基本パラメーターが入っているhash tableを'data'に取得できました。

2)保存先のディレクトリーを用意する

今後ダウンロードをする画像を保存先ディレクトリがあるかどうか確認して、なければつくりましょう。


output_dir = os.path.dirname(os.path.realpath(__file__)) + '/champion'

try:
    os.stat(output_dir)
except:
    os.mkdir(output_dir)
 

3)キャラクターをループで回して、画像を保存しておく

DataDragonではチャンピオンの名前をファイル名として使われています:
http://ddragon.leagueoflegends.com/cdn/4.20.1/img/champion/Aatrox.png

チャンピオンの名前わかれば取得しやすいでしょうけど、たとえば、Riot APIで最近のゲーム履歴(/game/by-summoner/SUMMONER_ID/recent)取得した場合、利用したチャンピオンのkey(ID)しか返ってこないので、名前だけだとかなり不便ですね。
そのため、画像をダウンロードしたら、ファイル名をチャンピオンの名前じゃなくて、チャンピオンのkeyをつけましょう。


champion_img_base = DD_URL + "/cdn/" + version + "/img/champion/"

for name in data:
    img_url = champion_img_base + name + ".png"
    filename = data[name]['key'] + ".png"

    res = requests.get(img_url, stream=True)
    if res.status_code == 200:
        with open(output_dir + '/' + filename, 'wb') as f:
            res.raw.decode_content = True
            shutil.copyfileobj(res.raw, f)
 

以上!
ダウンロードできたかどうかを確認すると:


ls champion/
1.png 106.png 114.png 122.png 14.png 161.png 21.png 25.png 28.png 34.png 40.png 45.png 55.png 61.png 7.png 79.png 85.png 96.png
10.png 107.png 115.png 126.png 143.png 17.png 22.png 254.png 29.png 35.png 41.png 48.png 56.png 62.png 72.png 8.png 86.png 98.png
101.png 11.png 117.png 127.png 15.png 18.png 222.png 26.png 3.png 36.png 412.png 5.png 57.png 63.png 74.png 80.png 89.png 99.png
102.png 110.png 119.png 13.png 150.png 19.png 23.png 266.png 30.png 37.png 42.png 50.png 58.png 64.png 75.png 81.png 9.png
103.png 111.png 12.png 131.png 154.png 2.png 236.png 267.png 31.png 38.png 429.png 51.png 59.png 67.png 76.png 82.png 90.png
104.png 112.png 120.png 133.png 157.png 20.png 238.png 268.png 32.png 39.png 43.png 53.png 6.png 68.png 77.png 83.png 91.png
105.png 113.png 121.png 134.png 16.png 201.png 24.png 27.png 33.png 4.png 44.png 54.png 60.png 69.png 78.png 84.png 92.png
  

全部揃ってありますね!これで新しいチャンピオンが追加された場合でもスクリプトを動かすだけですぐに画像をダウンロードできそうですね。

2014/12/06

Riot API: プロフィールアイコンの取得

この記事はRiot APIのAdvent Calendarの6日目の投稿です。

Riot APIで取得できるJSONデータをみるとなんかワクワクして、LOLKingなどMobafireみたいなかっこいいサイト作りたくなりますよね?
でももちろん、ウェブサイトを作った場合に、最新のアイコンなどの画像も必要になりますので、今回はPythonをつかって自分のプロフィールアイコンのurlを取得するところまでデモしたいと思います。

Riot Gamesのアセット・リポジトリはData Dragonというサービスに通じてアクセスできます。
Riot APIと別のチームが管理しているらしいので、アップデート直後にData Dragonへの反映が遅れたり、 データーが一致しない可能性もありますが、それでも最新の画像の取得には一番便利なツールになります。

ではプロフィールアイコンの取得をやりましょう

1) バージョン取得

画像データーはバッチでアップロードされていて、一番最初にダウンロードしたい画像のバージョンを取得する必要があります。 自分のアカウントはNorth Americaサーバーにありますので、naのjsonフィードを使います。

# datadragon.py
import requests    # pip install requests

DD_URL = 'http://ddragon.leagueoflegends.com'

version_path = '/realms/na.json'
res = requests.get(DD_URL + version_path)
res.raise_for_status()      # catch non 2xx status

print(res.text)
  

プリントの結果はこんな感じです:


{"n":{"item":"4.20.1","rune":"4.17.1","mastery":"4.17.1","summoner":"4.20.1","champion":"4.20.1",            "profileicon":"4.20.1","language":"4.20.1"},"v":"4.20.1","l":"en_US","cdn":"http:\/\/ddragon.leagueoflegends.  com\/cdn","dd":"4.17.1","lg":"0.152.55","css":"0.152.55","profileiconmax":28,"store":null}
  

今回はプロフィールアイコンのバージョンが取得したいと思いますので、こういうふうにprofileiconのバージョン番号だけ取得しましょう:


version = res.json()['n']['profileicon']
  

2) 自分のプロフィールアイコンIDを取得

次に自分のプロフィールアイコンIDを取得します。今回はデータードラゴンじゃなくて、通常のRiotAPIを使いましょう。


API_URL = 'https://na.api.pvp.net/api/lol/na'
summoner_by_name_path = "/summoner/by-name/laouji";
profile_url = API_URL + "/v1.4" + summoner_by_name_path + "?api_key=" + API_KEY

res = requests.get(profile_url)
res.raise_for_status()
 

res.textの中身を確認すると:


"laouji":{"id":46048341,"name":"laouji","profileIconId":607,"summonerLevel":23,"revisionDate":1411808085000}}
 

3)画像のURLを組み合わせる

APIコールの結果に必要なprofileIconIdありましたので、画像のurlを組み合わせるのが簡単です:


def icon_url ( version, icon_id ):
    url = DD_URL + '/cdn/' + version + '/img/profileicon/' + str(icon_id) + '.png'
    return url

icon_id = res.json()['laouji']['profileIconId']
icon_url = icon_url(version, icon_id)
 

自分の場合はこれでした:

2014/08/24

OpenRestyとLapisでSupervisorctlを実行できるシンプルなウェブアプリ作成

周りのマークアップエンジニアがgit使いこなしていて、平気でブランチを切り替えたりして開発サーバー上で作業して貰っています。ただし、ブランチ切り変えになると、たまにアプリリスタートも必要で、マークアップじゃ一人で作業したいブランチを開発サーバーに反映できない場合があります。

Plack::Loader::Shotgunを使ってアプリさえ実行すれば、こういった問題をよりやすく解決できる気もしますが、一応、Nginx OpenRestyを使ってみたかったので、試しにウェブインタフェースを使ってプロセスをリスタートできるアプリを作っちゃいました。

*OpenRestyについてはOpenRestyの公式ページを参考にしてください

**フレームワークはLapisというOpenResty上で動くLuaのウェブフレームワークです。おしゃんてぃでおすすめです。

最近process管理のため主にSupervisordを使っています。周りの人がそれをrootを使って実行しガチなんですが、やっぱり開発環境といってもウェブのユーザがrootのプロセスが実行できたら怖いですし、nginx自体も普段nobodyユーザによって実行されるので、まずSupervisordをnobodyユーザとして実行しました。 そこでnobodyがアクセスできるディレクトリを作って、以下のlapis app.luaを作成しました:
--app.lua
local lapis = require("lapis")
local app_helpers = require("lapis.application")
local validate = require("lapis.validate")
local cjson = require("cjson")

local capture_errors = app_helpers.capture_errors

local app = lapis.Application()
app:enable("etlua")
app.layout = false

validate.validate_functions.alphanumeric = function(input)
     return string.match(input, "^[%w_%-]+$"), "must be alphanumeric"
end

-- たたいたコマンドのSTDOUTパージング
function app:parse_status(line)
    local parts = {}
    for word in line:gmatch("%S+") do table.insert(parts, word) end

    local status = {
        ["name"] = parts[1],
        ["status"] = parts[2],
    }
    if status["status"] == 'RUNNING' then
        status["pid"] = string.gsub(parts[4], ",", "")
        status["uptime"] = parts[#parts]
    elseif status["status"] == 'STOPPED' then
        status["uptime"] = string.format("%s %s %s", parts[3], parts[4], parts[5])
    end

    return status
end

-- トップページにstatusの結果をテーブルで表示したいので、結果をselfにいれるとテンプレートで使えるようになる
app:get("/", function(self)
    local handle = io.popen("/usr/bin/supervisorctl status" .. " 2>&1")

    self.supervisor_status = {}
    for line in handle:lines() do
        table.insert(
            self.supervisor_status,
            self.app:parse_status(line)
        )
    end

    handle:close()

    return { render = 'index' }
end)

-- 最低限のvalidationとリスタートをかける処理
app:post("/:app_name/restart/", capture_errors(function(self)
    validate.assert_valid(self.params, {
        { "app_name", exists = true, alphanumeric = true }
    })

    local app_name = self.params.app_name
    local handle = io.popen("/usr/bin/supervisorctl restart " .. app_name .. " 2>&1")

    self.message = {}
    for line in handle:lines() do
        table.insert(self.message, line)
    end

    return cjson.encode(self.message)
end))

return app
.

出来上がったものはこんな感じ:

2014/07/27

Amazon SNSを使ってSESメールのホワイトリスト管理

ご無沙汰です。

メールマガジンって色々大変ですよね?私がその大変さを実感したのが最近ばかりのことですが、今日はAmazonのSimple Notification Serviceを使ってホワイトリストの管理を簡単にできる方法を紹介させてもらいたいと思います。

SESを使ってメールを送信した時に、ユーザのメールアドレスが存在しなかったため送信完了できなかった場合(Bounce)や、ユーザが「迷惑メール」のボタンを押した(Complaint)場合、Amazonからエラーの詳細が書いてあるメールを送ってもらうのがデフォルトの設定かと思いますが、それ以外にもSNSの通信を送ってもらうこともできます。

そしてSNSにはHTTPSのインタフェースもあるので、簡単なAPIを立ち上げて、BounceとComplaintを自動的にブラックリスト化をすることが意外と簡単です。

SNS Topicの作成の仕方や送信先(endpoint)の認証の仕方が丁寧にドキュメントに書いてありますが、BounceとComplaintの場合どんなメッセージが書いてくるかというと、こんな感じです:

POST / HTTP/1.1
x-amz-sns-message-type: Notification
x-amz-sns-message-id: 22b80b92-fdea-4c2c-8f9d-bdfb0c7bf324
x-amz-sns-topic-arn: arn:aws:sns:us-east-1:123456789012:MyTopic
x-amz-sns-subscription-arn: arn:aws:sns:us-east-1:123456789012:MyTopic:c9135db0-26c4-47ec-8998-413945fb5a96
Content-Length: 773
Content-Type: text/plain; charset=UTF-8
Host: example.com
Connection: Keep-Alive
User-Agent: Amazon Simple Notification Service Agent

{
  "Type" : "Notification",
  "MessageId" : "22b80b92-fdea-4c2c-8f9d-bdfb0c7bf324",
  "TopicArn" : "arn:aws:sns:us-east-1:123456789012:MyTopic",
  "Message" : "{\\"notificationType\\":\\"Bounce\\",\\"bounce\\":{\\"bounceSubType\\":\\"General\\",\\"bounceType\\":\\"Permanent\\",\\"reportingMTA\\":\\"dsn; a8-41.smtp-out.amazonses.com\\",   \\"bouncedRecipients\\":[{\\"status\\":\\"5.1.1\\",\\"action\\":\\"failed\\",\\"diagnosticCode\\":\\"smtp; 554 5.1.1 <recipient@example.com>: Recipient address rejected: User unknown\\",      \\"emailAddress\\":\\"recipient@example.com\\"}],\\"timestamp\\":\\"2014-07-27T09:39:22.070Z\\",\\"feedbackId\\":\\"00000147773054e9-96ae8a22-c833-4967-874c-d56accc9fd2d-000000\\"},\\"mail\\":{\\"timestamp\\":\\"2014-07-27T09:39:18.000Z\\",\\"source\\":\\"noreply@example.come\\",\\"messageId\\":\\"0000014777304606-b5a16bd1-5a20-40ef-993b-0395f14de101-000000\\",\\"destination\\":         [\\"recipient@example.com\\"]}}",
  "Timestamp" : "2012-05-02T00:54:06.655Z",
  "SignatureVersion" : "1",
  "Signature" : "EXAMPLEw6JRNwm1LFQL4ICB0bnXrdB8ClRMTQFGBqwLpGbM78tJ4etTwC5zU7O3tS6tGpey3ejedNdOJ+1fkIp9F2/LmNVKb5aFlYq+9rk9ZiPph5YlLmWsDcyC5T+Sy9/umic5S0UQc2PEtgdpVBahwNOdMW4JPwk0kAJJztnc=",
  "SigningCertURL" : "https://sns.us-east-1.amazonaws.com/SimpleNotificationService-f3ecfb7224c7233fe7bb5f59f96de52f.pem",
  "UnsubscribeURL" : "https://sns.us-east-1.amazonaws.com/?Action=Unsubscribe&SubscriptionArn=arn:aws:sns:us-east-1:123456789012:MyTopic:c9135db0-26c4-47ec-8998-413945fb5a96"
  }

リクエストbodyのJSONなのですが、JSONなのにContent-Typeが「text/plain」のでご注意ください。そして、今回の話題のブラックリストに入れたいメールアドレスがMessageの項目に入っています。エスケープされているJSONなので、これもデコード必要ですね。

私はPlack::Requestからrawのリクエストbodyを取得して、デコードしてみました。


if ($request->headers->header('x-amz-sns-message-type') eq "Notification") {
        my $raw_body = $request->raw_body;
        $raw_body =~ s/\\\\/\\/g;

        my $decoder = JSON::XS->new->utf8;
        my $json = $decoder->decode($raw_body) or die "could not decode json";
        my $message = $decoder->decode($json->{Message}) or die "could not decode json";

        my @addresses;
        if ($message->{notificationType} eq "Bounce") {
            my $bounced_recipients = $message->{bounce}->{bouncedRecipients};
            @addresses = map { $_->{emailAddress} } @$bounced_recipients;

        } elsif ($message->{notificationType} eq "Complaint") {
            my $complaining_recipients = $message->{complaint}->{complainedRecipients};
            @addresses = map { $_->{emailAddress} } @$complaining_recipients;

        }

        for my $address (@addresses) {
            #対象メールアドレスをブラックリストに追加
        }
}


意外と簡単でした!これでばっちり!

2013/07/11

VimからCorona SDKのシミュレーターを起動する方法

最近アプリ作りたくて、フレームワーク色々いじってきたところです。JavaやC++とかを使うとちゃんとしたIDEがないと困るかと思いますが、Eclipseって使うとなんかだるいですよね。。。 まぁ、とにかく気軽にスクリプト言語でかけるフレームワークを探しているところCorona SDKを見つけました。Lua全くわかんないというのがありましたが、どっちかというとC++を習うほど大変じゃないですし、なんとvimでの開発環境を構築するのが簡単すぎて、ほぼ一目惚れでした。

SDKをインストールができたら、以下をvimrcに張っておくぐらいで十分:


map  :!/Applications/CoronaSDK/Corona\ Terminal -project %:p -skin iPhone<CR>


*Mac上のCorona Terminalのデファルトパスの場合です

main.luaがvimで開いた時、を押すことによってコロナのiPhoneシミュレーターを起動できるので、とても便利です。