forked from pythonpe/python.pe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauthors.py
33 lines (28 loc) · 1.01 KB
/
authors.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
import os
from typing import Tuple
from urllib.request import Request, urlopen
REPO_OWNER = "pythonpe"
REPO = "pythonpe-blog"
BRANCH = "main"
AUTHORS_FILENAME = "AUTHORS"
HEADERS = {"Accept": "application/vnd.github.VERSION.raw"}
URL = (
f"https://api.github.com/repos/{REPO_OWNER}/{REPO}/contents/"
f"{AUTHORS_FILENAME}?ref={BRANCH}"
)
AUTHORS_FILENAME = "AUTHORS"
AUTHORS_FILEPATH = os.path.join(os.path.dirname(__file__), AUTHORS_FILENAME)
def get_authors() -> Tuple[str, str]:
author_lines = set()
with open(AUTHORS_FILEPATH, "r") as fd:
author_lines = {line for line in fd.readlines()}
req = Request(URL, headers=HEADERS)
with urlopen(req) as fd:
author_lines.update({line.decode("utf-8") for line in fd.readlines()})
for author_line in author_lines:
tokens = author_line.split("<")
email = tokens[1].strip()[:-1]
tokens = tokens[0].strip().split("(")
name = tokens[0]
nickname = tokens[1][:-1]
yield (nickname, name, f"mailto:{email}")