Skip to content

Commit 5235cff

Browse files
committed
update github api tutorial
1 parent aac8641 commit 5235cff

File tree

2 files changed

+24
-10
lines changed

2 files changed

+24
-10
lines changed

Diff for: general/github-api/get_user_repositories.py

+17-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
import base64
2-
from github import Github
2+
import github
33
import sys
4+
import os
5+
6+
# make a directory to save the Python files
7+
if not os.path.exists("python-files"):
8+
os.mkdir("python-files")
49

510

611
def print_repo(repo):
@@ -23,19 +28,25 @@ def print_repo(repo):
2328
print("-"*50)
2429
# repository content (files & directories)
2530
print("Contents:")
26-
for content in repo.get_contents(""):
27-
print(content)
2831
try:
32+
for content in repo.get_contents(""):
33+
# check if it's a Python file
34+
if content.path.endswith(".py"):
35+
# save the file
36+
filename = os.path.join("python-files", f"{repo.full_name.replace('/', '-')}-{content.path}")
37+
with open(filename, "wb") as f:
38+
f.write(content.decoded_content)
39+
print(content)
2940
# repo license
3041
print("License:", base64.b64decode(repo.get_license().content.encode()).decode())
31-
except:
32-
pass
42+
except Exception as e:
43+
print("Error:", e)
3344

3445

3546
# Github username from the command line
3647
username = sys.argv[1]
3748
# pygithub object
38-
g = Github()
49+
g = github.Github()
3950
# get that user by username
4051
user = g.get_user(username)
4152
# iterate over all public repositories

Diff for: general/github-api/search_github_repositories.py

+7-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from github import Github
1+
import github
22
import base64
33

44
def print_repo(repo):
@@ -21,8 +21,11 @@ def print_repo(repo):
2121
print("-"*50)
2222
# repository content (files & directories)
2323
print("Contents:")
24-
for content in repo.get_contents(""):
25-
print(content)
24+
try:
25+
for content in repo.get_contents(""):
26+
print(content)
27+
except github.GithubException as e:
28+
print("Error:", e)
2629
try:
2730
# repo license
2831
print("License:", base64.b64decode(repo.get_license().content.encode()).decode())
@@ -33,7 +36,7 @@ def print_repo(repo):
3336
username = "username"
3437
password = "password"
3538
# initialize github object
36-
g = Github(username, password)
39+
g = github.Github(username, password)
3740
# or use public version
3841
# g = Github()
3942

0 commit comments

Comments
 (0)