diff options
| -rw-r--r-- | .gitignore | 1 | ||||
| -rw-r--r-- | custom_requests.py (renamed from graphql_requests.py) | 9 | ||||
| -rw-r--r-- | graphql_queries.py | 20 | ||||
| -rwxr-xr-x | main.py | 57 | 
4 files changed, 69 insertions, 18 deletions
@@ -1,3 +1,4 @@  __pycache__/  config.ini  *.swp + diff --git a/graphql_requests.py b/custom_requests.py index f802cdd..65673cf 100644 --- a/graphql_requests.py +++ b/custom_requests.py @@ -1,5 +1,6 @@  import requests  import sys +import json  from config import config  def handle_errors(r): @@ -12,6 +13,7 @@ def handle_errors(r):          error = True          for e in json["errors"]:              print(e["message"]) +            print(e)      if error:          sys.exit(1) @@ -19,12 +21,9 @@ def handle_errors(r):  def get_headers():      return { "Authorization": "Bearer %s" % config["wiki"]["key"] }  -def escape_query(query): -    return query.replace('"', '\"') - -def send_query(query): +def send_query(query, query_vars):      '''Returns status code, json''' -    payload = { "query": query } +    payload = { "query": query, "variables": query_vars}      r = requests.post(config["wiki"]["url"], json=payload, headers = get_headers())      handle_errors(r)      return r.json() diff --git a/graphql_queries.py b/graphql_queries.py index 92e3734..6d7094f 100644 --- a/graphql_queries.py +++ b/graphql_queries.py @@ -1,13 +1,21 @@ -import graphql_requests +import custom_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) +    query = 'query ($id: Int!) {\npages {\nsingle (id: $id) {\nid\npath\ntitle\ncontent\n}\n}\n}' +    query_vars = {"id": page_id} +    return custom_requests.send_query(query, query_vars)  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) +    query = 'mutation ($content: String!, $path: String!, $title: String!) {\npages {\ncreate(\ncontent: $content\ndescription: ""\neditor: "markdown"\nisPrivate: false\nisPublished: true\nlocale: "en"\npath: $path\npublishEndDate: ""\npublishStartDate: ""\nscriptCss: ""\nscriptJs: ""\ntags: []\ntitle: $title\n) {\nresponseResult {\nsucceeded\nerrorCode\nslug\nmessage\n__typename\n}\npage {\nid\nupdatedAt\n__typename\n}\n__typename\n}\n__typename\n}\n}' +    query_vars = {"content": content, "title": title, "path": path} +    return custom_requests.send_query(query, query_vars)  def get_tree():      query = 'query {\n pages {\n list (orderBy: PATH) {\n id\npath\ntitle\n}\n}\n}' -    return graphql_requests.send_query(query) +    query_vars = {  } +    return custom_requests.send_query(query, query_vars) + +def edit_page(page_id, content, title, path): +    query = 'mutation ($id: Int!, $content: String!, $path: String!, $title: String!){\npages {\nupdate(\nid: $id\ncontent: $content\ndescription: ""\neditor: "markdown"\nisPrivate: false\nisPublished: true\nlocale: "en"\npath: $path\npublishEndDate: ""\npublishStartDate: ""\nscriptCss: ""\nscriptJs: ""\ntags: []\ntitle: $title\n) {\nresponseResult {\nsucceeded\nerrorCode\nslug\nmessage\n__typename\n}\npage {\nid\nupdatedAt\n__typename\n}\n__typename\n}\n__typename\n}\n}' +    query_vars = {"id": page_id, "content": content, "title": title, "path": path} +    return custom_requests.send_query(query, query_vars) @@ -1,8 +1,9 @@  #!/usr/bin/python3  import sys - +import os +import subprocess  from config import config -import graphql_queries, graphql_requests +import graphql_queries  def print_item(item):      trimmmed_path = item["path"][:17]+"..." if len(item["path"]) > 20 else item["path"] @@ -29,11 +30,10 @@ def tree():              continue          print_item(item) -def single(): -    if len(sys.argv) < 3: -        print("Usage: ./main.py single <id|path>") -        sys.exit(0) +def get_single_page():      if not sys.argv[2].isdigit(): +        if sys.argv[2].startswith("/"): +            sys.argv[2] = sys.argv[2][1:]          found = False          for item in graphql_queries.get_tree()["data"]["pages"]["list"]:              if sys.argv[2] == item["path"]: @@ -44,12 +44,52 @@ def single():              sys.exit(0)      page_id = int(sys.argv[2])      response = graphql_queries.get_single_page(page_id) -    page = response["data"]["pages"]["single"] +    return response["data"]["pages"]["single"] + +def single(): +    if len(sys.argv) < 3: +        print("Usage: ./main.py single <id|path>") +        sys.exit(0) +    page = get_single_page()      print("-" * 80)      print_item(page)      print("-" * 80)      print(page["content"]) +def edit(): +    # Load content to edit +    if len(sys.argv) < 3: +        print("Usage: ./main.py edit <id|path>") +        sys.exit(0) +    page = get_single_page() +    body = page["content"] + +    # Open it in editor +    if "VISUAL" in os.environ: +        editor = os.environ['VISUAL'] +    else: +        editor = os.environ['EDITOR'] +    edit_filename = "/tmp/wikijscmd-edit" +    with open(edit_filename, "w") as f: +        f.write(body) +    subprocess.run([editor, edit_filename]) +    +    # Prompt user to save it to the wiki +    with open(edit_filename, "r") as f: +        new_body = f.read() +    print_item(page) +    print("-" * 80) +    print(new_body) +    print("-" * 80) +    if input("Save changes? (y/n) ") == "y": +        response = graphql_queries.edit_page(page["id"], new_body, page["title"], page["path"]) +        result = response["data"]["pages"]["update"]["responseResult"] +        if not result["succeeded"]: +            print("Error!", result["message"]) +            sys.exit(1) +        print(result["message"]) +    os.remove(edit_filename) +  def main():      if len(sys.argv) < 2:          print("Usage: ./main.py <command> <args>") @@ -57,6 +97,7 @@ def main():          print("\tcreate <path> <title> <content>")          print("\ttree <contains?>")          print("\tsingle <id|path>") +        print("\tedit <id|path>")          sys.exit(0)      if sys.argv[1] == "create": @@ -65,6 +106,8 @@ def main():          tree()      elif sys.argv[1] == "single":          single() +    elif sys.argv[1] == "edit": +        edit()      else:          print("Unknown command: %s" % sys.argv[1])  | 
