Coder Social home page Coder Social logo

go-substack's Introduction

Go-substack - push markdown straight to substack

This package provides a converter to transform markdown into substack document format and a client to push the document to substack to create and update drafts, you can check the notes

Example Usage

See test folder for example scripts.

Create a draft:

package main

import (
	"context"
	"flag"
	"fmt"

	"github.com/can3p/go-substack/client"
	"github.com/can3p/go-substack/markdown"
	"github.com/can3p/go-substack/types"
)

func main() {
	sessionID := flag.String("sid", "", "session id")
	substackName := flag.String("name", "", "substack name")
	substackID := flag.Int("substack-id", 0, "substack id")
	title := flag.String("title", "", "draft title")
	md := flag.String("md", "", "markdown text to publish")

	flag.Parse()

	if *sessionID == "" || *substackName == "" || *md == "" || *title == "" || *substackID == 0 {
		panic("all arguments are required")
	}

	c, err := client.NewClient(nil, *sessionID, *substackName)

	if err != nil {
		panic(err)
	}

	doc, err := markdown.ToDoc(*md)

	if err != nil {
		panic(err)
	}

	ctx := context.Background()

	draft := &types.Draft{
		Audience:   "everyone",
		Type:       "newsletter",
		DraftTitle: *title,
		DraftBody:  types.DraftBody(*doc),
		DraftBylines: []types.DraftBylines{
			{ID: *substackID},
		},
	}

	respDraft, err := c.CreateNewDraft(ctx, draft)

	if err != nil {
		panic(err)
	}

	fmt.Printf("You can checkout new draft on https://%s.substack.com/publish/post/%d\n", *substackName, respDraft.ID)
}

You can run it like this:

$ go run create_draft.go -name substack_name -sid take_from_cookie -title "test draft from the client 333" -md "this is the first paragraph

And this is the second one111" -substack-id 1234
You can checkout new draft on https://substack_name.substack.com/publish/post/1456

Update an existing draft:

package main

import (
	"context"
	"flag"
	"fmt"

	"github.com/can3p/go-substack/client"
	"github.com/can3p/go-substack/markdown"
	"github.com/can3p/go-substack/types"
)

func main() {
	sessionID := flag.String("sid", "", "session id")
	substackName := flag.String("name", "", "substack name")
	substackID := flag.Int("substack-id", 0, "substack id")
	draftID := flag.Int("draft-id", 0, "draft id")
	title := flag.String("title", "", "draft title")
	md := flag.String("md", "", "markdown text to publish")

	flag.Parse()

	if *sessionID == "" || *substackName == "" || *md == "" || *title == "" || *substackID == 0 || *draftID == 0 {
		panic("all arguments are required")
	}

	c, err := client.NewClient(nil, *sessionID, *substackName)

	if err != nil {
		panic(err)
	}

	doc, err := markdown.ToDoc(*md)

	if err != nil {
		panic(err)
	}

	ctx := context.Background()

	draft := &types.Draft{
		Audience:   "everyone",
		Type:       "newsletter",
		DraftTitle: *title,
		DraftBody:  types.DraftBody(*doc),
		DraftBylines: []types.DraftBylines{
			{ID: *substackID},
		},
	}

	respDraft, err := c.UpdateDraft(ctx, *draftID, draft)

	if err != nil {
		panic(err)
	}

	fmt.Printf("You can checkout updated draft on https://%s.substack.com/publish/post/%d\n", *substackName, respDraft.ID)
}

Will be run like this:

$ go run update_draft.go -name substack_name -sid take_from_cookie -title "test draft from the client" -md "this is the first paragraph

And this is the second one111234" -substack-id 12345 -draft-id 456
You can checkout updated draft on https://substack_name.substack.com/publish/post/456

where:

  • sid is a substack.sid cookie that you can extract with dev tools
  • substack-id is a substack internal id probable, you can get it from one of the requests with dev tools

FAQ

Can I use it?

Only on your own risk. API is not public and the package can break any time

Should I use it?

Only if you're adventurous

Why should I hunt for the values with dev tools?

API is not public and there is no way to use authentenication like OAuth or anything like this. If you know a better way, please let me know!

Is all markdown supported?

Not at all, you could check tests to see what's allowed. The good news is that the package should fail conversion in case of unknown markup instead of trying to push garbage

Are substack specific elements supported?

They can be, since it's clear how to add the to the substack document format, but not implemented yet. The way to go would be to extend markdown a bit for that

License

MIT

go-substack's People

Contributors

can3p avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.