def tag(self): text = request.params.get('text') apikey = request.params.get('key') language = request.params.get('language') channel = request.params.get('channel') referrer = request.headers.get('REFERER', '/') host = get_host(referrer) ip_address = request.environ.get("X_FORWARDED_FOR", request.environ.get("HTTP_X_FORWARDED_FOR", request.environ.get("REMOTE_ADDR"))) log.info('apikey=%s referrer=%s host=%s', apikey, referrer, host) # The text parameter is required for the tag method if not text: log.info('Missing text parameter.') return "001 Missing Parameter: Required parameter is not supplied (text)." log.info('Text to be tagged: %s', text) tags = TweetTagger.tag(text) log.info('Tags extracted: %s', str(tags)) # Now update the call count on the key row... key = session.get('key') if key: key.calls = key.calls + 1 key.last_call = datetime.datetime.now() # Log the api call apicall = APICall() apicall.parameters = text apicall.result = simplejson.dumps(tags) if key: apicall.apikey_id = key.id apicall.method = 'tag' apicall.http_method = request.method apicall.called_from = ip_address Session.add(apicall) Session.commit() response.headers['Content-Type'] = 'application/json' return simplejson.dumps(tags)
default=60) (options, args) = parser.parse_args() if not options.owner_name: parser.error("You must supply the owner name. (--owner_name).") if not options.value: random.shuffle(KEYCHARS) options.value = ''.join(KEYCHARS[0:options.keylength]) conf = appconfig('config:' + options.ini, relative_to='.') load_environment(conf.global_conf, conf.local_conf) engine = create_engine(conf['sqlalchemy.url'], echo=True) meta = MetaData() conn = engine.connect() apikey = APIKey() apikey.owner_name = options.owner_name apikey.keystr = options.value apikey.owner_url = options.owner_url apikey.valid_domains = options.valid_domains apikey.calls_per_minute = options.calls_per_minute Session.add(apikey) Session.commit() print "Thanks for applying to be an Alpha Tester for Swift Web Services." print "Here is your API key for OpenSiLCC: %s" % options.value
def example(self): text = request.params.get('text') apikey = request.params.get('key') language = request.params.get('language') channel = request.params.get('channel') referrer = request.headers.get('REFERER', '/') host = get_host(referrer) ip_address = request.environ.get("X_FORWARDED_FOR", request.environ.get("HTTP_X_FORWARDED_FOR", request.environ.get("REMOTE_ADDR"))) tags = request.params.get('tags') corpus = request.params.get('corpus') log.info('apikey=%s referrer=%s host=%s', apikey, referrer, host) # The text parameter is required for the example method if not text: log.info('Missing text parameter.') return "001 Missing Parameter: Required parameter is not supplied (text)." # The tags parameter is required for the example method if not tags: log.info('Missing tags parameter.') return "001 Missing Parameter: Required parameter is not supplied (tags)." # The corpus parameter is required for the example method if not tags: log.info('Missing corpus parameter.') return "001 Missing Parameter: Required parameter is not supplied (corpus)." # Now update the call count on the key row... key = session.get('key') if key: key.calls = key.calls + 1 key.last_call = datetime.datetime.now() # Log the api call... apicall = APICall() apicall.parameters = text apicall.result = simplejson.dumps(tags) if key: apicall.apikey_id = key.id apicall.method = 'example' apicall.http_method = request.method apicall.called_from = ip_address Session.add(apicall) Session.commit() # Save the example to the database... example = Example() example.text = text example.tags = tags example.corpus = corpus example.apicall_id = apicall.id Session.add(example) Session.commit() tags = tags.split() response.headers['Content-Type'] = 'application/json' return simplejson.dumps(dict(text=text,tags=tags,corpus=corpus))
def tag(self): text = request.params.get('text') apikey = request.params.get('key') language = request.params.get('language') channel = request.params.get('channel') referrer = request.headers.get('REFERER', '/') host = get_host(referrer) log.info('apikey=%s referrer=%s host=%s', apikey, referrer, host) if not apikey: # From Swift River API docs: # 007 Access denied. Your API key is no longer valid. Please contact the administrator. # 008 Access denied. You need an API key to perform that task. Please contact the administrator. response.status = '401 Unauthorized' return "008 Access denied. You need an API key to perform that task. Please contact the administrator." # Now load the key from the db if it exists... key = Session.query(APIKey).filter_by(keystr=apikey).first() if not key: log.info('No matching key was found in the db.') response.status = '401 Unauthorized' return "008 Access denied. You need an API key to perform that task. Please contact the administrator." # Check that the key is valid for the referrer host... if key.valid_domains != host and key.valid_domains != '*': log.info("A Key was found but the referring host is invalid.") response.status = '401 Unauthorized' return "008 Access denied. You need an API key to perform that task. Please contact the administrator." # Now check the number of calls in the last minute... query = select([func.count(APICall.table.c.id)]) query = query.where("called_at > now() - interval 1 minute") results = Session.execute(query).fetchone() log.info('number of previous calls: %s', str(results)) # The text parameter is required for the tag method if not text: log.info('Missing text parameter.') return "001 Missing Parameter: Required parameter is not supplied (text)." log.info('Text to be tagged: %s', text) tags = TweetTagger.tag(text) log.info('Tags extracted: %s', str(tags)) # Now update the call count on the key row... key.calls = key.calls + 1 key.last_call = datetime.datetime.now() # Log the api call apicall = APICall() apicall.parameters = text apicall.result = simplejson.dumps(tags) apicall.apikey_id = key.id apicall.method = 'tag' apicall.http_method = request.method ip_address = request.environ.get("X_FORWARDED_FOR", request.environ.get("HTTP_X_FORWARDED_FOR", request.environ.get("REMOTE_ADDR"))) apicall.called_from = ip_address Session.add(apicall) Session.commit() response.headers['Content-Type'] = 'application/json' return simplejson.dumps(tags)