Skip to content

Instantly share code, notes, and snippets.

@evert
Created July 15, 2018 04:16
Show Gist options
  • Save evert/3332e6cc73848aefe36fd9d0a30ac390 to your computer and use it in GitHub Desktop.
Save evert/3332e6cc73848aefe36fd9d0a30ac390 to your computer and use it in GitHub Desktop.
Convert Disqus export to a JSON file
<?php
$xml = simplexml_load_file('php://stdin');
$threads = [];
$posts = [];
$userMap = [
'evertp' => [
'url' => 'https://evertpot.com',
'avatar' => 'https://s.gravatar.com/avatar/9e9b4f80c65dc6076d74cd421084e4fc?s=200',
]
];
foreach($xml->thread as $thread) {
$thread = [
'id' => (string)$thread->attributes('http://disqus.com/disqus-internals')['id'],
'url' => (string)$thread->link,
'post_id' => (string)$thread->id,
'comments' => [],
];
$threads[$thread['id']] = $thread;
}
foreach($xml->post as $post) {
$threadId = (string)$post->thread->attributes('http://disqus.com/disqus-internals')['id'];
$postId = (string)$post->attributes('http://disqus.com/disqus-internals')['id'];
if ($post->parent) {
$parentId = (string)$post->parent->attributes('http://disqus.com/disqus-internals')['id'];
} else {
$parentId = null;
}
$posts[$postId] = [
'id' => $postId,
'created' => (string)$post->createdAt,
'isDeleted' => (string)$post->isDeleted !== 'false',
'isClosed' => (string)$post->isClosed !== 'false',
'isSpam' => (string)$post->isSpam !== 'false',
'name' => (string)$post->author->name,
'user' => (string)$post->author->username,
'message' => (string)$post->message,
'children' => [],
];
if ($parentId) {
$posts[$parentId]['children'][] =& $posts[$postId];
} else {
$threads[$threadId]['comments'][] =& $posts[$postId];
}
}
function convertComments(array $comments) {
global $userMap;
$result = [];
foreach($comments as $comment) {
if($comment['isDeleted']) {
continue;
}
if($comment['isSpam']) {
continue;
}
$newComment = [
'created' => $comment['created'],
'name' => $comment['name'],
'disqusUser' => $comment['user'],
'message' => $comment['message'],
'children' => convertComments($comment['children']),
];
if (isset($userMap[$newComment['disqusUser']])) {
$newComment['url'] = 'https://evertpot.com/';
$newComment['avatar'] = 'https://s.gravatar.com/avatar/9e9b4f80c65dc6076d74cd421084e4fc?s=200';
}
$result[] = $newComment;
}
return $result;
}
function convertThreads(array $threads) {
$result = [];
foreach($threads as $thread) {
if (!$thread['comments']) {
continue;
}
$result[$thread['post_id']] = convertComments($thread['comments']);
}
return $result;
}
$threads = convertThreads($threads);
echo json_encode($threads, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
@sven337
Copy link

sven337 commented Feb 5, 2020

'post_id' => (string)$thread->id,
Needs to be turned into
'post_id' => (string)$thread->attributes('http://disqus.com/disqus-internals')['id'],

on recent Disqus exports. Otherwise this works well, thanks!!

@evert
Copy link
Author

evert commented Feb 5, 2020

@sven337 That doesn't seem right, as there was both a post_id and id field, and the id field already has this line:

    'id' => (string)$thread->attributes('http://disqus.com/disqus-internals')['id'],

@sven337
Copy link

sven337 commented Feb 5, 2020

I know, but it seems to work. My XML export has:

<thread dsq:id="2390033662">
<id />

The "id" for all threads is empty. Using the dsq:id seems to make the export produce something that makes sense.

EDIT: actually this is broken too, the commetns don't thread properly. I need to debug more.

@evert
Copy link
Author

evert commented Feb 5, 2020

Yea that's what I would expect to break =) there's no reference anymore to what post anything belongs to

@sven337
Copy link

sven337 commented Feb 5, 2020

Actually, this works just fine. The reason I had a threading issue was related to URIs changing on my blog.
So we're back to: my change seems to work even though I have no idea why (and don't really care to find out).

Thanks a lot for your helpful article!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment