0

I have been working on some code that will grab emergency incident information from a service called PulsePoint. It works with software built into computer controlled dispatch centers.

This is an app that empowers citizen heroes that are CPR trained to help before a first resonder arrives on scene. I'm merely using it to get other emergency incidents.

I reversed-engineered there app as they have no documentation on how to make your own requests. Because of this i have knowingly left in the api key and auth info because its in plain text in the Android manifest file.

I will definitely make a python module eventually for interfacing with this service, for now its just messy.

Anyhow, sorry for that long boring intro.

My real question is, how can i simplify this function so that it looks and runs a bit cleaner in making a timed request and returning a json object that can be used through subscripts?

import requests, time, json
def getjsonobject(agency):
    startsecond = time.strftime("%S")
    url = REDACTED
    body = []
    currentagency = requests.get(url=url, verify=False, stream=True, auth=requests.auth.HTTPBasicAuth(REDACTED, REDCATED), timeout = 13)
    for chunk in currentagency.iter_content(1024):
        body.append(chunk)
        if(int(startsecond) + 5 < int(time.strftime("%S"))): #Shitty internet proof, with timeout above
            raise Exception("Server sent to much data")
    jsonstringforagency = str(b''.join(body))[2:][:-1] #Removes charecters that define the response body so that the next line doesnt error
    currentagencyjson = json.loads(jsonstringforagency) #Loads response as decodable JSON
    return currentagencyjson

currentincidents = getjsonobject("lafdw")
for inci in currentincidents["incidents"]["active"]:
    print(inci["FullDisplayAddress"])
Beall619
  • 65
  • 4
  • 9

1 Answers1

1

Requests handles acquiring the body data, checking for json, and parsing the json for you automatically, and since you're giving the timeout arg I don't think you need separate timeout handling. Request also handles constructing the URL for get requests, so you can put your query information into a dictionary, which is much nicer. Combining those changes and removing unused imports gives you this:

import requests

params = dict(both=1,
              minimal=1,
              apikey=REDACTED)

url = REDACTED

def getjsonobject(agency):
    myParams = dict(params, agency=agency)
    return requests.get(url, verify=False, params=myParams, stream=True,
            auth=requests.auth.HTTPBasicAuth(REDACTED, REDACTED),
            timeout = 13).json()

Which gives the same output for me.

Beall619
  • 65
  • 4
  • 9
Nathan Vērzemnieks
  • 5,495
  • 1
  • 11
  • 23
  • This works and was close to my original code however what happens on my rural connection is the connection is successful but due to an unknown problem it never finishes returning the data and just gets stuck there indefinitely – Beall619 Jan 25 '18 at 17:08
  • Note that it happens sometimes but at least once a day if I run it in a timed loop – Beall619 Jan 25 '18 at 17:12
  • Ah, that would have been useful information to put in your question :) In that case I recommend looking at this answer: https://stackoverflow.com/a/22096841/9200529 – Nathan Vērzemnieks Jan 25 '18 at 19:40
  • That seams really easy, however the signals answer to that question may be a better solution for me as it's standard in Python. Thank you for appointing me to it! – Beall619 Jan 25 '18 at 21:29