-
-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathgithub.py
More file actions
75 lines (63 loc) · 2.59 KB
/
Copy pathgithub.py
File metadata and controls
75 lines (63 loc) · 2.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# This code is made by MRayan Asim
# Packages needed:
# pip install requests
import requests
# Default timeout for HTTP requests (seconds)
DEFAULT_TIMEOUT = 10
def analyze_github_repository(owner, repo, timeout=DEFAULT_TIMEOUT):
"""
Analyze a GitHub repository and display its statistics.
Args:
owner: GitHub repository owner
repo: Repository name
timeout: Request timeout in seconds (default: 10)
"""
# API endpoint for GitHub repository
repo_url = f"https://api.github.com/repos/{owner}/{repo}"
try:
# Send GET request to fetch repository data with timeout
response = requests.get(repo_url, timeout=timeout)
if response.status_code == 200:
# Parse JSON response
repository = response.json()
# Extract desired information
name = repository.get("name")
description = repository.get("description")
stars = repository.get("stargazers_count")
forks = repository.get("forks_count")
watchers = repository.get("subscribers_count")
# API endpoint for repository views
views_url = f"https://api.github.com/repos/{owner}/{repo}/traffic/views"
try:
# Send GET request to fetch repository views with timeout
views_response = requests.get(views_url, timeout=timeout)
if views_response.status_code == 200:
views_data = views_response.json()
views = views_data.get("count")
else:
views = "N/A"
except requests.Timeout:
views = "Timeout"
except requests.RequestException:
views = "N/A"
# Print repository information
print(f"Repository: {name}")
print(f"Description: {description}")
print(f"Stars: {stars}")
print(f"Forks: {forks}")
print(f"Watchers: {watchers}")
print(f"Views: {views}")
else:
print("Error: Repository not found or API request failed")
print(f"Status code: {response.status_code}")
except requests.Timeout:
print(f"Error: Connection timed out after {timeout} seconds.")
except requests.ConnectionError:
print("Error: Unable to connect to GitHub API.")
except requests.RequestException as e:
print(f"Error: {e}")
# usage
if __name__ == "__main__":
owner = input("Enter the name of the owner: ")
repo = input("Enter the name of the repository: ")
analyze_github_repository(owner, repo)