Package: bountycountry
bountycountry-pythonPython library for the Bounty Country APIThe Bounty Country Python library provides convenient access to the Bounty Country API for applications written in the Python language.DocumentationSee thePython API docs.InstallationYou don't need this source code unless you want to modify the package. If you just
want to use the package, just run:pip install --upgrade bountycountryInstall from source with:python setup.py installRequirementsPython 3+Requests >= 2.20UsageThe library needs to be configured with your account's public key and secret key which are
available in yourBounty Country Dashboard.Setbountycountry.BC_PUBLIC_KEYto your public key value.
Setbountycountry.BC_SECRET_KEYto your secret key value.importbountycountrybountycountry.BC_PUBLIC_KEY="....your.public.key.here...."bountycountry.BC_SECRET_KEY="....your.secret.key.here...."Stream a live datasetThe stream data is returned in JSON batches via apython generator. You can iterate over each batch/page of items returned using any loop. By default a batch will consist of 250 items.results=bountycountry.getLiveStream('dataset-id-goes-here')forbatchinresults:#do something with each batch of resultsprint(batch)Batch Format{'Items':[{'ttl':1550188588,'upload_timestamp':1550102188.420983,'item_data':'the actual data goes here as a string or JSON object'},{'ttl':1550189000,'upload_timestamp':1550109000.420456,'item_data':{"somekey":"somevalue","somelist":[204,306,99]}},]}ThegetLiveStreamfunction will indefinitely poll Bounty Country for the latest data and implement an exponential backoff (starting with a 2 second wait) if there is no new data before retrying. By default, the function will also attempt to minimize the requests used (and resultant costs to the user) by introducing additional waits if the result set returned is less than the BatchSize (which is set to the maximum of 250 items by default).You can also cap your request expenditure rate by setting the MaxHourlyRequests parameter.OPTIONSBatchSize- the number of results to return per request/page (maximum of 250, default=250, format = integer)MinimizeRequests- whether to reduce request frequency to maximize result set size (reduce cost). Set to False if you have a strong requirement for minimal data latency and are less concerned by cost. (default=True)MaxHourlyRequests- the maximum number of requests to perform per hour, if exceeded the function will sleep for OnMaxWait seconds (default=None, format = integer)OnMaxWait- the number of seconds to wait if MaxHourlyRequests is exceeded (default = 0, format = integer)Get a specific time range within a streamA specific time range in the last 24 hours of data in a stream can be queried usingbountycountry.getStreamRange.Timestamps must be expressed in EpochTime format integers:importtime# get the current time as integer epochtimecurrenttime=int(time.time())# convert ISO8601 time stamp to integer epochtimeepochtime=int(time.mktime(time.strptime("2019-04-01 19:20:00","%Y-%m-%d%H:%M:%S")))The Stream Range is apython generatorso you can iterate over each batch/page of items returned using any loop.
By default a batch will return up to 250 items.# create generatorresults=bountycountry.getStreamRange('dataset-id-goes-here',FromTime=1554106800,ToTime=1554109000)# iterate over batches/pages and print the raw data of each itemforbatchinresults:foriteminbatch['Items']:print(item['item_data'])OPTIONSIf a FromTime and ToTime are not provided the function returns the 250 newest items in the streamFromTime- epoch timestamp of the earliest point in time to queryToTime- epoch timestamp of the latest point in time to queryOrder- the order in which to return results (options = 'Newest','Oldest', default = 'Newest')Limit- function will stop when Limit number of items have been returned (default = None, format = integer)Last- instead of passing a 'ToTime' or 'FromTime' you can manually pass the 'Last' result timestamp to get the next batch/page of results (format = integer epoch timestamp)BatchSize- the number of results to return per request/page (maximum of 250, default=250, format = integer)Post items to a StreamThegetLiveStreamfunction will upload your items in batches of 25. Items can be accepted in one of three formats specified in theformatparameter:'array'- accepts a python array of strings or objects. Objects will be json-serialized.'lines'- reads a file (provide a string path) line by line and uploads each line as an individual item'dir'- reads all files in a directory (provide a string path) and uploads the text contexts. Non-utf-8 encoded files are skipped.items=[{"somekey":[{"something nested":"value","somethingelsenested":"somevalue"}],"a string","another string","final string"]bountycountry.postStream('dataset-id-goes-here',items,format='array')