diff options
-rw-r--r-- | .gitignore | 3 | ||||
-rw-r--r-- | config.py | 6 | ||||
-rw-r--r-- | graphql_queries.py | 13 | ||||
-rw-r--r-- | graphql_requests.py | 31 | ||||
-rwxr-xr-x | main.py | 72 |
5 files changed, 125 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2380aaa --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +__pycache__/ +config.ini +*.swp diff --git a/config.py b/config.py new file mode 100644 index 0000000..3b57c53 --- /dev/null +++ b/config.py @@ -0,0 +1,6 @@ +from configparser import ConfigParser + +config_path = "/home/mark/projects/wikijscmd/config.ini" + +config = ConfigParser() +config.read(config_path) diff --git a/graphql_queries.py b/graphql_queries.py new file mode 100644 index 0000000..92e3734 --- /dev/null +++ b/graphql_queries.py @@ -0,0 +1,13 @@ +import graphql_requests + +def get_single_page(page_id): + query = 'query {\npages {\nsingle (id: %s) {\nid\npath\ntitle\ncontent\n}\n}\n}' % page_id + return graphql_requests.send_query(query) + +def create_page(content, title, path): + query = 'mutation {\npages {\ncreate(\ncontent: "%s"\ndescription: ""\neditor: "markdown"\nisPrivate: false\nisPublished: true\nlocale: "en"\npath: "%s"\npublishEndDate: ""\npublishStartDate: ""\nscriptCss: ""\nscriptJs: ""\ntags: []\ntitle: "%s"\n) {\nresponseResult {\nsucceeded\nerrorCode\nslug\nmessage\n__typename\n}\npage {\nid\nupdatedAt\n__typename\n}\n__typename\n}\n__typename\n}\n}' % ( content, path, title ) + return graphql_requests.send_query(query) + +def get_tree(): + query = 'query {\n pages {\n list (orderBy: PATH) {\n id\npath\ntitle\n}\n}\n}' + return graphql_requests.send_query(query) diff --git a/graphql_requests.py b/graphql_requests.py new file mode 100644 index 0000000..f802cdd --- /dev/null +++ b/graphql_requests.py @@ -0,0 +1,31 @@ +import requests +import sys +from config import config + +def handle_errors(r): + error = False + if r.status_code != 200: + error = True + print("Error status code: %s" % r.status_code) + json = r.json() + if "errors" in json: + error = True + for e in json["errors"]: + print(e["message"]) + if error: + sys.exit(1) + + +def get_headers(): + return { "Authorization": "Bearer %s" % config["wiki"]["key"] } + +def escape_query(query): + return query.replace('"', '\"') + +def send_query(query): + '''Returns status code, json''' + payload = { "query": query } + r = requests.post(config["wiki"]["url"], json=payload, headers = get_headers()) + handle_errors(r) + return r.json() + @@ -0,0 +1,72 @@ +#!/usr/bin/python3 +import sys + +from config import config +import graphql_queries, graphql_requests + +def print_item(item): + trimmmed_path = item["path"][:17]+"..." if len(item["path"]) > 20 else item["path"] + print("| %6s | %20s | %44s |" % (item["id"], trimmmed_path, item["title"]) ) + +def create(): + if len(sys.argv) < 5: + print("Usage: ./main.py create <path> <title> <content>") + sys.exit(1) + path = sys.argv[2] + title = sys.argv[3] + content = sys.argv[4] + response = graphql_queries.create_page(content, title, path) + result = response["data"]["pages"]["create"]["responseResult"] + if not result["succeeded"]: + print("Error!", result["message"]) + sys.exit(1) + print(result["message"]) + +def tree(): + response = graphql_queries.get_tree() + for item in response["data"]["pages"]["list"]: + if len(sys.argv) == 3 and sys.argv[2] not in item["path"]: + continue + print_item(item) + +def single(): + if len(sys.argv) < 3: + print("Usage: ./main.py single <id|path>") + sys.exit(0) + if not sys.argv[2].isdigit(): + found = False + for item in graphql_queries.get_tree()["data"]["pages"]["list"]: + if sys.argv[2] == item["path"]: + sys.argv[2] = item["id"] + found = True + if not found: + print("No page with path: %s" % sys.argv[2]) + sys.exit(0) + page_id = int(sys.argv[2]) + response = graphql_queries.get_single_page(page_id) + page = response["data"]["pages"]["single"] + print("-" * 80) + print_item(page) + print("-" * 80) + print(page["content"]) + +def main(): + if len(sys.argv) < 2: + print("Usage: ./main.py <command> <args>") + print("Commands:") + print("\tcreate <path> <title> <content>") + print("\ttree <contains?>") + print("\tsingle <id|path>") + sys.exit(0) + + if sys.argv[1] == "create": + create() + elif sys.argv[1] == "tree": + tree() + elif sys.argv[1] == "single": + single() + else: + print("Unknown command: %s" % sys.argv[1]) + +if __name__ == "__main__": + main() |