Skip to content

Instantly share code, notes, and snippets.

@nottrobin
Last active April 22, 2020 20:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nottrobin/96d3f325b4ae29847c759c4015c9b05f to your computer and use it in GitHub Desktop.
Save nottrobin/96d3f325b4ae29847c759c4015c9b05f to your computer and use it in GitHub Desktop.
Get discourse topic from discourse.ubuntu.com
#! /usr/bin/env python3
import json
import os
import sys
import time
import urllib.request
topic_id = sys.argv[1]
api_key = os.environ["UBUNTU_DISCOURSE_API_KEY"]
url = "https://discourse.ubuntu.com/t/{topic_id}.json?include_raw=true&api_key={api_key}&api_username=system".format(**locals())
response = None
while response is None or response.status == 429:
if response is not None and response.status == 429:
seconds = 5
error_message = json.loads(response.read().decode())["errors"][0]
seconds_match = re.search("wait (\d+) seconds", error_message)
if seconds_match:
seconds = int(seconds_match.groups()[0]) + 1
print(
" > 429 from API, waiting {seconds} seconds ... "
"('{error_message}')".format(**locals())
)
time.sleep(seconds)
response = urllib.request.urlopen(url)
print(json.loads(response.read().decode())["post_stream"]["posts"][0]["raw"])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment