-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithub_functions.py
More file actions
108 lines (85 loc) · 2.9 KB
/
github_functions.py
File metadata and controls
108 lines (85 loc) · 2.9 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import base64
import requests
import os
from dotenv import load_dotenv
from github import Github
user = 'Anish-CodeDev'
load_dotenv()
token = os.environ['GITHUB_TOKEN']
class Readme:
def __init__(self,repo):
self.repo = repo
self.url = f"https://api.github.com/repos/{user}/{repo}/contents/README.md"
self.data = {}
self.headers = {"Authorization": f"token {token}",
"Accept": "application/vnd.github+json"}
self.g = Github(token)
def load_readme(self):
self.headers = {"Authorization":f"token {token}"}
response = requests.get(headers=self.headers,url=self.url)
self.data = response.json()
#print(self.data)
try:
content = base64.b64decode(self.data['content']).decode('utf-8')
#print(content)
return content
except KeyError as e:
return "An error occurred"
def update_readme(self,new_data):
try:
#new_data = base64.b64encode(new_data.encode('utf-8')).decode('utf-8')
repo = self.g.get_repo(user + '/' + self.repo)
readme_file = repo.get_contents("README.md")
print(self.data['sha'])
repo.update_file(
path='README.md',
message="15th Nov 2025",
content=new_data,
sha=readme_file.sha,
branch='main'
)
print("The readme was updated")
except:
print("An error occurred")
def update_about(self,content):
try:
repo = self.g.get_repo(user + '/' + self.repo)
repo.edit(
description=content
)
print("The about was updated")
return "The about section was updated"
except Exception as e:
print(repo.permissions)
print("An error occurred: ",str(e))
return "An error occurred while updating the about section"
def list_repos(username):
page = 1
repos = []
url = f"https://api.github.com/users/{username}/repos"
while True:
response = requests.get(url=url,params={"per_page":100,"page":page})
data = response.json()
if not data:
break
page+=1
print(data)
repos.extend([repo['name'] for repo in data])
return repos
def get_stars(username):
starred = {}
page = 1
url = f"https://api.github.com/users/{username}/repos"
while True:
response = requests.get(url=url,params={"per_page":100,'page':page})
data = response.json()
if not data:
break
page+=1
for repo in data:
starred[repo['name']] = repo['stargazers_count']
return starred
if __name__ == "__main__":
#readme = Readme("multi-agent-dev-marketing-system")
#readme.update_about("test")
print(get_stars("Anish-CodeDev"))