This is a demonstration of how to use requests in Python. In this example, we are using requests to access a file using the GitHub API. The resulting JSON object contains a base64-encoded attribute (content), which is decoded, tuned into a list of lines. Lastly, we select some lines of interest.
This example is used in our work on software metrics and the literal include service (for writing projects).
In [1]:
import requests
import base64
In [2]:
r = requests.get("https://api.github.com/repos/gkthiruvathukal/st-hec/contents/hydra/dataserver.py")
In [3]:
print(r.status_code)
In [4]:
r.json().keys()
Out[4]:
In [5]:
b64data = r.json().get('content')
lines = base64.b64decode(b64data).decode("utf-8").split('\n')
selected_lines = lines[10:30]
print("\n".join(selected_lines))
This shows how to base64 encode text. b64encode() expects bytes and returns bytes (b).
In [6]:
text = "How to encode some text"
bytes = text.encode("UTF-8")
encoded = base64.b64encode(bytes)
encoded
Out[6]:
To get the string representation of bytes, use decode().
In [7]:
encoded.decode("UTF-8")
Out[7]: