Skip to main content

A Python wrapper around the Substack API.

Project description

Python Substack

This is an unofficial library providing a Python interface for Substack. I am in no way affiliated with Substack.

Python Downloads Release Build

Installation

You can install python-substack using:

$ pip install python-substack

Setup

Set the following environment variables by creating a .env file:

EMAIL=
PASSWORD=

If you don't have a password

Recently Substack has been setting up new accounts without a password. If you sign-out and sign back in it just uses your email address with a "magic" link.

Set a password:

  • Sign-out of Substack
  • At the sign-in page click, "Sign in with password" under the Email text box
  • Then choose, "Set a new password"

The .env file will be ignored by git but always be careful.


Usage

Check out the examples folder for some examples 😃 🚀

import os

from substack import Api
from substack.post import Post

api = Api(
    email=os.getenv("EMAIL"),
    password=os.getenv("PASSWORD"),
)

user_id = api.get_user_id()

# Switch Publications - The library defaults to your users primary publication. You can retrieve all your publications and change which one you want to use.

# primary publication
user_publication = api.get_user_primary_publication()
# all publications
user_publications = api.get_user_publications()

# This step is only necessary if you are not using your primary publication
# api.change_publication(user_publication)

post = Post(
    title="How to publish a Substack post using the Python API",
    subtitle="This post was published using the Python API",
    user_id=user_id
)

post.add({'type': 'paragraph', 'content': 'This is how you add a new paragraph to your post!'})

# bolden text
post.add({'type': "paragraph",
          'content': [{'content': "This is how you "}, {'content': "bolden ", 'marks': [{'type': "strong"}]},
                      {'content': "a word."}]})

# add hyperlink to text
post.add({'type': 'paragraph', 'content': [
    {'content': "View Link", 'marks': [{'type': "link", 'href': 'https://whoraised.substack.com/'}]}]})

# set paywall boundary
post.add({'type': 'paywall'})

# add image
post.add({'type': 'captionedImage', 'src': "https://media.tenor.com/7B4jMa-a7bsAAAAC/i-am-batman.gif"})

# add local image
image = api.get_image('image.png')
post.add({"type": "captionedImage", "src": image.get("url")})

# embed publication
embedded = api.publication_embed("https://jackio.substack.com/")
post.add({"type": "embeddedPublication", "url": embedded})

draft = api.post_draft(post.get_draft())

# set section (THIS CAN BE DONE ONLY AFTER HAVING FIRST POSTED THE DRAFT)
post.set_section("rick rolling", api.get_sections())
api.put_draft(draft.get("id"), draft_section_id=post.draft_section_id)

api.prepublish_draft(draft.get("id"))

api.publish_draft(draft.get("id"))

Contributing

Install pre-commit:

pip install pre-commit

Set up pre-commit

pre-commit install

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

python_substack-0.1.15.tar.gz (9.8 kB view details)

Uploaded Source

Built Distribution

python_substack-0.1.15-py3-none-any.whl (9.9 kB view details)

Uploaded Python 3

File details

Details for the file python_substack-0.1.15.tar.gz.

File metadata

  • Download URL: python_substack-0.1.15.tar.gz
  • Upload date:
  • Size: 9.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.7.1 CPython/3.11.0 Linux/6.2.0-1018-azure

File hashes

Hashes for python_substack-0.1.15.tar.gz
Algorithm Hash digest
SHA256 5ab1ffd9aee2987d56f0bbdb99ebf0c8809e550232153a56bc515918268c9fe7
MD5 2c5a7630cedc56b73dafb0ce5864ccb5
BLAKE2b-256 b4e366fa74ac3b2cea121fdd8fa22cc33b6be843b9105b07d1ce2317ea2ea992

See more details on using hashes here.

File details

Details for the file python_substack-0.1.15-py3-none-any.whl.

File metadata

  • Download URL: python_substack-0.1.15-py3-none-any.whl
  • Upload date:
  • Size: 9.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.7.1 CPython/3.11.0 Linux/6.2.0-1018-azure

File hashes

Hashes for python_substack-0.1.15-py3-none-any.whl
Algorithm Hash digest
SHA256 fa69792c837042922c7e349f45a998f2913c6cbe5911bd5fb09e988acf3fd4cb
MD5 ef39ad3166c43953db9bfde00b4338bf
BLAKE2b-256 a6ed03c075778a6fcb9806dfde1fe521dc80a131b31bc2638e1636e68c1cbbba

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page