Skip to content

Instantly share code, notes, and snippets.

@danielvlopes
Created October 14, 2023 14:28
Show Gist options
  • Save danielvlopes/c5f261a1aec50ea431843813b2440b47 to your computer and use it in GitHub Desktop.
Save danielvlopes/c5f261a1aec50ea431843813b2440b47 to your computer and use it in GitHub Desktop.
require 'discourse_api'
class DiscourseClient
def initialize(api_key, api_username, host)
@client = DiscourseApi::Client.new(host)
@client.api_key = api_key
@client.api_username = api_username
end
def fetch_posts_from_category(category_slug)
all_posts = []
# Fetch topics from the category
topics = @client.category_topics(category_slug)
topic_ids = topics.map { |t| t['id'] }
# Fetch posts for each topic
topic_ids.each do |topic_id|
posts = fetch_posts_from_topic(topic_id)
all_posts.concat(posts)
end
all_posts
end
def fetch_posts_from_topic(topic_id)
posts = @client.topic_posts(topic_id)
posts.map { |p| p['cooked'] }
end
end
# Configuration
api_key = "YOUR_API_KEY"
api_username = "YOUR_API_USERNAME"
host = "https://YOUR_DISCOURSE_DOMAIN"
# Initialize client
client = DiscourseClient.new(api_key, api_username, host)
# Fetch all posts from a specific category
category_slug = "your-category-slug"
posts = client.fetch_posts_from_category(category_slug)
# Output posts
puts posts
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment