October 19, 2024

How to get articles using REST API

GET {wordpress_url}/wp-json/wp/v2/posts
GET {wordpress_url}/{category}/wp-json/wp/v2/posts

Using Markdownify in order to convert html

markdownify is a good library to have markdown conversion functions. Here shows the usage

Iterates articles and post it evrytime

import io
import requests
import markdownify

res = requests.get(f"{wordpress_url}/wp-json/wp/v2/posts")
articles = res.json()


for a_loop in articles:
    title = artile.get('title').get('rendered')
    content_md = markdownify.markdownify(
        article.get('content').get('rendered'), heading_style='ATX'
    )
    data = io.BytesIO()
    data.write(bytes(content_md, encoding='utf-8'))
    data.name = f"{title}.txt"

    res = requests.post(
        service_url,
        files={'files': (f"{title}.txt", data.getvalue())}
    )
    # print(res.status_code)

Iterates articles and post chunk of articles



for a_loop in articles:
    title = artile.get('title').get('rendered')
    content_md = markdownify.markdownify(
        article.get('content').get('rendered'), heading_style='ATX'
    )
    data = io.BytesIO()
    data.write(bytes(content_md, encoding='utf-8'))
    data.name = f"{title}.txt"
    files.append(
        ('files', (data.name, data.getvalue())
    )


res = requests.post(
     service_url,
     files={'files': (f"{title}.txt", data.getvalue())}
)

# print(res.status_code)