[blipit] r158 committed - Communicating with Blipit SVC on a background job

0 views
Skip to first unread message

bli...@googlecode.com

unread,
Jan 16, 2011, 2:54:21 AM1/16/11
to bli...@googlegroups.com
Revision: 158
Author: dipt...@gmail.com
Date: Sat Jan 15 23:53:11 2011
Log: Communicating with Blipit SVC on a background job
http://code.google.com/p/blipit/source/detail?r=158

Added:
/trunk/blipitsms/geocoder/app.py
Modified:
/trunk/blipitsms/geocoder/blip.py
/trunk/blipitsms/geocoder/handlers/geocoder.py
/trunk/blipitsms/geocoder/tasks.py
/trunk/blipitsms/settings.py

=======================================
--- /dev/null
+++ /trunk/blipitsms/geocoder/app.py Sat Jan 15 23:53:11 2011
@@ -0,0 +1,24 @@
+from rapidsms.apps.base import AppBase
+from models import Channel
+from geocoder import tasks
+from geocoder import blip
+
+class App(AppBase):
+
+ def parse(self, msg):
+ msg_txt = (msg.text).strip().split(" ",1)
+ print msg_txt
+ channels = Channel.objects.filter(channel_name = msg_txt[0])
+ print channels
+ channel_list = []
+ for channel in channels:
+ channel_list.append(blip.Channel(channel.channel_id))
+
+ print channel_list
+ if len(channel_list) is not 0:
+ tasks.send.delay(msg.connection.identity, msg_txt[1],
channel_list, msg)
+ msg.respond("We are processing your request")
+ return
+
+ msg.respond("Sorry, we do not have a channel called %s" %
(msg_txt[0],))
+
=======================================
--- /trunk/blipitsms/geocoder/blip.py Fri Jan 14 02:05:54 2011
+++ /trunk/blipitsms/geocoder/blip.py Sat Jan 15 23:53:11 2011
@@ -29,7 +29,6 @@

def post(self):
json_data = json.dumps(self.blip)
- print json_data
request = urllib2.Request(self.url, json_data)
response = urllib2.urlopen(request)
return
=======================================
--- /trunk/blipitsms/geocoder/handlers/geocoder.py Fri Jan 14 02:05:54 2011
+++ /trunk/blipitsms/geocoder/handlers/geocoder.py Sat Jan 15 23:53:11 2011
@@ -1,40 +1,31 @@
-#!/usr/bin/env python
-# vim: ai ts=4 sts=4 et sw=4
-
-
from rapidsms.contrib.handlers.handlers.keyword import KeywordHandler
import urllib2, urllib
import json
from blipitsms.geocoder.blip import Channel, GeoPoint, Blip, BlipItPost

class GeoCoderHandler(KeywordHandler):
- """
- Handle any message prefixed ``echo``, responding with the remainder
- of the text. Useful for remotely testing internationalization.
- <channel-name> <addr>
- """
-
keyword = "panic|fire|PANIC|FIRE|"

def help(self):
self.respond("To echo some text, send: PANIC <ANYTHING>")

def handle(self, text):
- url
= "http://maps.googleapis.com/maps/api/geocode/json?sensor=true&address=" +
urllib.quote_plus(text)
- result = urllib2.urlopen(url)
- json_data = json.loads(result.read())
- if json_data['status'] == u'OK':
- latitude =
json_data['results'][0]['geometry']['location']['lat']
- longitude =
json_data['results'][0]['geometry']['location']['lng']
- self.respond("lat is : %f and longitude is : %f" %
(latitude,longitude))
- creatorId = self.msg.connection.identity
- c1 = Channel(3001)
- c2 = Channel(4001)
- g1 = GeoPoint(latitude, longitude)
- b = Blip([c1,c2], "PANIC sms", g1, "PANIC sms", creatorId)
- blipitPost = BlipItPost(b)
- blipitPost.post()
- return
-
- self.respond("Sorry we do not understand your address!")
-
+ pass
+ # url
= "http://maps.googleapis.com/maps/api/geocode/json?sensor=true&address=" +
urllib.quote_plus(text)
+ # result = urllib2.urlopen(url)
+ # json_data = json.loads(result.read())
+ # if json_data['status'] == u'OK':
+ # latitude =
json_data['results'][0]['geometry']['location']['lat']
+ # longitude =
json_data['results'][0]['geometry']['location']['lng']
+ # self.respond("lat is : %f and longitude is : %f" %
(latitude,longitude))
+ # creatorId = self.msg.connection.identity
+ # c1 = Channel(3001)
+ # c2 = Channel(4001)
+ # g1 = GeoPoint(latitude, longitude)
+ # b = Blip([c1,c2], "PANIC sms", g1, "PANIC sms",
creatorId)
+ # blipitPost = BlipItPost(b)
+ # blipitPost.post()
+ # return
+
+ # self.respond("Sorry we do not understand your address!")
+
=======================================
--- /trunk/blipitsms/geocoder/tasks.py Fri Jan 14 09:51:25 2011
+++ /trunk/blipitsms/geocoder/tasks.py Sat Jan 15 23:53:11 2011
@@ -1,16 +1,37 @@
from celery.decorators import task
-import urllib2, json
+import urllib2, urllib, json
from models import Channel
+from django.conf import settings
+
+from blip import Blip, BlipItPost, GeoPoint

@task
def scan_channels():
- url = "http://blipitsvc.appspot.com/blipit/panic/channel"
- req = urllib2.urlopen(url)
+ req = urllib2.urlopen(settings.BLIPIT_SVC_URL)
json_data = json.loads(req.read())
for channel in json_data:
channel_id = channel['key']['id']
channel_name = channel['name']
if len(Channel.objects.filter(channel_name = channel_name)) is 0:
Channel(channel_id = channel_id, channel_name =
channel_name).save()
-
-
+
+
+@task
+def send(phone_number, address, channel_list, msg):
+ url = settings.GEOCODER_SVC_URL + urllib.quote_plus(address)
+ url_request = urllib2.urlopen(url)
+ response = url_request.read()
+ geocoder_result = json.loads(response)
+ print msg
+ if geocoder_result['status'] == u'OK':
+ latitude =
geocoder_result['results'][0]['geometry']['location']['lat']
+ longitude =
geocoder_result['results'][0]['geometry']['location']['lng']
+ blip = Blip(channel_list, "PANIC SMS", GeoPoint(latitude,
longitude), "PANIC SMS", phone_number)
+ blipItPost = BlipItPost(blip)
+ blipItPost.post()
+ msg.respond("We have added your request to our queue")
+ return
+
+ msg.respond("Sorry the Geo-Coding service has failed.")
+
+
=======================================
--- /trunk/blipitsms/settings.py Fri Jan 14 09:51:25 2011
+++ /trunk/blipitsms/settings.py Sat Jan 15 23:53:11 2011
@@ -40,7 +40,6 @@
"ENGINE": "rapidsms.backends.bucket",
}
}
-

# to help you get started quickly, many django/rapidsms apps are enabled
# by default. you may wish to remove some and/or add your own.
@@ -96,6 +95,9 @@
},
}

+BLIPIT_SVC_URL = "http://blipitsvc.appspot.com/blipit/panic/channel"
+
+GEOCODER_SVC_URL
= "http://maps.googleapis.com/maps/api/geocode/json?sensor=true&address="

# this rapidsms-specific setting defines which views are linked by the
# tabbed navigation. when adding an app to INSTALLED_APPS, you may wish

Reply all
Reply to author
Forward
0 new messages