def _request_token_if_needed(request, record_id, api_base): """ Requests a request token for record id, if needed. """ ts = TokenStore() cookie = request.get_cookie(_cookie_name) token, ex_api_base, ex_record_id = ts.tokenServerRecordForCookie(cookie) # we already got a token, test if it still works if token is not None \ and (record_id is None or int(ex_record_id) == int(record_id)) \ and (api_base is None or unicode(ex_api_base) == unicode(api_base)) \ and _test_record_token(api_base, record_id, token): _log_debug("reusing existing token") return False, None # request a fresh token bottle.response.delete_cookie(_cookie_name) _log_debug("requesting token for record %s on %s" % (record_id, api_base)) smart = _smart_client(api_base, record_id) if smart is None: return False, None smart.token = None try: token = smart.fetch_request_token() except Exception, e: return False, str(e)
def _exchange_token(req_token, verifier): """ Takes the request token and the verifier, obtained in our authorize callback, and exchanges it for an access token. Stores the access token and returns a tuple with the cookie key, api_base and record_id. """ ts = TokenStore() full_token, api_base, record_id = ts.tokenServerRecordForToken(req_token) if record_id is None: _log_error("Unknown token, cannot exchange %s" % req_token) return None, None, None # exchange the token _log_debug("exchange token: %s" % full_token) smart = _smart_client(api_base, record_id) if smart is None: return None, None, None smart.update_token(full_token) try: acc_token = smart.exchange_token(verifier) except Exception, e: _log_error("token exchange failed: %s" % e) return None, api_base, None
def _testrecord_from_request(request): """ Returns a "TestRecord" instance from the cookie in the request. """ if request is None: return None # read the cookie cookie = request.get_cookie(_cookie_name) if cookie is None: return None # get the token ts = TokenStore() token, api_base, record_id = ts.tokenServerRecordForCookie(cookie) record = None if record_id is not None: smart_client = _smart_client(api_base, record_id) if smart_client is not None: smart_client.update_token(token) record = TestRecord(smart_client) return record
import sys from flask import request from flask_api import FlaskAPI sys.path.append(os.path.abspath("../indexer/src")) from searchengine import SearchEngine from tokenstore import TokenStore app = FlaskAPI(__name__) @app.route("/") def index(): return app.send_static_file('html/index.html') @app.route("/api/search", methods=["POST"]) def search() -> list: """ curl -X POST http://127.0.0.1:5000/api/search -d queries="hello world" """ return search_engine.search(request.data.get("queries").lower().split(",")) if __name__ == "__main__": store = TokenStore() search_engine = SearchEngine(store) app.run(debug=True, host="0.0.0.0")
import json import os import signal from tokenstore import TokenStore from workerpool import WorkerPool if __name__ == '__main__': bookkeeping = json.load(open("../WEBPAGES_RAW/bookkeeping.json", 'r')) store = TokenStore() store.store_bookkeeping(bookkeeping) pool = WorkerPool(TokenStore(), worker_num=32, bookkeeping=bookkeeping, mode=os.environ.get("INDEXER_MODE", "SERVER")) # capture signal.SIGINT and handle it with safe termination signal.signal(signal.SIGINT, lambda _s, _f: pool.safe_terminate()) pool.execute()
# loop over all records and test against our rules if _BG: for record_id in smart.loop_over_records(): record = TestRecord(smart) print "-> Record", record.record_id for rule in rules: print "--> Testing against", rule.name if rule.match_against(record): print "==> Record %s matches rule %s" % (record_id, rule.name) print rule.perform_actions(record) sys.exit(0) # -------------------------------------------------------------------------- Record Scoped smart.record_id = '665677' ts = TokenStore() known_token = ts.tokenForRecord(ep_url, smart.record_id) # request a token for a specific record id if known_token is None: token = smart.fetch_request_token() ts.storeTokenForRecord(ep_url, smart.record_id, token) print "-> Now visit: %s" % smart.auth_redirect_url Popen(['open', smart.auth_redirect_url]) oauth_verifier = raw_input("Enter the oauth_verifier: ") # exchange and store the token token = smart.exchange_token(oauth_verifier) if token and token.get('oauth_token') and token.get('oauth_token_secret'): ts.storeTokenForRecord(ep_url, smart.record_id, token)