There won't be to many comments on the steps involved.
Starting with the Web2py
~db.py~
Code: Select all
try:
import json
except ImportError:
from gluon.contrib import simplejson as json
Code: Select all
class RESIZE(object):
def __init__(self,nx=160,ny=80,error_message='Problem'):
(self.nx,self.ny,self.error_message)=(nx,ny,error_message)
def __call__(self,value):
if isinstance(value, str) and len(value)==0:
return (value,None)
from PIL import Image
import cStringIO
try:
img = Image.open(value.file)
img.thumbnail((self.nx,self.ny), Image.ANTIALIAS)
s = cStringIO.StringIO()
img.save(s, img.format, quality=100)
s.seek(0)
value.file = s
except:
return (value, self.error_message)
else:
return (value, None)
def THUMB(image, nx=320, ny=60):
from PIL import Image
import os
try:
img = Image.open(request.folder + 'static/ads/' + image)
img.thumbnail((nx,ny), Image.ANTIALIAS)
root,ext = os.path.splitext(image)
thumb='%s_thumb%s' %(root, ext)
img.save(request.folder + 'static/ads/' + thumb)
return thumb
except:
pass
db.define_table('ads',
Field('website', requires=IS_EMPTY_OR(IS_URL())),
Field('phone'),
Field('localAd', 'upload', uploadfolder=request.folder+'static/ads', label='Business Ad', autodelete=True, requires=IS_EMPTY_OR([IS_IMAGE(extensions=('png', 'gif', 'jpg', 'jpeg')), RESIZE(320, 60)])),
Field('localAd_thumb', 'upload', compute=lambda r: THUMB(r['localAd'])))
default.py
Code: Select all
@service.json
def ads():
rows = db(db.ads.id > 0).select(db.ads.website, db.ads.phone, db.ads.localAd, limitby=(0, 1), orderby='<random>')
return rows
Moving on to Corona SDK!
Create a new main.lua file or add the code below to an existing project
Code: Select all
local widget = require( "widget" )
local json = require("json")
local http = require("socket.http")
local function adNetwork()
local downloadURL = "http://www.yourdomain.com/missedmybus/static/ads"
local URL = "http://www.yourdomain.com/missedmybus/default/call/json/ads"
local response = http.request(URL)
if response == nil then do
end
else
local data = json.decode(response)
for i in pairs( data ) do
adValue = data[i].localAd
webValue = data[i].website
phoneValue = data[i].phone
end
end
local url = downloadURL .. "/" .. tostring(adValue)
local function showNetworkImage(event)
myTarget = event.target
if ( event.isError ) then do
end
else
function myTarget:touch( e )
if e.phase == "began" then
system.openURL( tostring(webValue) )
return true
end
end
myTarget:addEventListener( "touch", myTarget )
end
end
display.loadRemoteImage(url, "GET", showNetworkImage, adValue, system.TemporaryDirectory, display.contentCenterX, display.contentCenterY + 200)
end
You would then need to call the function in your code
Code: Select all
adNetwork()
*cheers
Mabo.