def prune(config, before, since, offset, limit): readConfig(config) apiKey = getApiKey() query = alert_makequery('open', before, since) try: res = alerts_list(apiKey, query, offset, limit) except Exception as exc: click.echo('Error: opsgenie api returned error ' + str(exc.args)) return None click.echo('Alert Count ' + str(len(res))) for adef in res: al_id = adef['id'] adata = alerts_get(apiKey, al_id) al_alias = adata['alias'] al_status = adata['status'] al_desc = adata['description'] al_createdAt = adata['createdAt'] if 'You are making too many requests!' in al_desc: click.echo('Alert ' + al_alias + ' caused by OpsGenie API limits (id:' + al_id + ')') try: res = alerts_close(apiKey, al_id, 'devops', None, 'Close Api Limit Alert') except Exception as exc: click.echo('Error: opsgenie api returned error ' + str(exc.args)) return None click.echo('Closed Alert ' + al_id) else: click.echo('Alert ' + al_createdAt + ' alias:' + al_alias + 'status:' + al_status + ' Ignored') return res
def status(prefix, config, showall): readConfig(config) apiKey = getApiKey() try: hbjson = hb_getlist(apiKey) except Exception as exc: click.echo('Error: opsgenie api returned error ' + str(exc.args)) return None for hbdef in hbjson: hbname = hbdef['name'] if not hbname.startswith(prefix): continue while True: try: hbdata = hb_get(apiKey, hbname) hbexpired = hbdata['expired'] if hbexpired: click.echo('Expired: ' + hbdef['name'] + ' interval:' + str(hbdef['interval'])) elif showall == 'true': click.echo('Healthy: ' + hbdef['name'] + ' interval:' + str(hbdef['interval'])) break except Exception as exc: if exc.args[0] == 429: # hit api limit, pause and try again time.sleep(2) continue else: click.echo('Error: opsgenie api returned error ' + str(exc.args)) return None return hbjson
def getComments(videoId, count): apiKey = getApiKey() apiUrl = getCommentsApiUrl() if (not apiKey or not apiUrl or not videoId): return [] areMoreData = True comments = [] token = "" urlForToken = apiUrl.replace("{api_key}", apiKey).replace("{video_id}", videoId) url = urlForToken.replace("{nextPageToken}", token) commentsCount = 0 while (areMoreData and (commentsCount < count)): commentsData = getCommentsData(url) newComments = commentsData["items"] comments.extend(newComments) if ("nextPageToken" in commentsData): token = commentsData["nextPageToken"] url = urlForToken.replace("{nextPageToken}", token) commentsCount = commentsCount + 100 else: areMoreData = False return comments
def count(config, status, before, since): readConfig(config) apiKey = getApiKey() query = alert_makequery(status, before, since) try: res = alerts_getcount(apiKey, query) except Exception as exc: click.echo('Error: opsgenie api returned error ' + str(exc.args)) return None click.echo('Alert Count ' + str(res)) return res
def list(config): readConfig(config) apiKey = getApiKey() try: hbjson = hb_getlist(apiKey) except Exception as exc: click.echo('Error: opsgenie api returned error ' + str(exc.args)) return None click.echo('Heartbeat Count ' + str(len(hbjson))) for hbdef in hbjson: click.echo('Name: ' + hbdef['name'] + ' interval:' + str(hbdef['interval'])) return hbjson
def hitBinanceWebAPI(self, method, url, param='', signed=False): url = "https://api.binance.com" + url headers = {'X-MBX-APIKEY': config.getApiKey()} if (not signed): try: r = requests.request(method=method.upper(), url=url, params=param, headers=headers) data = r.json() return data except requests.exceptions.ConnectionError as e: err = {'e': 100, 'msg': 'Unable to connect to network'} data = json.loads(json.dumps(err)) except e: err = {'e': 100, 'msg': str(e)} data = json.loads(json.dumps(err)) return data else: if (param != ''): msg = utils.getQueryString(param) signature = utils.getSignature(msg) param.update({"signature": signature}) print param try: r = requests.request(method=method.upper(), url=url, headers=headers, params=param) data = r.json() except requests.exceptions.ConnectionError as e: err = {'e': 100, 'msg': 'Unable to connect to network'} data = json.loads(json.dumps(err)) except e: err = {'e': 100, 'msg': str(e)} data = json.loads(json.dumps(err)) return data
def close(config, before, since, offset, limit): readConfig(config) apiKey = getApiKey() query = alert_makequery('open', before, since) try: res = alerts_list(apiKey, query, offset, limit) except Exception as exc: click.echo('Error: opsgenie api returned error ' + str(exc.args)) return None click.echo('Alert Count ' + str(len(res))) for adef in res: al_id = adef['id'] try: delres = alerts_close(apiKey, al_id, None, None, None) click.echo('Closed Alert ' + al_id) except Exception as exc: click.echo('Error: opsgenie api returned error ' + str(exc.args)) return None return res
def list(config, status, before, since, offset, limit): readConfig(config) apiKey = getApiKey() query = alert_makequery(status, before, since) try: res = alerts_list(apiKey, query, offset, limit) except Exception as exc: click.echo('Error: opsgenie api returned error ' + str(exc.args)) return None click.echo('Alert Count ' + str(len(res))) for adef in res: al_id = adef['id'] adata = alerts_get(apiKey, al_id) al_alias = adata['alias'] al_status = adata['status'] al_desc = adata['description'] al_createdAt = adata['createdAt'] click.echo('Alert ' + al_alias + ' createdAt:' + al_createdAt + ' status:' + al_status + ' desc:' + al_desc) click.echo() return res
def bulkset(prefix, config, timeout): if timeout == None: click.echo('Must specify --timeout option in minutes') return None timeout = int(timeout) readConfig(config) apiKey = getApiKey() click.echo('[ bulkset prefix=' + prefix + ' timeout=' + str(timeout) + ' ]') try: hbjson = hb_getlist(apiKey) except Exception as exc: click.echo('Error: opsgenie api returned error ' + str(exc.args)) return None click.echo('Heartbeat Count ' + str(len(hbjson))) for hbdef in hbjson: hbname = hbdef['name'] if not hbname.startswith(prefix): # skip non-matching heartbeat record continue hbtimeout = hbdef['interval'] if hbtimeout != timeout: # retry loop to patch heartbeat record while True: try: hbres = hb_patch(apiKey, hbname, timeout) click.echo('HB ' + hbname + ' timeout old:' + str(hbtimeout) + ' new:' + str(timeout)) break except Exception as exc: if exc.args[0] == 429: # hit api limit, pause and try again time.sleep(2) continue else: click.echo('Error: opsgenie api returned error ' + str(exc.args)) return None return hbjson
def getAuth(): auth = tweepy.OAuthHandler(config.getApiKey(), config.getApiSecret()) auth.set_access_token(config.getAccessToken(), config.getAccessSecret()) return auth