# Python Substack
This is an unofficial library providing a Python interface for [Substack](https://substack.com/).
I am in no way affiliated with Substack.
[](https://www.python.org/downloads/)
[](https://pepy.tech/project/python-substack)

---
# 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 😃 🚀
```python
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:
```shell
pip install pre-commit
```
Set up pre-commit
```shell
pre-commit install
```
Raw data
{
"_id": null,
"home_page": "https://github.com/ma2za/python-substack",
"name": "python-substack",
"maintainer": "",
"docs_url": null,
"requires_python": ">=3.7,<4.0",
"maintainer_email": "",
"keywords": "substack",
"author": "Paolo Mazza",
"author_email": "mazzapaolo2019@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/b4/e3/66fa74ac3b2cea121fdd8fa22cc33b6be843b9105b07d1ce2317ea2ea992/python_substack-0.1.15.tar.gz",
"platform": null,
"description": "# Python Substack\n\nThis is an unofficial library providing a Python interface for [Substack](https://substack.com/).\nI am in no way affiliated with Substack.\n\n[](https://www.python.org/downloads/)\n[](https://pepy.tech/project/python-substack)\n\n---\n\n# Installation\n\nYou can install python-substack using:\n\n $ pip install python-substack\n\n---\n\n# Setup\n\nSet the following environment variables by creating a **.env** file:\n\n EMAIL=\n PASSWORD=\n\n## If you don't have a password\n\nRecently Substack has been setting up new accounts without a password. If you sign-out and sign back in it just uses\nyour email address with a \"magic\" link.\n\nSet a password:\n\n- Sign-out of Substack\n- At the sign-in page click, \"Sign in with password\" under the `Email` text box\n- Then choose, \"Set a new password\"\n\nThe .env file will be ignored by git but always be careful.\n\n---\n\n# Usage\n\nCheck out the examples folder for some examples \ud83d\ude03 \ud83d\ude80\n\n```python\nimport os\n\nfrom substack import Api\nfrom substack.post import Post\n\napi = Api(\n email=os.getenv(\"EMAIL\"),\n password=os.getenv(\"PASSWORD\"),\n)\n\nuser_id = api.get_user_id()\n\n# Switch Publications - The library defaults to your users primary publication. You can retrieve all your publications and change which one you want to use.\n\n# primary publication\nuser_publication = api.get_user_primary_publication()\n# all publications\nuser_publications = api.get_user_publications()\n\n# This step is only necessary if you are not using your primary publication\n# api.change_publication(user_publication)\n\npost = Post(\n title=\"How to publish a Substack post using the Python API\",\n subtitle=\"This post was published using the Python API\",\n user_id=user_id\n)\n\npost.add({'type': 'paragraph', 'content': 'This is how you add a new paragraph to your post!'})\n\n# bolden text\npost.add({'type': \"paragraph\",\n 'content': [{'content': \"This is how you \"}, {'content': \"bolden \", 'marks': [{'type': \"strong\"}]},\n {'content': \"a word.\"}]})\n\n# add hyperlink to text\npost.add({'type': 'paragraph', 'content': [\n {'content': \"View Link\", 'marks': [{'type': \"link\", 'href': 'https://whoraised.substack.com/'}]}]})\n\n# set paywall boundary\npost.add({'type': 'paywall'})\n\n# add image\npost.add({'type': 'captionedImage', 'src': \"https://media.tenor.com/7B4jMa-a7bsAAAAC/i-am-batman.gif\"})\n\n# add local image\nimage = api.get_image('image.png')\npost.add({\"type\": \"captionedImage\", \"src\": image.get(\"url\")})\n\n# embed publication\nembedded = api.publication_embed(\"https://jackio.substack.com/\")\npost.add({\"type\": \"embeddedPublication\", \"url\": embedded})\n\ndraft = api.post_draft(post.get_draft())\n\n# set section (THIS CAN BE DONE ONLY AFTER HAVING FIRST POSTED THE DRAFT)\npost.set_section(\"rick rolling\", api.get_sections())\napi.put_draft(draft.get(\"id\"), draft_section_id=post.draft_section_id)\n\napi.prepublish_draft(draft.get(\"id\"))\n\napi.publish_draft(draft.get(\"id\"))\n```\n\n# Contributing\n\nInstall pre-commit:\n\n```shell\npip install pre-commit\n```\n\nSet up pre-commit\n\n```shell\npre-commit install\n```\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "A Python wrapper around the Substack API.",
"version": "0.1.15",
"project_urls": {
"Homepage": "https://github.com/ma2za/python-substack",
"Repository": "https://github.com/ma2za/python-substack"
},
"split_keywords": [
"substack"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "a6ed03c075778a6fcb9806dfde1fe521dc80a131b31bc2638e1636e68c1cbbba",
"md5": "ef39ad3166c43953db9bfde00b4338bf",
"sha256": "fa69792c837042922c7e349f45a998f2913c6cbe5911bd5fb09e988acf3fd4cb"
},
"downloads": -1,
"filename": "python_substack-0.1.15-py3-none-any.whl",
"has_sig": false,
"md5_digest": "ef39ad3166c43953db9bfde00b4338bf",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.7,<4.0",
"size": 9932,
"upload_time": "2023-12-26T17:10:54",
"upload_time_iso_8601": "2023-12-26T17:10:54.106425Z",
"url": "https://files.pythonhosted.org/packages/a6/ed/03c075778a6fcb9806dfde1fe521dc80a131b31bc2638e1636e68c1cbbba/python_substack-0.1.15-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "b4e366fa74ac3b2cea121fdd8fa22cc33b6be843b9105b07d1ce2317ea2ea992",
"md5": "2c5a7630cedc56b73dafb0ce5864ccb5",
"sha256": "5ab1ffd9aee2987d56f0bbdb99ebf0c8809e550232153a56bc515918268c9fe7"
},
"downloads": -1,
"filename": "python_substack-0.1.15.tar.gz",
"has_sig": false,
"md5_digest": "2c5a7630cedc56b73dafb0ce5864ccb5",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.7,<4.0",
"size": 9841,
"upload_time": "2023-12-26T17:10:56",
"upload_time_iso_8601": "2023-12-26T17:10:56.603535Z",
"url": "https://files.pythonhosted.org/packages/b4/e3/66fa74ac3b2cea121fdd8fa22cc33b6be843b9105b07d1ce2317ea2ea992/python_substack-0.1.15.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-12-26 17:10:56",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "ma2za",
"github_project": "python-substack",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "python-substack"
}