def __init__(self, username, password, url=URL_BASE, validity_ms=120000, auto_login=False, proxies=None): """Log-in to the Luminoso API username, password => (str) credentials to access the server validity_ms => milliseconds of validity for signed messages auto_login => remember the credentials and use them when the connection times out (NOT IMPLEMENTED YET) session => requests session to use for queries""" # Store the login parameters self._auto_login = auto_login self.username = username if auto_login else None self.password = password if auto_login else None self.url = url.rstrip('/') parsed = urlparse.urlparse(self.url) self._host = parsed.netloc # Store the validity parameter self._validity_ms = validity_ms # Initialize the requests session self._session = requests.session() if proxies is not None: self._session.proxies = proxies # Fetch session credentials self.login(username, password)
def postit(tag, location): #change verifiy to True if using valid certs #opens a session so we have the same token for our two requests client = requests.session(config={'verbose': sys.stderr}, verify=False, \ auth=('user', 'pw')) csrftoken = getcsrftoken(client) #get session token payload = {'tag': tag, 'location': location} #information to upsert payload.update(csrftoken) #append csrftoken dictionary to payload #post the payload to the api r = client.post("https://localhost:5000/reportwrite", data=payload, \ verify=False) #make True for production use with valid certs
def auth(app_id, app_secret): OAuthHook.consumer_key = app_id OAuthHook.consumer_secret = app_secret oauth_hook = OAuthHook('', '') client = requests.session(hooks={'pre_request': oauth_hook}) request = client.get(AUTH_URL % (app_id, app_secret)) access_token = request.headers['x-access-token'] access_token_expires = request.headers['x-access-token-expires'] return (access_token, access_token_expires)
def __init__(self, auth, url, root_url=None, proxies=None): """ Create a LuminosoClient given an existing auth object. It is probably easier to call LuminosoClient.connect() to handle the authentication for you. """ self._auth = auth self._session = requests.session() self._session.auth = auth if proxies is not None: self._session.proxies = proxies self.url = ensure_trailing_slash(url) self.root_url = root_url or get_root_url(url)
def __init__(self, auth, url, proxies=None): """ Create a LuminosoClient given an existing auth object. It is probably easier to call LuminosoClient.connect() to handle the authentication for you. """ self._auth = auth self._session = requests.session() self._session.auth = auth if proxies is not None: self._session.proxies = proxies self.url = ensure_trailing_slash(url) self.root_url = get_root_url(url)
def twitter_user_show(user): """ Get user info using twitter API """ client = requests.session( hooks={'pre_request': twitter_oauth_hook, 'response': throttle_hook}) request_url = "https://api.twitter.com/1.1/users/show.json?screen_name=%s" % user resp = client.get(request_url) json_content = resp.content try: data = json.loads(json_content) check_twitter_error(data) except ValueError: if "<title>Twitter / Over capacity</title>" in json_content: raise TwitterError("Capacity Error") if "<title>Twitter / Error</title>" in json_content: raise TwitterError("Technical Error") raise TwitterError("json decoding") return data
def search_tweets(query=None, count=10, lang='en', result_type='recent'): """ Search tweets according to query using twitter API """ client = requests.session( hooks={'pre_request': twitter_oauth_hook, 'response': throttle_hook}) request_url = "https://api.twitter.com/1.1/search/tweets.json?q=%s&lang=%s&count=%d&result_type=%s" % \ (query, lang, count, result_type) resp = client.get(request_url) json_content = resp.content try: data = json.loads(json_content) check_twitter_error(data) except ValueError: if "<title>Twitter / Over capacity</title>" in json_content: raise TwitterError("Capacity Error") if "<title>Twitter / Error</title>" in json_content: raise TwitterError("Technical Error") raise TwitterError("json decoding") return data['statuses']
def twitter_user_show(user): """ Get user info using twitter API """ client = requests.session(hooks={ 'pre_request': twitter_oauth_hook, 'response': throttle_hook }) request_url = "https://api.twitter.com/1.1/users/show.json?screen_name=%s" % user resp = client.get(request_url) json_content = resp.content try: data = json.loads(json_content) check_twitter_error(data) except ValueError: if "<title>Twitter / Over capacity</title>" in json_content: raise TwitterError("Capacity Error") if "<title>Twitter / Error</title>" in json_content: raise TwitterError("Technical Error") raise TwitterError("json decoding") return data
def make_github_issue(title, body=None, labels=None): git = dict() # Github repo to POST data to url = "https://api.github.com/repos/%s/%s/issues" % (settings.REPO_OWNER, settings.REPO_NAME) # Create an authenticated session to create the issue session = requests.session(headers={"Authorization": "token %s" % os.environ.get("TOKEN")}) # Create our issue issue = {"title": title, "body": body, "labels": labels} # Add the issue to our repository r = session.post(url, json.dumps(issue)) # Handle returned data from the server if r.status_code == 201: git["content"] = json.loads(r.content) git["err"] = False return git else: git["content"] = json.loads(r.content) git["err"] = True return git
def search_tweets(query=None, count=10, lang='en', result_type='recent'): """ Search tweets according to query using twitter API """ client = requests.session(hooks={ 'pre_request': twitter_oauth_hook, 'response': throttle_hook }) request_url = "https://api.twitter.com/1.1/search/tweets.json?q=%s&lang=%s&count=%d&result_type=%s" % \ (query, lang, count, result_type) resp = client.get(request_url) json_content = resp.content try: data = json.loads(json_content) check_twitter_error(data) except ValueError: if "<title>Twitter / Over capacity</title>" in json_content: raise TwitterError("Capacity Error") if "<title>Twitter / Error</title>" in json_content: raise TwitterError("Technical Error") raise TwitterError("json decoding") return data['statuses']
def make_github_issue(title, body=None, labels=None): git = dict() # Github repo to POST data to url = 'https://api.github.com/repos/%s/%s/issues' % (settings.REPO_OWNER, settings.REPO_NAME) # Create an authenticated session to create the issue session = requests.session( headers={'Authorization': 'token %s' % os.environ.get('GITHUB_TOKEN')}) # Create our issue issue = {'title': title, 'body': body, 'labels': labels} # Add the issue to our repository r = session.post(url, json.dumps(issue)) # Handle returned data from the server if r.status_code == 201: git['content'] = json.loads(r.content) git['err'] = False return git else: git['content'] = json.loads(r.content) git['err'] = True return git