get_user_details.py
import requestsfrom pprint import pprint# github usernameusername = "x4nth055"# url to requesturl = f"https://api.github.com/users/{username}"# make the request and return the jsonuser_data = requests.get(url).json()# pretty print JSON datapprint(user_data)# get namename = user_data["name"]# get blog url if there isblog = user_data["blog"]# extract locationlocation = user_data["location"]# get email address that is publicly availableemail = user_data["email"]# number of public repositoriespublic_repos = user_data["public_repos"]# get number of public gistspublic_gists = user_data["public_gists"]# number of followersfollowers = user_data["followers"]# number of followingfollowing = user_data["following"]# date of account creationdate_created = user_data["created_at"]# date of account last updatedate_updated = user_data["updated_at"]# urlsfollowers_url = user_data["followers_url"]following_url = user_data["following_url"]# print allprint("User:", username)print("Name:", name)print("Blog:", blog)print("Location:", location)print("Email:", email)print("Total Public repositories:", public_repos)print("Total Public Gists:", public_gists)print("Total followers:", followers)print("Total following:", following)print("Date Created:", date_created)print("Date Updated:", date_updated)
get_user_repositories.py
import base64import githubimport sysimport os# make a directory to save the Python filesif not os.path.exists("python-files"): os.mkdir("python-files")def print_repo(repo): # repository full name print("Full name:", repo.full_name) # repository description print("Description:", repo.description) # the date of when the repo was created print("Date created:", repo.created_at) # the date of the last git push print("Date of last push:", repo.pushed_at) # home website (if available) print("Home Page:", repo.homepage) # programming language print("Language:", repo.language) # number of forks print("Number of forks:", repo.forks) # number of stars print("Number of stars:", repo.stargazers_count) print("-"*50) # repository content (files & directories) print("Contents:") try: for content in repo.get_contents(""): # check if it's a Python file if content.path.endswith(".py"): # save the file filename = os.path.join("python-files", f"{repo.full_name.replace('/', '-')}-{content.path}") with open(filename, "wb") as f: f.write(content.decoded_content) print(content) # repo license print("License:", base64.b64decode(repo.get_license().content.encode()).decode()) except Exception as e: print("Error:", e) # Github username from the command lineusername = sys.argv[1]# pygithub objectg = github.Github()# get that user by usernameuser = g.get_user(username)# iterate over all public repositoriesfor repo in user.get_repos(): print_repo(repo) print("="*100)
search_github_repositories.py
import githubimport base64def print_repo(repo): # repository full name print("Full name:", repo.full_name) # repository description print("Description:", repo.description) # the date of when the repo was created print("Date created:", repo.created_at) # the date of the last git push print("Date of last push:", repo.pushed_at) # home website (if available) print("Home Page:", repo.homepage) # programming language print("Language:", repo.language) # number of forks print("Number of forks:", repo.forks) # number of stars print("Number of stars:", repo.stargazers_count) print("-"*50) # repository content (files & directories) print("Contents:") try: for content in repo.get_contents(""): print(content) except github.GithubException as e: print("Error:", e) try: # repo license print("License:", base64.b64decode(repo.get_license().content.encode()).decode()) except: pass# your github account credentialsusername = "username"password = "password"# initialize github objectg = github.Github(username, password)# or use public version# g = Github()# search repositories by namefor repo in g.search_repositories("pythoncode tutorials"): # print repository details print_repo(repo) print("="*100)print("="*100)print("="*100)# search by programming languagefor i, repo in enumerate(g.search_repositories("language:python")): print_repo(repo) print("="*100) if i == 9: break
creating_and_deleting_files.py
from github import Github# your github account credentialsusername = "username"password = "password"# initialize github objectg = Github(username, password)# searching for my repositoryrepo = g.search_repositories("pythoncode tutorials")[0]# create a file and commit n pushrepo.create_file("test.txt", "commit message", "content of the file")# delete that created filecontents = repo.get_contents("test.txt")repo.delete_file(contents.path, "remove test.txt", contents.sha)