Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

python get github file content

import requests
import base64
import json


def constructURL(user = "404",repo_name= "404",path_to_file= "404",url= "404"):
  url = url.replace("{user}",user)
  url = url.replace("{repo_name}",repo_name)
  url = url.replace("{path_to_file}",path_to_file)
  return url

user = '<provide value>'
repo_name = '<provide value>'
path_to_file = '<provide value>'
json_url ='https://api.github.com/repos/{user}/{repo_name}/contents/{path_to_file}'

json_url = constructURL(user,repo_name,path_to_file,json_url) #forms the correct URL
response = requests.get(json_url) #get data from json file located at specified URL 

if response.status_code == requests.codes.ok:
    jsonResponse = response.json()  # the response is a JSON
    #the JSON is encoded in base 64, hence decode it
    content = base64.b64decode(jsonResponse['content'])
    #convert the byte stream to string
    jsonString = content.decode('utf-8')
    finalJson = json.loads(jsonString)
else:
    print('Content was not found.')

for key, value in finalJson.items():
    print("The key and value are ({}) = ({})".format(key, value))
Comment

PREVIOUS NEXT
Code Example
Python :: delete occurrences of an element if it occurs more than n times python 
Python :: python crop string 
Python :: lamda python 
Python :: How to check palindrom in python 
Python :: python string cut left 
Python :: series.string.split expand 
Python :: python turtle set screen size 
Python :: how to make a python terminal 
Python :: python save button 
Python :: gurobi python example 
Python :: numpy create array with values in range 
Python :: isinstance function python 
Python :: openai gym random action 
Python :: ordenar lista decrescente python 
Python :: encrypt password with sha512 + python 
Python :: seaborn plot histogram for all columns 
Python :: pandas do not display index 
Python :: check file existence python 
Python :: break in python 
Python :: python crear dataframe 
Python :: not equal python 
Python :: tkinter treeview clear 
Python :: virtualenv 
Python :: installing pip in pytho 
Python :: random.randint 
Python :: create a 2d array in python 
Python :: Setting spacing between ticks in matplotlib 
Python :: python argsort a list 
Python :: odoo order by xml rpc 
Python :: os.move file 
ADD CONTENT
Topic
Content
Source link
Name
8+1 =