def post(self, psychicGuid, method):
        # Always validate the token first
        psychicGuid_token = validate_token(request)

        if psychicGuid_token != psychicGuid:
            raise Unauthorized

        values = request.json

        # Routing
        if method is not None:
            if method == 'profile':
                return self.postPsychicProfile(psychicGuid, values)
            if method == 'active':
                return self.postPsychicState(psychicGuid, values)
    def get(self, psychicGuid, method):
        # Always validate the token first
        tokenPsychicGuid = validate_token(request)

        # Check that the token cooresponds to the psychic being asked about
        if tokenPsychicGuid != psychicGuid:
            raise Unauthorized

        # Routing
        if method is not None:
            if method == 'rating':
                return self.getPsychicRating(psychicGuid)
            if method == 'allFortunes':
                return self.getPsychicAllFortunes(psychicGuid)
            else:
                return 'Invalid method'
        else:
            return self.getPsychic(psychicGuid)
def postFortune():
    # Always validate the token first
    psychicGuid = validate_token(request)

    values = request.json
    print(values)

    # Validate that all the required fields are present
    if 'message' not in values or values['message'] is None:
        return 'Missing message'
    if 'service' not in values or values['service'] is None:
        return 'Missing service'

    guid = str(uuid())

    db.query(
        'INSERT INTO fortune (guid, message, psychicGuid, service) VALUES (%s, %s, %s, %s)',
        (guid, values['message'], psychicGuid, values['service'])
    )

    return 'Fortune created'
    def post(self, fortuneGuid, method):
        # Always validate the token first
        psychicGuid = validate_token(request)

        # Check the fortune being requested belongs to this psychic
        if fortuneGuid is not None and not validate_token_fortune_ownership(request, fortuneGuid):
            raise Unauthorized

        values = request.json

        # Routing
        if method is not None:
            if method == 'message':
                return self.postFortuneMessage(fortuneGuid, values)
            if method == 'status':
                return self.postFortuneStatus(fortuneGuid, values)
            if method == 'delete':
                return self.postFortuneDelete(fortuneGuid)
            if method == 'network':
                return self.postFortuneNetwork(fortuneGuid, values)
        else:
            return self.postFortune(fortuneGuid, values)
    def get(self, fortuneGuid, method):
        # Always validate the token first
        psychicGuid = validate_token(request)

        # Check the fortune being requested belongs to this psychic
        if fortuneGuid is not None and not validate_token_fortune_ownership(request, fortuneGuid):
            raise Unauthorized

        # Routing
        if method is not None:
            if method == 'message':
                return self.getFortuneMessage(fortuneGuid)
            elif method == 'status':
                return self.getFortuneStatus(fortuneGuid)
            elif method == 'applied':
                return self.getFortuneAppliedTo(fortuneGuid)
            elif method == 'statistics':
                return self.getFortuneStatistics(fortuneGuid)
            else:
                return 'Invalid method'
        else:
            return self.getFortune(fortuneGuid)
def main(cnf=None):
    config.load_config(cnf)

    print "Responding to emails"

    # Connect and grab the 10 latest emails
    messages = get_latest_messages()

    # Iterate and process
    for message in messages:
        print "----"

        to = message['Delivered-to']
        frm = message['From']
        email = raw_email(frm)

        if not email_allowed(email):
            print "{} is not an allowed address".format(email)
            continue

        if not '+' in to:
            print "Generating schedule for {}".format(email)
            generate_schedule(email)
            continue

        # We need to extract the token from the email address (first+TOKEN@....)
        # and ensure it matches the email address we received.
        token = token_from_email(to)

        print "Looking for token - {}".format(token)
        success, record = validate_token(token, frm)
        if not success:
            # Notify user of failure?
            print "FAILED to find a record for the token"
            continue

        dataset = record['dataset']
        date = record.get('date')

        print "Looking for URL to add to {}".format(dataset)
        process = None
        payloads = message.get_payload()

        (payload, ) = [
            p for p in payloads if p.get_content_type() == "text/plain"
        ]

        m = [
            r for r in re.findall(URL_REGEX,
                                  payload.as_string()[0:1024])
            if not config.ckan_host() in r
        ]
        if not m:
            print "Could not find any URLs"
            continue

        first_url = m[0]
        print "Processing first URL: {} and adding to {}".format(
            first_url, dataset)

        res = config.ckan().action.resource_create(
            **{
                'package_id': dataset,
                'url': first_url,
                'description': 'CSV',
                'format': 'CSV',
                'date': datetime.now().strftime("%d/%m/%Y")
            })

        print res

        print "Sending success message"
        send_success(email, config.this_email(), config.get_dataset(dataset))
        invalidate_token(token)