<commit_before>#!/usr/bin/env python
#
# Copyright 2011-2014 Splunk, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License"): you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.


# python yelp.py __EXECUTE__ 'location="New York"'

import sys
import time
from splunklib.searchcommands import \
    dispatch, GeneratingCommand, Configuration, Option, validators

import requests
import oauth2
import json
import urllib

@Configuration(retainsevents=False)
class YelpCommand(GeneratingCommand):
    location = Option(
        doc='''
        **Syntax:** **location=***<location>*
        **Description:** Location to search for businesses''',
        require=True)

    term = Option(
        doc='''
        **Syntax:** **term=***<term>*
        **Description:** Type of business''',
        require=False)

    category = Option(
        doc='''
        **Syntax:** **category=***<category>*
        **Description:** Delimitted list of categories''',
        require=False)

    sort = Option(
        doc='''
        **Syntax:** **sort=***<sort>*
        **Description:** Sort mode: 0=Best matched (default), 1=Distance, 2=Highest Rated''',
        require=False, validate=validators.Integer(), default=1)

    limit = Option(
        doc='''
        **Syntax:** **limit=***<limit>*
        **Description:** Number of records to return''',
        require=False, validate=validators.Integer())

    offset = Option(
        doc='''
        **Syntax:** **offset=***<offset>*
        **Description:** Offset the list of returned business results by this amount''',
        require=False, validate=validators.Integer())

    def generate(self):
        config = self.get_configuration()
        url = self.get_yelp_signed_url(
            config['consumer-key'],
            config['consumer-secret'],
            config['token'],
            config['token-secret'])
        
        response = requests.get(url)

        timestamp = time.time()
        results = response.json()
        
        if response.status_code != 200:
            yield {'ERROR': results['error']['text']}
            return
        
        for result in results["businesses"]:
            yield self.getEvent(result, timestamp)

    def getEvent(self, result, timestamp):
        event = {'_time': timestamp, 'name': result['name'], 'rating':result['rating'], 
            'address': ', '.join(result['location']['address']),<commit_msg>refactoring / cleanup for the post<commit_after>#!/usr/bin/env python
#
# Copyright 2011-2014 Splunk, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License"): you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.


# python yelp.py __EXECUTE__ 'location="New York"'

import sys, time, requests, oauth2, json, urllib

from splunklib.searchcommands import \
    dispatch, GeneratingCommand, Configuration, Option, validators

@Configuration()
class YelpCommand(GeneratingCommand):
    location = Option(
        doc='''
        **Syntax:** **location=***<location>*
        **Description:** Location to search for businesses''',
        require=True)

    term = Option(
        doc='''
        **Syntax:** **term=***<term>*
        **Description:** Type of business''',
        require=False)

    category = Option(
        doc='''
        **Syntax:** **category=***<category>*
        **Description:** Delimitted list of categories''',
        require=False)

    sort = Option(
        doc='''
        **Syntax:** **sort=***<sort>*
        **Description:** Sort mode: 0=Best matched (default), 1=Distance, 2=Highest Rated''',
        require=False, validate=validators.Integer(), default=1)

    limit = Option(
        doc='''
        **Syntax:** **limit=***<limit>*
        **Description:** Number of records to return''',
        require=False, validate=validators.Integer())

    offset = Option(
        doc='''
        **Syntax:** **offset=***<offset>*
        **Description:** Offset the list of returned business results by this amount''',
        require=False, validate=validators.Integer())

    def generate(self):
        config = self.get_configuration()
        url = self.get_yelp_signed_url(
            config['consumer-key'],
            config['consumer-secret'],
            config['token'],
            config['token-secret'])
        
        response = requests.get(url)

        results = response.json()
        
        if response.status_code != 200:
            yield {'ERROR': results['error']['text']}
            return
        
        for result in results["businesses"]:
            yield self.getEvent(result)

    def getEvent(self, result):
        event = {'_time': time.time(), 'name': result['name'], 'rating':result['rating'], 
            'address': ', '.join(result['location']['address']),
{'fraction_non_alphanumeric': 0.14098011363636365, 'fraction_numerical': 0.007102272727272727, 'mean_word_length': 2.8821502412129565, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2163693', 'n_tokens_mistral': 1535, 'n_tokens_neox': 1467, 'n_words': 464}