How to decode POST data from github in web.py? - python

(This question is related - but not the same - to this one)
This is the POST data that I get from a github hook:
payload=%7B%22pusher%22%3A%7B%22name%22%3A%22none%22%7D%2C%22repository%22%3A%7B%22name%22%3A%22test%22%2C%22size%22%3A84%2C%22has_wiki%22%3Atrue%2C%22created_at%22%3A%222012%2F01%2F12%2001%3A04%3A25%20-0800%22%2C%22watchers%22%3A1%2C%22private%22%3Afalse%2C%22fork%22%3Afalse%2C%22url%22%3A%22https%3A%2F%2Fgithub.com%2Fgonvaled%2Ftest%22%2C%22pushed_at%22%3A%222012%2F01%2F12%2001%3A05%3A26%20-0800%22%2C%22has_downloads%22%3Atrue%2C%22open_issues%22%3A0%2C%22has_issues%22%3Atrue%2C%22homepage%22%3A%22%22%2C%22description%22%3A%22%22%2C%22forks%22%3A1%2C%22owner%22%3A%7B%22name%22%3A%22gonvaled%22%2C%22email%22%3A%22gonvaled%40gonvaled.com%22%7D%7D%2C%22forced%22%3Afalse%2C%22after%22%3A%2214209371dcbdd95cc3ef5c4a07d80edd42f1295c%22%2C%22deleted%22%3Afalse%2C%22ref%22%3A%22refs%2Fheads%2Fmaster%22%2C%22commits%22%3A%5B%5D%2C%22before%22%3A%2214209371dcbdd95cc3ef5c4a07d80edd42f1295c%22%2C%22compare%22%3A%22https%3A%2F%2Fgithub.com%2Fgonvaled%2Ftest%2Fcompare%2F1420937...1420937%22%2C%22created%22%3Afalse%7D
Which I can decode using this:
urllib.unquote(data)
Getting this:
payload={"pusher":{"name":"none"},"repository":{"name":"test","size":84,"has_wiki":true,"created_at":"2012/01/12 01:04:25 -0800","watchers":1,"private":false,"fork":false,"url":"https://github.com/gonvaled/test","pushed_at":"2012/01/12 01:05:26 -0800","has_downloads":true,"open_issues":0,"has_issues":true,"homepage":"","description":"","forks":1,"owner":{"name":"gonvaled","email":"gonvaled#gonvaled.com"}},"forced":false,"after":"14209371dcbdd95cc3ef5c4a07d80edd42f1295c","deleted":false,"ref":"refs/heads/master","commits":[],"before":"14209371dcbdd95cc3ef5c4a07d80edd42f1295c","compare":"https://github.com/gonvaled/test/compare/1420937...1420937","created":false}
I can see the JSON there, after the payload= bit. The question I have is: what format is the full data? How can I get just the payload bit, using standard python libraries; I would prefer to avoid splitting the string myself, since I do not know the special cases.
The github help page gives this suggested implementation for a Sinatra server:
post '/' do
push = JSON.parse(params[:payload])
"I got some JSON: #{push.inspect}"
end
How can this params array be handled in python, with standard libraries? What is the most pythonic implementation of that Ruby code? My end goal is to have the full POST data accessible as a python dictionary.

Try this:
import json
import urlparse
data = urlparse.parse_qs(r)
print json.loads(r['payload'][0])
where r is the string you received as response.
See http://docs.python.org/library/urlparse.html#urlparse.parse_qs

import urlparse
import json
s = "payload=%7B%22pusher%22%3A%7B%22name%22%3A%22none%22%7D%2C%22repository%22%3A%7B%22name%22%3A%22test%22%2C%22size%22%3A84%2C%22has_wiki%22%3Atrue%2C%22created_at%22%3A%222012%2F01%2F12%2001%3A04%3A25%20-0800%22%2C%22watchers%22%3A1%2C%22private%22%3Afalse%2C%22fork%22%3Afalse%2C%22url%22%3A%22https%3A%2F%2Fgithub.com%2Fgonvaled%2Ftest%22%2C%22pushed_at%22%3A%222012%2F01%2F12%2001%3A05%3A26%20-0800%22%2C%22has_downloads%22%3Atrue%2C%22open_issues%22%3A0%2C%22has_issues%22%3Atrue%2C%22homepage%22%3A%22%22%2C%22description%22%3A%22%22%2C%22forks%22%3A1%2C%22owner%22%3A%7B%22name%22%3A%22gonvaled%22%2C%22email%22%3A%22gonvaled%40gonvaled.com%22%7D%7D%2C%22forced%22%3Afalse%2C%22after%22%3A%2214209371dcbdd95cc3ef5c4a07d80edd42f1295c%22%2C%22deleted%22%3Afalse%2C%22ref%22%3A%22refs%2Fheads%2Fmaster%22%2C%22commits%22%3A%5B%5D%2C%22before%22%3A%2214209371dcbdd95cc3ef5c4a07d80edd42f1295c%22%2C%22compare%22%3A%22https%3A%2F%2Fgithub.com%2Fgonvaled%2Ftest%2Fcompare%2F1420937...1420937%22%2C%22created%22%3Afalse%7D"
L = urlparse.parse_qsl(s)
for k, v in L:
print k
print json.loads(v)
gives
payload
{u'forced': False, u'compare': u'https://github.com/gonvaled/...1420937', ...
u'before': u'14209371dcbdd95cc3ef5c4a07d80edd42f1295c'}

Related

How to extract only wanted property from JSON object

When I run the code:
import requests
import json
def get_fact():
catFact = requests.get("https://catfact.ninja/fact?max_length=140")
json_data = json.loads(catFact.text)
return json_data
print(get_fact())
The output is like
{'fact': "Cats are the world's most popular pets, outnumbering dogs by as many as three to one", 'length': 84}
However I just want the fact.
How do I get rid of the 'fact:' at the front and 'length:' at the back?
What you want is to access the key in the python dict you made with the json.loads call. We actually don't need the json library as requests can read and deserialize JSON itself.
This code also checks if the response was OK and fails with informative error message. It follows PEP 20 – The Zen of Python.
import requests
def get_fact():
# Get the facts dictionary in a JSON serialized form.
cat_fact_response = requests.get("https://catfact.ninja/fact?max_length=140")
# Let the response raise the exception if something bad happened to the cat facts server connection.
cat_fact_response.raise_for_status()
# Deserialize the json (make a Python dict from the text we got). requests can do that on it's own:
cat_fact_dict = cat_fact_response.json()
# Access the fact from the json from the dictionary
return cat_fact_dict['fact']
print(get_fact())
When called you get following output as wanted:
# python3 script.py
The cat's tail is used to maintain balance.
Short answer:
you need to use either get_fact()['fact'] or get_fact().get('fact'). The former will throw an exception if fact doesn't exist whereas the latter will return None.
Why:
In your code sample you fetch some json data, and then print out the entire bit of json. When you parse json, the output is a key/value map called a dictionary (or map or object in other languages). The dictionary in this case contains two keys: fact and length. If you only one want of the values, then you need to tell python that you want only a single value -- fact in this case.
Remember though: this wouldn't apply to every json object you read. Not every one is going to have a fact key.
What you are returning in get_fact is a complete JSON object which you are then printing.
To get just its property fact (without the length) use a reference to that key or property like:
return json_data["fact"]
Below is also a link to a tutorial on using JSON in Python:
w3schools: Python JSON
To extract fact field from the response, use:
import requests
import json
def get_fact():
catFact = requests.get("https://catfact.ninja/fact?max_length=140")
json_data = json.loads(catFact.text)
return json_data['fact'] # <- HERE
print(get_fact())
Output:
Cats have "nine lives" thanks to a flexible spine and powerful leg and back muscles
Note: you don't need json module here, use json() method of Response instance returned by requests:
import requests
def get_fact():
catFact = requests.get("https://catfact.ninja/fact?max_length=140").json()
return catFact['fact']
print(get_fact())

String indices must be integers Giphy

I'm trying to get url from object data, but it isn't right. This program has stopped on line 4. Code is under.
My code:
import requests
gifs = str(requests.get("https://api.giphy.com/v1/gifs/random?
api_key=APIKEY"))
dump = json.dumps(gifs)
json.loads(dump['data']['url'])
Your description is not clear enough. You expect to read a json and select a field that brings you something?
I recommend you check this section of requests quickstart guide this i suspect you want to read the data to json and extract from some fields.
Maybe something like this might help:
r = requests.get('http://whatever.com')
url = r.json()['url']

Python 3 - ascii to hex for hmac

I'm having an issue and not quite sure how to explain it but I will try my best.
So I'm attempting to authenticate with an API which requires grabbing a private key that is provided by the website in hex representation (e.g. an example token is "665c20b3c4517e025311160b7fec3fdb9b4d091f142d308c568d0eec4745f569") and decode to ascii to create a keyed hash so I may pass it in an http header which is part of the authentication process.
When it comes to python2 I can simply
import hashlib
import hmac
import requests
headers = {
"custom header": hmac.new("665c20b3c4517e025311160b7fec3fdb9b4d091f142d308c568d0eec4745f569".decode("hex"),
msg="whatever",
digestmod=hashlib.sha256).hexdigest()
}
requests.get("my url", headers=headers)
However, I cannot get this working in python3 despite several hours of googling, various SO posts and looking at the official docs for hmac.
This seems to stem from the differences between how python2 and 3 handle strings.
In python2 running "665c20b3c4517e025311160b7fec3fdb9b4d091f142d308c568d0eec4745f569".decode("hex") returns this string of characters "f\ ��Q~S�?ۛM -0�V��GE�i" which is passed to hmac.new()
Somethings I have tried in Python3 after searching around:
bytes.fromhex('665c20b3c4517e025311160b7fec3fdb9b4d091f142d308c568d0eec4745f569').decode('utf-8')
bytes.fromhex('665c20b3c4517e025311160b7fec3fdb9b4d091f142d308c568d0eec4745f569').decode('ascii')
import binascii
binascii.unhexlify(b"665c20b3c4517e025311160b7fec3fdb9b4d091f142d308c568d0eec4745f569")
But these all error or output different returns that hmac.new() won't accept. I'm assuming there's a simple fix that I'm just ignorant on since I'm not very knowledgeable about the nuances of how p2 and p3 handle strings.
One of your attempts is correct:
In [1]: import binascii
...: binascii.unhexlify(b"665c20b3c4517e025311160b7fec3fdb9b4d091f142d308c568d0eec4745f569")
...:
Out[1]: b'f\\ \xb3\xc4Q~\x02S\x11\x16\x0b\x7f\xec?\xdb\x9bM\t\x1f\x14-0\x8cV\x8d\x0e\xecGE\xf5i'
If you get a wrong result from hmac afterwards, you can post a question about that specific scenario, with some examples comparing python2/3.
You may be running into a problem with the message itself, which needs to explicitly use bytes, not a string. These two give the same values:
Python 3:
In [10]: hmac.new(binascii.unhexlify(b"665c20b3c4517e025311160b7fec3fdb9b4d091f142d308c568d0eec4745f569"),
...: msg="whatever".encode('utf-8'),
...: digestmod=hashlib.sha256).hexdigest()
Out[10]: '79ca98357629c22a094c67a02638076573ec41d2c5ce8996435656f8488552d0'
Python 2:
>>> hmac.new("665c20b3c4517e025311160b7fec3fdb9b4d091f142d308c568d0eec4745f569".decode("hex"),
... msg="whatever",
... digestmod=hashlib.sha256).hexdigest()
'79ca98357629c22a094c67a02638076573ec41d2c5ce8996435656f8488552d0'

Python Requests - add text at the beginning of query string

When sending data through python-requests a GET request, I have a need to specifically add something at the beginning of the query string. I have tried passing the data in through dicts and json strings with no luck.
The request as it appears when produced by requests:
/apply/.../explain?%7B%22......
The request as it appears when produced by their interactive API documentation (Swagger):
/apply/.../explain?record=%7B%22....
Where the key-value pairs of my data follow the excerpt above.
Ultimately, I think the missing piece is the record= that gets produced by their documentation. It is the only piece that is different from what is produced by Requests.
At the moment I've got it set up something like this:
import requests
s = requests.Session()
s.auth = requests.auth.HTTPBasicAuth(username,password)
s.verify = certificate_path
# with data below being a dictionary of the values I need to pass.
r = s.get(url,data=data)
I am trying to include an image of the documentation below, but don't yet have enough reputation to do so:
apply/model/explain documentation
'GET' requests don't have data, that's for 'POST' and friends.
You can send the query string arguments using params kwarg instead:
>>> params = {'record': '{"'}
>>> response = requests.get('http://www.example.com/explain', params=params)
>>> response.request.url
'http://www.example.com/explain?record=%7B%22'
From the comments i felt the need to explain this.
http://example.com/sth?key=value&anotherkey=anothervalue
Let's assume you have a url like the above in order to call with python requests you only have to write
response = requests.get('http://example.com/sth', params={
'key':'value',
'anotherkey':'anothervalue'
})
Have in mind that if your value or your keys have any special character in them they will be escaped thats the reason for the %7B%2 part of url in your question.

python json unicode - how do I eval using javascript

Really spent a lot of time searching for this. Please need some help.
I am trying to add multilingual feature to my web app framework. For this I am unable to send non ascii characters as JSON. Here is what I am doing
Here is what I get from the database
'\xe0\xa4\xa4\xe0\xa5\x87\xe0\xa4\xb8\xe0\xa5\x8d\xe0\xa4\xa4'
which when I print gives me which is okay
तेस्त
I make the response object
response = {'a':'\xe0\xa4\xa4\xe0\xa5\x87\xe0\xa4\xb8\xe0\xa5\x8d\xe0\xa4\xa4'}
Send the repsonse
import json
sys.stdout.write(json.dumps(response))
This is what it prints
Returns u'{"a": "\u0924\u0947\u0938\u094d\u0924"}'
Any help, pointers will be welcome
Thanks!
Rushabh
Is this your desired output (see ensure_ascii argument for json.dumps)?
sys.stdout.write(json.dumps(response, ensure_ascii=False))
{"a": "तेस्त"}

Categories