Skip to content

Commit 6f31ddb

Browse files
committedApr 3, 2020
added using github api tutorial
1 parent f251143 commit 6f31ddb

6 files changed

+168
-0
lines changed
 

‎general/github-api/README.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# [How to Use Github API in Python](https://www.thepythoncode.com/article/using-github-api-in-python)
2+
To be able to execute this:
3+
`pip3 install -r requirements.txt`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from github import Github
2+
3+
# your github account credentials
4+
username = "username"
5+
password = "password"
6+
# initialize github object
7+
g = Github(username, password)
8+
9+
# searching for my repository
10+
repo = g.search_repositories("pythoncode tutorials")[0]
11+
12+
# create a file and commit n push
13+
repo.create_file("test.txt", "commit message", "content of the file")
14+
15+
# delete that created file
16+
contents = repo.get_contents("test.txt")
17+
repo.delete_file(contents.path, "remove test.txt", contents.sha)
+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import requests
2+
from pprint import pprint
3+
4+
# github username
5+
username = "x4nth055"
6+
# url to request
7+
url = f"https://api.github.com/users/{username}"
8+
# make the request and return the json
9+
user_data = requests.get(url).json()
10+
# pretty print JSON data
11+
pprint(user_data)
12+
# get name
13+
name = user_data["name"]
14+
# get blog url if there is
15+
blog = user_data["blog"]
16+
# extract location
17+
location = user_data["location"]
18+
# get email address that is publicly available
19+
email = user_data["email"]
20+
# number of public repositories
21+
public_repos = user_data["public_repos"]
22+
# get number of public gists
23+
public_gists = user_data["public_gists"]
24+
# number of followers
25+
followers = user_data["followers"]
26+
# number of following
27+
following = user_data["following"]
28+
# date of account creation
29+
date_created = user_data["created_at"]
30+
# date of account last update
31+
date_updated = user_data["updated_at"]
32+
# urls
33+
followers_url = user_data["followers_url"]
34+
following_url = user_data["following_url"]
35+
36+
# print all
37+
print("User:", username)
38+
print("Name:", name)
39+
print("Blog:", blog)
40+
print("Location:", location)
41+
print("Email:", email)
42+
print("Total Public repositories:", public_repos)
43+
print("Total Public Gists:", public_gists)
44+
print("Total followers:", followers)
45+
print("Total following:", following)
46+
print("Date Created:", date_created)
47+
print("Date Updated:", date_updated)
48+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import base64
2+
from github import Github
3+
import sys
4+
5+
6+
def print_repo(repo):
7+
# repository full name
8+
print("Full name:", repo.full_name)
9+
# repository description
10+
print("Description:", repo.description)
11+
# the date of when the repo was created
12+
print("Date created:", repo.created_at)
13+
# the date of the last git push
14+
print("Date of last push:", repo.pushed_at)
15+
# home website (if available)
16+
print("Home Page:", repo.homepage)
17+
# programming language
18+
print("Language:", repo.language)
19+
# number of forks
20+
print("Number of forks:", repo.forks)
21+
# number of stars
22+
print("Number of stars:", repo.stargazers_count)
23+
print("-"*50)
24+
# repository content (files & directories)
25+
print("Contents:")
26+
for content in repo.get_contents(""):
27+
print(content)
28+
try:
29+
# repo license
30+
print("License:", base64.b64decode(repo.get_license().content.encode()).decode())
31+
except:
32+
pass
33+
34+
35+
# Github username from the command line
36+
username = sys.argv[1]
37+
# pygithub object
38+
g = Github()
39+
# get that user by username
40+
user = g.get_user(username)
41+
# iterate over all public repositories
42+
for repo in user.get_repos():
43+
print_repo(repo)
44+
print("="*100)

‎general/github-api/requirements.txt

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
requests
2+
PyGithub
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
from github import Github
2+
import base64
3+
4+
def print_repo(repo):
5+
# repository full name
6+
print("Full name:", repo.full_name)
7+
# repository description
8+
print("Description:", repo.description)
9+
# the date of when the repo was created
10+
print("Date created:", repo.created_at)
11+
# the date of the last git push
12+
print("Date of last push:", repo.pushed_at)
13+
# home website (if available)
14+
print("Home Page:", repo.homepage)
15+
# programming language
16+
print("Language:", repo.language)
17+
# number of forks
18+
print("Number of forks:", repo.forks)
19+
# number of stars
20+
print("Number of stars:", repo.stargazers_count)
21+
print("-"*50)
22+
# repository content (files & directories)
23+
print("Contents:")
24+
for content in repo.get_contents(""):
25+
print(content)
26+
try:
27+
# repo license
28+
print("License:", base64.b64decode(repo.get_license().content.encode()).decode())
29+
except:
30+
pass
31+
32+
# your github account credentials
33+
username = "username"
34+
password = "password"
35+
# initialize github object
36+
g = Github(username, password)
37+
# or use public version
38+
# g = Github()
39+
40+
# search repositories by name
41+
for repo in g.search_repositories("pythoncode tutorials"):
42+
# print repository details
43+
print_repo(repo)
44+
print("="*100)
45+
46+
print("="*100)
47+
print("="*100)
48+
49+
# search by programming language
50+
for i, repo in enumerate(g.search_repositories("language:python")):
51+
print_repo(repo)
52+
print("="*100)
53+
if i == 9:
54+
break

0 commit comments

Comments
 (0)
Please sign in to comment.