This might be a really dumb or simple question or might better suited to ask in the IRC channel, but I can't seem to find an answer to this anywhere on google. I've been trying to test various bits of code to see how it runs on the web2py framework but without tying it to a specific view. Does anyone know how to run a script / function in a module based off of events such as a button press or a input command to the web shell?
I made a simple function in a module which, provided a valid API Key, Verification Code and Character ID, parses and stores a list of mail messages pulled from the
MailMessage function made available by the EVE Online API Server. There is also a function which will do the same as before but for every valid API Key, Verification Code and Character ID stored in the database all at once. I'm working on a means to properly handle the other
API calls available and would like to run those on a semi-regular basis, but haven't currently achieved a working result yet. I intended to invoke these method when a user logs into the site, as well as roughly once every twelve hours, to keep a relatively up-to-date database filled with mails for EVE Online.
All relevant ID columns for things like Character ID, Message ID, etc... from the API server are unique so a quick IS_NOT_IN_DB and a unique key constraint should stop any and all duplicates. I guess later I could log attempts to enter a duplicate value, but right now it's not a concern.
From my understanding, any code in the Model or Controller folders will be executed each time the page is viewed. This works well for a lot of things, but doesn't seem optimal for relatively slow net and database code. The Scheduler seems like it could be useful for this, but that doesn't seem like it should be placed in a model or a controller file.
Here is what I've used so far to read messages and pull them to a database, it's pretty crude but I just wanted a working example to build off of later.
# Put your userID and apiKey (full access) here before running this script.
YOUR_KEYID = [redacted]
YOUR_VCODE = "[redacted]"
YOUR_CHARACTERID=[redacted]
from os.path import join, exists
from httplib import HTTPException
from gluon.sql import DAL, Field
from gluon.validators import *
import logging
import eveapi
import datetime
api = eveapi.EVEAPIConnection()
db = DAL('sqlite://test3.sqlite')
#----------------------------------------------------------------------------
message = db.define_table('message',
Field('messageID','integer', unique=True),
Field('header','string'),Field('body','string'),
Field('dateTimeSent','datetime'))
# /account/Characters.xml.aspx
getMailHeader = api("/char/MailMessages", keyID=YOUR_KEYID, vCode=YOUR_VCODE, characterID=YOUR_CHARACTERID)
listOfMessageID = []
listOfHeaders = []
result = getMailHeader.messages.SortedBy("sentDate")
for s in result:
try:
db.message.insert(messageID=s.get("messageID"), header=s.get("title"), dateTimeSent = datetime.datetime.utcfromtimestamp( s.get("sentDate") ) , body="")
listOfMessageID.append(s.get("messageID"))
except Exception as e:
logging.warning("Message with ID of %s already exists in database", s.get("messageID"))
if len(listOfMessageID) > 0:
getMailBody = api("/char/MailBodies", keyID=YOUR_KEYID, vCode=YOUR_VCODE, characterID=YOUR_CHARACTERID, ids = ','.join([ str(i) for i in listOfMessageID]) )
MailMessages = getMailBody.messages.SortedBy("messageID")
for s in MailMessages:
mailID = s.get("messageID")
mailBody = s.get("data").encode("utf-8")
db( db[message].messageID==mailID).update(**{"body":mailBody})
#print s.__str__().encode("utf-8")
#print
for row in db(message).select():
print row
print
db.commit()