Ejemplo n.º 1
0
def search(
    service: str,
    *,
    config_path: str,
    mapping_path: str,
    save: bool = False,
    show_mapping: bool = False,
    query: t.Optional[str] = None,
    page: t.Optional[int] = None,
    per_page: t.Optional[int] = None,
    out: t.Optional[t.IO] = None,
    verbose: bool = False,
) -> None:
    from shosai import App

    out = out or sys.stdout
    app = App(config_path, service=service, mapping_path=mapping_path)
    with app.resource as r:
        data = r.search(q=query, page=page, per_page=per_page)
    if show_mapping:
        for _, mapping in app.transform.from_search_response(data):
            json.dump(mapping, out, indent=2, ensure_ascii=False)
            out.write(os.linesep)
    else:
        json.dump(data, out, indent=2, ensure_ascii=False)
    if save:
        with app.saver as append:
            for post, mapping in app.transform.from_search_response(data):
                append(post, mapping, savefile=True)  # xxx
Ejemplo n.º 2
0
def auth(
    service: str,
    *,
    config_path: str,
    out: t.Optional[t.IO] = None,
    verbose: bool = False,
) -> None:
    """show setup information (not auth)"""
    from shosai import App

    out = out or sys.stdout
    app = App(config_path, service=service)
    c = app.configuration
    print(
        "please, setting value something like following (in {})".format(
            app.config_path),
        file=sys.stderr,
    )
    d = {app.service: c.CONFIG_SKELETON}
    json.dump(d, out, indent=2, ensure_ascii=False)
    print("", file=out)

    import webbrowser

    print("open {}".format(c.AUTH_DOC_URL))
    webbrowser.open_new_tab(c.AUTH_DOC_URL)
Ejemplo n.º 3
0
def tags(
    service: str,
    *,
    config_path: str,
    mapping_path: str,
    out: t.Optional[t.IO] = None,
    verbose: bool = False,
):
    """https://help.docbase.io/posts/92979"""
    from shosai import App

    out = out or sys.stdout
    app = App(config_path, service=service, mapping_path=mapping_path)
    assert isinstance(app.resource, resources.Resource)
    with app.resource as r:
        data = r.tags()
        json.dump(data, out, indent=2, ensure_ascii=False)
Ejemplo n.º 4
0
def attachment(
    *,
    config_path: str,
    mapping_path: str,
    service: str,
    save: bool = True,
    paths: t.Sequence[str],
    out: t.Optional[t.IO] = None,
) -> None:
    from shosai import App

    out = out or sys.stdout
    app = App(config_path, service=service, mapping_path=mapping_path)
    with app.resource as r:
        contents = []
        for path in paths:
            contents.append(r.attachment.build_content_from_file(path))
        data = r.attachment(contents)
    json.dump(data, out, indent=2, ensure_ascii=False)
Ejemplo n.º 5
0
def clone(
    service: str,
    *,
    config_path: str,
    mapping_path: str,
    url: str,
    name: t.Optional[str] = None,
    out: t.Optional[t.IO] = None,
    verbose: bool = False,
) -> None:
    from shosai import App

    out = out or sys.stdout
    app = App(config_path, service=service, mapping_path=mapping_path)
    with app.resource as r:
        data = r.fetch.from_url(url)
    with app.saver as append:
        post, mapping = app.transform.from_fetch_response(data)
        append(post, mapping, name=name, savefile=True)
Ejemplo n.º 6
0
def pull(
    service: str,
    *,
    config_path: str,
    mapping_path: str,
    path: str,
    out: t.Optional[t.IO] = None,
    verbose: bool = False,
) -> None:
    from shosai import App

    out = out or sys.stdout
    app = App(config_path, service=service, mapping_path=mapping_path)
    with app.resource as r:
        meta = app.loader.lookup(path)
        if meta is None:
            print(f"mapped file is not found {path}", file=sys.stderr)
            sys.exit(1)
        data = r.fetch(meta["id"])
    with app.saver as append:
        post, mapping = app.transform.from_fetch_response(data)
        append(post, mapping, filepath=meta["file"], savefile=True)
Ejemplo n.º 7
0
def push(
    service: str,
    *,
    config_path: str,
    mapping_path: str,
    save: bool = True,
    path: str,
    draft: t.Optional[bool] = None,
    notice: bool = False,
    id: t.Optional[str] = None,
    out: t.Optional[t.IO] = None,
    verbose: bool = False,
) -> None:
    from shosai import App
    from shosai import parsing

    out = out or sys.stdout
    app = App(config_path, service=service, mapping_path=mapping_path)
    with app.resource as r:
        with open(path) as rf:
            parsed = parsing.parse_article(rf.read())

        meta = app.loader.lookup(path)
        id = id or (meta and meta["id"])

        # parse article and upload images as attachments.
        attachments = []
        namestore = NameStore()
        content = parsed.content

        for image in parsed.images:
            if "://" in image.src:
                continue
            imagepath = os.path.expanduser(
                os.path.join(os.path.dirname(path),
                             image.src).strip("'").strip('"'))
            if not os.path.exists(imagepath):
                logger.info("image: %s is not found (where %s)", imagepath,
                            path)
                continue
            if image.src in namestore:
                continue
            namestore[image.src] = os.path.basename(imagepath)
            attachments.append(
                r.attachment.build_content_from_file(
                    imagepath, name=namestore[image.src]))
        if attachments:
            logger.info("attachments is found, the length is %d",
                        len(attachments))
            uploaded = r.attachment(attachments)

            logger.info("overwrite passed article %s", path)
            for uploaded_image in uploaded:
                image_src = namestore.reverse_lookup(uploaded_image["name"])
                rx = re.compile(f"\\( *{image_src}*\\)")
                content = rx.subn(f"({uploaded_image['url']})", content)[0]
            with open(path, "w") as wf:
                tagspart = "".join([f"[{t}]" for t in parsed.tags])
                wf.write(f"#{tagspart}{parsed.title}\n")
                wf.write("")
                wf.write(content)

        data = r.post(
            parsed.title or (meta and meta.get("title")) or "",
            content,
            tags=parsed.tags,
            id=id,
            draft=draft,
            notice=notice,
            meta=meta,
        )
    if verbose:
        json.dump(data, out, indent=2, ensure_ascii=False)
    if save:
        with app.saver as append:
            post, mapping = app.transform.from_fetch_response(data)
            append(post, mapping, filepath=path, savefile=False)