_post = _getpost('https://outerbounds.com/wp-json/wp/v2/posts/220')
assert _post
_post = _getpost(post_id=247, baseurl='https://outerbounds.com/wp-json/wp/v2/posts') #this is an alternate way of calling this
assert _post
url2api
allows you to retrieve the full api endpoint to get the contents of the wordpress article as described here from any public-facing wordpress url!
test_eq(url2api('https://outerbounds.com/blog/notebooks-in-production-with-metaflow/'), 'https://outerbounds.com/wp-json/wp/v2/posts/220')
Instantiating a WP
object will allow you to get access to useful properties that can render as front matter.
_post = WP('https://outerbounds.com/blog/notebooks-in-production-with-metaflow/')
print(_post.frontmatter)
_result="""---
title: "Notebooks In Production With Metaflow"
date: "2022-02-09T22:59:06"
image: "https://outerbounds.com/wp-content/uploads/2022/02/Screen-Shot-2022-02-09-at-12.45.20-pm-1024x525.png"
slug: "notebooks-in-production-with-metaflow"
---
"""
test_eq(_post.frontmatter, _result)
The markdown representation is also available as a property (below we print the first 1,000 characters:
print(_post.markdown[:1500])
Getting Hidden Posts
A wordpress post may note be public (i.e. it might have a status other than published
). In that case, you will need two pieces of information to retrieve the markdown content for that post.
The url for the api. This is
<your_site>/wp-json/v2/posts
, for examplehttps://outerbounds.com/wp-json/wp/v2/posts
. Note: This is the api route to retrieve a single WP post.The
post id
you wish to convert to markdown. The post id can be extracted from wordpress edit url, for example the id forhttps://outerbounds.com/wp-admin/post.php?post=220&action=edit
is220
.
For example, we can get the contets of a post which has an id of 220
as follows:
_post = WP(baseurl='https://outerbounds.com/wp-json/wp/v2/posts', post_id=220)
test_eq(_post.title, 'Notebooks In Production With Metaflow')
assert len(_post.mdimages) == 5
_post.tomd()
_data_dir = Path('_notebooks-in-production-with-metaflow_data/')
assert _data_dir.exists()
_data_dir.ls()
The paths to downloaded images are also automatically replaced in the markdown file:
_md = Path('notebooks-in-production-with-metaflow.md').read_text()
_img_url=r'https://lh4.googleusercontent.com/-8XLZezB4E7s64BQcu-KTZO0VWh4VyKXpNhVwzSqPSgrAC_3qu62OZ-fleSr5mGRqPYTcEm5ed_ZKx8o6W0o2JIQea1kCemhuX5cZMMZRRtumGi0yf0mIp3DJWvDzKGpUR9GP8ug'
assert _img_url in _post.raw_markdown
assert _img_url not in _md