Last active
January 20, 2023 12:15
-
-
Save Salamandar/461419d3e4b75c94ac5d1b8c34859f33 to your computer and use it in GitHub Desktop.
Import json to Fider via SQL queries. import_apps should be called first, then the list could be reviewed, then imported via import_apps.py.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import re | |
import json | |
import yaml | |
import psycopg2 | |
def connect_db(): | |
hostname = "localhost" | |
with open("/etc/yunohost/apps/fider/settings.yml", encoding="utf-8") as settings_file: | |
settings = yaml.load(settings_file, Loader=yaml.SafeLoader) | |
username = settings["db_name"] | |
database = settings["db_name"] | |
password = settings["psqlpwd"] | |
conn = psycopg2.connect( | |
f"host={hostname} user={username} password={password} dbname={database}") | |
return conn | |
def commit_and_close_db(conn): | |
conn.commit() | |
conn.close() | |
def insert_app(conn, app): | |
insert_post_sql = """ | |
insert into posts | |
(title, description, slug, tenant_id, created_at, user_id, number, status) | |
values ( | |
%(name)s, %(description)s, %(name_slug)s, | |
1, '2023-01-19 23:00:00.0000+01', 1, (SELECT MAX(number)+1 FROM posts), 0 | |
); | |
""" | |
name = f"New app: {app['name']}" | |
description = f"{app['description']}" | |
if url := app["url"]: | |
description += f"\nProject url: {url}" | |
if upstream := app["upstream"]: | |
description += f"\nUpstream source code: {upstream}" | |
slug = re.sub('[^0-9a-zA-Z]+', '-', name.lower()) | |
conn.cursor().execute( | |
insert_post_sql, | |
{"name": name, "description": description, "name_slug": slug} | |
) | |
def main(): | |
conn = connect_db() | |
with open("apps_formatted.json", encoding="utf-8") as apps_json: | |
apps = json.load(apps_json) | |
for app in apps: | |
insert_app(conn, app) | |
commit_and_close_db(conn) | |
if __name__ == "__main__": | |
main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
""" | |
This script expects apps.json to be filled by https://tableconvert.com/markdown-to-json | |
""" | |
# import re | |
import json | |
from typing import Dict, Tuple, Optional, Any | |
import markdown | |
from lxml import etree | |
def text_to_link(text: str) -> Tuple[str, Optional[str]]: | |
"""[text](link) -> text, link""" | |
try: | |
doc = etree.fromstring(markdown.markdown(text)) | |
links = doc.xpath("//a") | |
return links[0].text, links[0].get("href") | |
except Exception: | |
return text, None | |
def reformat_app(app: Dict[str, str]) -> None: | |
"""Reformats the apps objets""" | |
if "" in app.keys(): | |
del app[""] | |
app["name"], url = text_to_link(app["name"]) | |
app["url"] = url or "" | |
app["upstream"] = text_to_link(app["upstream"])[1] or app["upstream"] | |
app["package_draft"] = text_to_link(app["upstream"])[1] or app["package_draft"] | |
def main(): | |
with open("apps.json", encoding="utf-8") as apps_json: | |
apps = json.load(apps_json) | |
for app in apps: | |
reformat_app(app) | |
with open("apps_formatted.json", "w", encoding="utf-8") as apps_json: | |
apps_json.write(json.dumps(apps, indent=4)) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment