feat: Gitea started showing logs in a new format, this patch accommodates it

new-time-format
Aravinth Manivannan 2022-12-31 02:24:42 +05:30
parent 8532043620
commit 542a2ab78c
Signed by: realaravinth
GPG Key ID: AD9F0F08E855ED88
1 changed files with 24 additions and 9 deletions

View File

@ -16,6 +16,7 @@ class TimeSpent:
name: str name: str
comment_id: str comment_id: str
issue: str issue: str
task: str
repo: str repo: str
issue: str issue: str
date: str date: str
@ -54,7 +55,10 @@ class Gitea:
else: else:
logs[time.nick]["mins"] = int(time.mins) logs[time.nick]["mins"] = int(time.mins)
else: else:
logs[time.nick] = {"hours": int(time.hours), "mins": int(time.mins)} logs[time.nick] = {
"hours": int(time.hours),
"mins": int(time.mins),
}
for nick in logs: for nick in logs:
time = logs[nick] time = logs[nick]
@ -64,7 +68,7 @@ class Gitea:
def write_csv(self): def write_csv(self):
w = None w = None
print('writing to times.csv') print("writing to times.csv")
with open("times.csv", "w+", encoding="utf-8") as f: with open("times.csv", "w+", encoding="utf-8") as f:
for repo in self.times: for repo in self.times:
for issue in self.times[repo]: for issue in self.times[repo]:
@ -157,14 +161,13 @@ class Gitea:
for issue in issues: for issue in issues:
times = [] times = []
url = issue["html_url"] url = issue["html_url"]
task = issue["title"]
resp = requests.get(url) resp = requests.get(url)
contents = resp.text contents = resp.text
soup = BeautifulSoup(contents, "html.parser") soup = BeautifulSoup(contents, "html.parser")
divs = soup.find_all("div", attrs={"class": "timeline-item event"}) divs = soup.find_all("div", attrs={"class": "timeline-item event"})
for div in divs: for div in divs:
for a in div.find_all( for a in div.find_all("a", attrs={"class": "author"}):
"a", attrs={"class": "author"}
):
if "added spent time" in a.parent.text: if "added spent time" in a.parent.text:
for s in a.parent.find_all( for s in a.parent.find_all(
"span", attrs={"class": "time-since"} "span", attrs={"class": "time-since"}
@ -178,13 +181,24 @@ class Gitea:
.find_all("span")[0] .find_all("span")[0]
.text .text
) )
if "day" in time:
continue
if "h" in time: if "h" in time:
hours = int(time.split("h")[0]) if "hours" in time:
hours = int(time.split("hours")[0])
elif "hour" in time:
hours = int(time.split("hour")[0])
else:
hours = int(time.split("h")[0])
else: else:
hours = 0 hours = 0
if "min" in time: if "min" in time:
splits = time.split("min")[0] splits = time.split("min")[0]
if "h" in splits: if "hours" in time:
mins = int(splits.split("hours")[1])
elif "hour" in time:
mins = int(splits.split("hour")[1])
elif "h" in time:
mins = int(splits.split("h")[1]) mins = int(splits.split("h")[1])
else: else:
mins = int(splits) mins = int(splits)
@ -203,6 +217,7 @@ class Gitea:
hours=hours, hours=hours,
repo=repo, repo=repo,
issue=url, issue=url,
task=task,
url=f"{url}#{comment_id}", url=f"{url}#{comment_id}",
) )
) )
@ -210,9 +225,9 @@ class Gitea:
repo_times.append(times) repo_times.append(times)
total_times[repo] = repo_times total_times[repo] = repo_times
print(f"Found {num} log events in {self.org}") print(f"Found {num} log events in {self.org}")
return total_times return total_times
if __name__ == "__main__": if __name__ == "__main__":
g = Gitea(host="https://gitea.hostea.org", org="Hostea") g = Gitea(host="https://gitea.gna.org", org="gna")