From 30c8c88fe874e757bc61b27e2604b0fa1ff3a85a Mon Sep 17 00:00:00 2001 From: Elbert Fliek Date: Tue, 12 Sep 2017 13:42:27 +0200 Subject: [PATCH 1/2] added example of how to ignore behaviors --- py/ignore_behaviors.py | 63 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 py/ignore_behaviors.py diff --git a/py/ignore_behaviors.py b/py/ignore_behaviors.py new file mode 100644 index 0000000..3cf6700 --- /dev/null +++ b/py/ignore_behaviors.py @@ -0,0 +1,63 @@ +''' + Filename: ignore_behaviors.py + Author: Elbert Fliek + Python Version: 2.7.12 + + Takes a .txt file containing behavior IDs (one per line) as an argument. + Ignores those behaviors +''' +import argparse +import lotame_utils +import requests +from ConfigParser import SafeConfigParser + + +# Set up our Parser and get the values +parser = SafeConfigParser() +parser.read('config.cfg') +username = parser.get('api_examples', 'username') +password = parser.get('api_examples', 'password') +base_api_url = parser.get('api_examples', 'api_url') + +argparser = argparse.ArgumentParser(description='Ignore a list of behaviors.') +argparser.add_argument( + 'idsfile', + help='the name of the file containing a list of behaviors to ignore') +args = argparser.parse_args() + + +def putRequest(grandingTicket, service, body): + service_call = base_api_url + 'behaviors/ignored' + payload = {'service': service_call} + service_ticket = requests.post(grandingTicket, data=payload).text + headers = {'Accept': 'application/json'} + return requests.put( + ('%s?ticket=%s') % (service_call, service_ticket), + json=body, + headers=headers) + + +def main(): + grandingTicket = lotame_utils.getTicketGrandingTicket(username, password) + + ignore = [] + with open(args.idsfile) as behavior_ids: + for behavior_id in behavior_ids: + ignore.append({'id': behavior_id}) + + body = {'behaviors': { + 'behavior': ignore, + 'totalRows': len(ignore) + }} + + resp = putRequest(grandingTicket, 'behaviors/ignored', body) + + print 'Ignore behaviors response status code: %s' % resp.status_code + + # Once we are done with the Ticket Granting Ticket we should clean it up' + resp = requests.delete(grandingTicket) + print 'Remove Granding Ticket response status code: %s' % resp.status_code + + +if __name__ == '__main__': + main() From a8cc32e2f253efee7159dc6d475698ccd889e0c6 Mon Sep 17 00:00:00 2001 From: Elbert Fliek Date: Tue, 12 Sep 2017 13:44:28 +0200 Subject: [PATCH 2/2] added comment describing config file --- py/ignore_behaviors.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/py/ignore_behaviors.py b/py/ignore_behaviors.py index 3cf6700..97b3c94 100644 --- a/py/ignore_behaviors.py +++ b/py/ignore_behaviors.py @@ -5,6 +5,15 @@ Takes a .txt file containing behavior IDs (one per line) as an argument. Ignores those behaviors + + it expects a config file in the same directory that is named config.cfg + and has the following content: + [api_examples] + authentication_url = https://crowdcontrol.lotame.com/auth/v1/tickets + api_url = https://api.lotame.com/2/ + username = + password = + ''' import argparse import lotame_utils