Skip to content

Instantly share code, notes, and snippets.

@jasonrahm
Created November 22, 2022 19:34
Show Gist options
  • Save jasonrahm/a693da1661d4dfb2cd8745350ee7f911 to your computer and use it in GitHub Desktop.
Save jasonrahm/a693da1661d4dfb2cd8745350ee7f911 to your computer and use it in GitHub Desktop.
Khoros API Message Updates Script
'''
This script does a general query using LiQL against Khoros API for "code",
which is not at all case specific and doesn't seem to match "<", which
would have been extremely helpful.
It loops through message offsets 50 at a time, then attempts to update the
editor code from <CODE></CODE> to <li-code lang="tcl"></code> and then
issues a PUT to the /api/2.0/messages/<msg-id> endpoint with a json payload
of {'data': {'type': 'message', 'body': <new_body>}}
Successes and Failures are logged to separate files, storing the message id and
the URL for future reference.
Less than half of the matching statements were successful due to khoros
validation complaining of invalid HTML. Will investigate that for round 2.
'''
import json
import os
import re
from khoros import Khoros
k = Khoros(
community_url=os.getenv('KHOROS_COMMUNITY_URL'),
session_auth={'username': os.getenv('KHOROS_COMMUNITY_USER'), 'password': os.getenv('KHOROS_COMMUNITY_PASSWORD')}
)
with open(os.getenv('KHOROS_UPDATE_FAILURE_FILE')) as failures:
msg_ids = list(set([x.rstrip().split("\"")[3] for x in failures]))
failure_log = open(os.getenv('KHOROS_UPDATE_FAILURE_FILE'), "a")
success_log = open(os.getenv('KHOROS_UPDATE_SUCCESS_FILE'), "a")
found_match = 0
for offset in range(0, 9451, 50):
query = f"SELECT id, view_href, body, last_publish_time FROM messages WHERE board.id = os.getenv('KHOROS_COMMUNITY_BOARD_ID') AND body MATCHES 'code' AND last_publish_time < 1643097600000 LIMIT 50 OFFSET {offset}"
response = k.query(query)
print(f'\n\nStarting query at offset {offset}.\n\n')
for item in response.get('data').get('items'):
if '<CODE>' in item.get('body'):
new_body = re.sub('<CODE>', '<li-code lang="tcl">', item.get('body'))
new_body = re.sub('</CODE>', '</li-code>', new_body)
msg_update = {'data': {'type': 'message', 'body': new_body}}
if item.get("id") not in msg_ids:
print(f'MATCH: Update for message id: {item.get("id")}, URL: {item.get("view_href")}')
found_match += 1
try:
response = k.put(f'/api/2.0/messages/{item.get("id")}', msg_update)
if response.get('status') == 'error':
failure_log.write(json.dumps({'id': item.get("id"), 'url': item.get("view_href")}) + "\n")
print(f"\t{response.get('message')}\n")
else:
success_log.write(json.dumps({'id': item.get("id"), 'url': item.get("view_href")}) + "\n")
except Exception as e:
print(e)
# else:
# print(f'Message {item.get("id")} didn\'t match.')
failure_log.close()
success_log.close()
print(f'Matches found this round: {found_match}')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment