Skip to content

Commit 2c8908d

Browse files
committed
add github views, clones stat
1 parent 90f80ba commit 2c8908d

File tree

1 file changed

+63
-38
lines changed

1 file changed

+63
-38
lines changed

src/writegithubstat/githubstat.py

Lines changed: 63 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ def __init__(self, owner: str, repo: str, token: str) -> None:
1313
self._owner = owner
1414
self._repo = repo
1515
self._header = GithubAuth._get_auth_header(token)
16-
self._repo_id = GithubAuth._get_repo_id(self._owner, self._repo, self._header)
1716

1817
@staticmethod
1918
def _get_auth_header(token: str) -> dict:
@@ -23,18 +22,6 @@ def _get_auth_header(token: str) -> dict:
2322
}
2423
return auth_header
2524

26-
@staticmethod
27-
def _get_repo_id(owner: str, repo: str, auth_header: str) -> str:
28-
url = f"https://api.github.com/repos/{owner}/{repo}"
29-
response = requests.get(url, headers=auth_header)
30-
if response.status_code == 200:
31-
repository = response.json()
32-
return repository["id"]
33-
else:
34-
raise requests.HTTPError(
35-
f"Request failed with status code {response.status_code}: {response.text}"
36-
)
37-
3825
@property
3926
def owner(self) -> str:
4027
return self._owner
@@ -43,22 +30,19 @@ def owner(self) -> str:
4330
def repo(self) -> str:
4431
return self._repo
4532

46-
@property
47-
def repo_id(self) -> str:
48-
return self._repo_id
49-
5033
@property
5134
def header(self) -> dict:
5235
return self._header
5336

5437

5538
class GithubStatType(ABC):
56-
def __init__(self, auth: GithubAuth) -> None:
57-
self._auth = auth
39+
def __init__(self, owner: str, repo: str) -> None:
40+
self._owner = owner
41+
self._repo = repo
5842

5943
@property
6044
@abstractmethod
61-
def url(self):
45+
def urls(self):
6246
pass
6347

6448
@property
@@ -72,14 +56,16 @@ def measures(self):
7256
pass
7357

7458
@staticmethod
75-
def process_stat(data):
59+
def process_stat(responses):
7660
pass
7761

7862

7963
class Referrers(GithubStatType):
8064
@property
81-
def url(self):
82-
return f"https://api.github.com/repositories/{self._auth.repo_id}/traffic/popular/referrers"
65+
def urls(self):
66+
return [
67+
f"https://api.github.com/repos/{self._owner}/{self._repo}/traffic/popular/referrers"
68+
]
8369

8470
@property
8571
def dimensions(self):
@@ -90,15 +76,18 @@ def measures(self):
9076
return ["count", "uniques"]
9177

9278
@staticmethod
93-
def process_stat(data):
79+
def process_stat(responses):
80+
data = responses[0]
9481
df = pd.DataFrame(data)
9582
return df
9683

9784

9885
class Paths(GithubStatType):
9986
@property
100-
def url(self):
101-
return f"https://api.github.com/repositories/{self._auth.repo_id}/traffic/popular/paths"
87+
def urls(self):
88+
return [
89+
f"https://api.github.com/repos/{self._owner}/{self._repo}/traffic/popular/paths"
90+
]
10291

10392
@property
10493
def dimensions(self):
@@ -109,15 +98,16 @@ def measures(self):
10998
return ["count", "uniques"]
11099

111100
@staticmethod
112-
def process_stat(data):
101+
def process_stat(responses):
102+
data = responses[0]
113103
df = pd.DataFrame(data)
114104
return df
115105

116106

117-
class Cumulative(GithubStatType):
107+
class StarsForks(GithubStatType):
118108
@property
119-
def url(self):
120-
return f"https://api.github.com/repos/{self._auth.owner}/{self._auth.repo}"
109+
def urls(self):
110+
return [f"https://api.github.com/repos/{self._owner}/{self._repo}"]
121111

122112
@property
123113
def dimensions(self):
@@ -128,23 +118,58 @@ def measures(self):
128118
return ["stars", "forks"]
129119

130120
@staticmethod
131-
def process_stat(data):
121+
def process_stat(responses):
122+
data = responses[0]
132123
stars = data["stargazers_count"]
133124
forks = data["forks_count"]
134125
df = pd.DataFrame({"stars": [stars], "forks": [forks]})
135126
return df
136127

137128

129+
class ViewsClones(GithubStatType):
130+
@property
131+
def urls(self):
132+
return [
133+
f"https://api.github.com/repos/{self._owner}/{self._repo}/traffic/views",
134+
f"https://api.github.com/repos/{self._owner}/{self._repo}/traffic/clones",
135+
]
136+
137+
@property
138+
def dimensions(self):
139+
return []
140+
141+
@property
142+
def measures(self):
143+
return []
144+
145+
@staticmethod
146+
def process_stat(responses):
147+
views = responses[0]["views"][-1]
148+
clones = responses[1]["clones"][-1]
149+
df = pd.DataFrame(
150+
{
151+
"views_total": [views["count"]],
152+
"views_unique": [views["uniques"]],
153+
"clones_total": [clones["count"]],
154+
"clones_unique": [clones["uniques"]],
155+
}
156+
)
157+
return df
158+
159+
138160
class _GithubStat:
139161
@staticmethod
140162
def _get_stat(stat_type: GithubStatType, auth_header: dict) -> pd.DataFrame:
141-
response = requests.get(stat_type.url, headers=auth_header)
142-
if response.status_code == 200:
143-
return stat_type.process_stat(response.json())
144-
else:
145-
raise requests.HTTPError(
146-
f"Request failed with status code {response.status_code}: {response.text}"
147-
)
163+
responses = []
164+
for url in stat_type.urls:
165+
response = requests.get(url, headers=auth_header)
166+
if response.status_code == 200:
167+
responses.append(response.json())
168+
else:
169+
raise requests.HTTPError(
170+
f"Request failed with status code {response.status_code}: {response.text}"
171+
)
172+
return stat_type.process_stat(responses)
148173

149174

150175
class WriteGithubStat:

0 commit comments

Comments
 (0)