-
Notifications
You must be signed in to change notification settings - Fork 0
/
github.py
110 lines (68 loc) · 3.24 KB
/
github.py
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
109
110
from generalpackager import PACKAGER_GITHUB_API
import requests
import json
import re
class GitHub:
""" Tools to interface a GitHub Repository. """
def __init__(self, name, owner=None):
if owner is None:
owner = "ManderaGeneral"
self.name = name
self.owner = owner
self.url = self.get_url(name=self.name, owner=self.owner)
# if not self.is_url_functioning():
# raise AssertionError(f"Url for {self.name} is not functioning.")
@classmethod
def is_creatable(cls, name, owner):
""" Return whether this API can be created. """
return requests.get(url=cls.get_url(name=name, owner=owner)).status_code == 200
@staticmethod
def get_url(name, owner):
""" Get static URL from owner and name. """
return f"https://github.com/{owner}/{name}"
def is_url_functioning(self, url=None):
""" Checks name, owner and token all in one. """
return self._request(url=url).status_code == 200
def api_url(self, endpoint=None):
""" Get URL from owner, name and enpoint. """
return "/".join(("https://api.github.com", "repos", self.owner, self.name) + ((endpoint, ) if endpoint else ()))
def get_website(self):
""" Get website specified in repository details.
:rtype: list[str] """
return self._request(method="get").json()["homepage"]
def set_website(self, website):
""" Set a website for the GitHub repository. """
return self._request(method="patch", name=self.name, homepage=website)
def get_topics(self):
""" Get a list of topics in the GitHub repository.
:rtype: list[str] """
return self._request(method="get", endpoint="topics").json()["names"]
def set_topics(self, *topics):
""" Set topics for the GitHub repository.
:param str topics: """
return self._request(method="put", endpoint="topics", names=topics)
def get_description(self):
""" Get a string of description in the GitHub repository.
:rtype: list[str] """
return self._request(method="get").json()["description"]
def set_description(self, description):
""" Set a description for the GitHub repository. """
return self._request(method="patch", name=self.name, description=description)
def _request(self, method="get", url=None, endpoint=None, **data):
""" :rtype: requests.Response """
method = getattr(requests, method.lower())
kwargs = {
"headers": {"Accept": "application/vnd.github.mercy-preview+json"},
"auth": (self.owner, PACKAGER_GITHUB_API.value),
}
if data:
kwargs["data"] = json.dumps(data)
if url is None:
url = self.api_url(endpoint=endpoint)
return method(url=url, **kwargs)
@staticmethod
def get_users_packages(user=None):
""" Get a set of a user's packages' names on GitHub. """
if user is None:
user = "ManderaGeneral"
return set(re.findall(f'"/{user}/([a-z]*)"', requests.get(f"https://github.com/{user}?tab=repositories").text))