API to manage subscription

Adapt docker part
Improve log
Add a just target
This commit is contained in:
2023-11-12 01:01:05 +01:00
parent fb4ac8ea73
commit 9a8a7042b3
5 changed files with 68 additions and 13 deletions

View File

@@ -8,6 +8,11 @@ from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import requests
from fastapi import FastAPI
SCRIPT_DIR = os.path.dirname(__file__)
CONF_FILE = os.path.join(SCRIPT_DIR, "conf.ini")
TEMPLATE_FILE = os.path.join(SCRIPT_DIR, "template.html")
class EnvInjection(configparser.Interpolation):
@@ -26,15 +31,51 @@ class EnvInjection(configparser.Interpolation):
return env_value if env_value else file_value
def main():
script_dir = os.path.dirname(__file__)
conf_file = os.path.join(script_dir, "conf.ini")
template_file = os.path.join(script_dir, "template.html")
#
# API PARTS
#
app = FastAPI()
@app.get("/subscriptions")
def get_projects():
parser = load_conf()
projects = json.loads(parser["projects"].get("projects"))
return projects
@app.put("/subscriptions")
def put_projects(project: str, author: str | None = None):
# TODO Check if project really exist?
parser = load_conf()
projects = json.loads(parser.get("projects", "projects"))
if author:
project = f"{author}/{project}"
projects.append(project)
# TODO Watch a converter for list: https://stackoverflow.com/a/53274707/1346391
parser.set("projects", "projects", json.dumps(projects, indent=0))
with open("conf.ini", "w", encoding="utf-8") as configfile:
parser.write(configfile)
return project
def load_conf(conf_file=CONF_FILE) -> configparser.ConfigParser:
parser = configparser.ConfigParser(
default_section="config", interpolation=EnvInjection()
)
parser.read(conf_file)
return parser
#
# SCRIPT PART
#
def main():
parser = load_conf()
default_config = parser["config"]
try:
@@ -67,8 +108,10 @@ def main():
return
content = ""
news = []
for new_r in new_releases:
news.append(new_r["project_name"])
content += f"""
<li>{get_html_project_link(new_r)}
: new release
@@ -77,6 +120,7 @@ def main():
(published {convert_date(new_r["published_date"])})</li>"""
for new_p in new_projects:
news.append(new_p["project_name"])
content += f"""
<li>{get_html_project_link(new_p)}
was added to your configuration.
@@ -84,10 +128,11 @@ def main():
{get_html_release_link(new_p)}
(published {convert_date(new_p["published_date"])})</li>"""
with open(template_file, "r", encoding="utf-8") as f_template:
with open(TEMPLATE_FILE, "r", encoding="utf-8") as f_template:
template = f_template.read()
send_mail(template.replace("{{content}}", content), default_config)
print(f"Send a mail for new releases and projects on: {', '.join(news)}")
with open("conf.ini", "w", encoding="utf-8") as configfile:
parser.write(configfile)
@@ -97,8 +142,7 @@ def get_last_release(project):
url = f"https://api.github.com/repos/{project}/releases/latest"
result = requests.get(url, timeout=10)
print(project)
print(url)
print(f"Check {project} - {url}")
release = result.json()
release_tag = release["tag_name"]
published_date = release["published_at"]