2011年8月15日月曜日

プリモバイルでみまもりケータイからの位置情報付きメールを地図表示する

プリモバイルではインターネットに接続できない。
みまもりケータイから送られて来た位置情報付きメールのURLを開くことは出来ない。
プリモバイルのメールし放題でGAEに転送して、代わりに地図の画像を取得して返信してもらう。
地図の画像の取得にはYahoo!デベロッパーネットワークのスタティックマップAPI、住所取得にはリバースジオコーダAPIを使っています。

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# mimamori_simplemap.py 2011.8.3
import logging
import os
import datetime
import re
import urllib
from django.utils import simplejson
from google.appengine.api import app_identity
from google.appengine.api import urlfetch
from google.appengine.api import mail
from google.appengine.ext import webapp
from google.appengine.ext.webapp.mail_handlers import InboundMailHandler
from google.appengine.ext.webapp.util import run_wsgi_app
import yahoo_dev

def getgeo(text):
    pat = r'N(?P\d+)\.(?P\d+)\.(?P\d+\.\d+)E(?P\d+)\.(?P\d+)\.(?P\d+\.\d+)'
    m = re.search(pat, text)
    if m:
        lat = float(m.group('d1')) + float(m.group('m1')) / 60 + float(m.group('s1')) / 3600
        lon = float(m.group('d2')) + float(m.group('m2')) / 60 + float(m.group('s2')) / 3600
        return (lat, lon)
    else:
        return None

def yahoo_static_map(geo):
    api_url = "http://map.olp.yahooapis.jp/OpenLocalPlatform/V1/static"
    params = {
        'appid': yahoo_dev.my_appid,
        'pin': str(geo[0]) + "," + str(geo[1]),
        'width': "480",
        'height': "480",
        'z': "19",
    }
    query = urllib.urlencode(params)
    result = urlfetch.fetch(api_url + '?' + query)
    if result.status_code == 200:
        return result.content
    else:
        return None

def yahoo_revgeo(geo):
    api_url = "http://reverse.search.olp.yahooapis.jp/OpenLocalPlatform/V1/reverseGeoCoder"
    params = {
        'appid': yahoo_dev.my_appid,
        'lat': str(geo[0]),
        'lon': str(geo[1]),
        'output': 'json',
    }
    query = urllib.urlencode(params)
    result = urlfetch.fetch(api_url + '?' + query)
    if result.status_code == 200:
        json_str = result.content
        obj = simplejson.loads(json_str)
        address = obj['Feature'][0]['Property']['Address']
        logging.info(u"位置情報:" + address)
        return address
    else:
        return u"位置情報は取得できませんでした."

class simplemap_email(InboundMailHandler):
    def receive(self, mail_message):
        path = self.request.path
        logging.info("path: " + path)
        logging.info("Received a message from: " + mail_message.sender)

        body_text = image_data = ""
        plaintext = mail_message.bodies(content_type='text/plain')
        for text in plaintext:
            txtmsg = text[1].decode()
            logging.info("Body is %s" % txtmsg)
            geo =getgeo(txtmsg)
            logging.info(geo)
            if geo:
                body_text = yahoo_revgeo(geo)
                image_data = yahoo_static_map(geo)
                break

        message = mail.EmailMessage()
        message.sender = "simplemap@" + app_identity.get_application_id() + ".appspotmail.com"
        message.to = mail_message.sender
        message.subject = u"位置情報 " + mail_message.subject
        message.body = u"現在地:\n" + body_text
        if image_data:
            message.attachments = [("map.png", image_data)]
        message.send()

application = webapp.WSGIApplication([
    simplemap_email.mapping(),
], debug=True)

def main():
    run_wsgi_app(application)

if __name__ == "__main__":
    main()

---

0 件のコメント: