pythonregexpython-2.7pushover

Pushover Acknowlage in python


Im having some issue trying to get ackknowalgemtns working with python.

thus far my code posts the message and gets the JSON sting in return but now i need a way to get the receipt out of the string

import requests
app_token = 'app token'
user_token = 'user token'
title = 'TEST MESSGAE'
text = 'text for polling'
params = {
            'token': app_token,
            'user': user_token,
            'title': title,
            'message': text,
            'retry': 30, 
            'expire': 180,
            'priority': 2,
            'sound': 'siren',
        }
msg = requests.post('https://api.pushover.net/1/messages.json', data=params)
print "POSTed message"
print msg.json()

output is

POSTed message
{u'status': 1, u'receipt': u'riD7NEukZsiDQpjhiNppphYnpue8uK', u'request': u'ef5f42d568e966cbf3d2b28c6579397c'}

The parameter is a case-sensitive, 30 character string containing the character set [A-Za-z0-9]. would this be a REGEX job ?


Solution

  • Because it's json type, so you can just use json module to load the data as a dict. For example if you have some json data like this:

    '{"message":"Hello there, wayfaring stranger"}'
    

    Then you can use json.loads() function to load it as a dict like this:

    >>> a = '{"message":"Hello there, wayfaring stranger"}'
    >>> import json
    >>> d = json.loads(a)
    >>> d.items()
    dict_items([('message', 'Hello there, wayfaring stranger')])
    >>> 
    

    As you can see, a is string, but d is a dict. And now, 'Hello there, wayfaring stranger' is the value of key 'message'.


    But msg.json() can auto covert the json data to dict, so you don't need json module here. So if you want to get 'ef5f42d568e966cbf3d2b28c6579397c', you can simply do something like this:

    json_data = msg.json()
    print json_data['request']