Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get comment count in Disqus?

I'd like to store the Disqus comment count on my own database, so that I can sort my articles by comment count. Basically, every time a page is read on my site, I'd like to ask Disqus how many comments that certain page has, then update the database with that count.

http://docs.disqus.com/help/3/ doesn't seem to be helpful.

Any suggestions?

like image 282
Obay Avatar asked Feb 05 '12 14:02

Obay


3 Answers

Get comment counts with disqus API

Here's what you'll need to have done before starting:

Register for a Disqus API key (optional) Have your own site to replace the example data

NOTE: The URL you use must match what's set as the URL in Disqus. See Web Integration docs for information on setting this up reliably.

Example HTML

<!DOCTYPE html>
<html>
    <head>
        <title>Disqus Comment Counts Example</title>
    </head>
    <body>
        <h1>Comment Counts Example</h1>
        <div>
            <a href="http://thenextweb.com/google/2013/05/03/fullscreen-beam-launches-first-youtube-app-for-google-glass-with-public-or-private-sharing/">
                <h2>Fullscreen BEAM: The first YouTube app for Google Glass comes with public or private sharing</h2>
                <div class="count-comments" data-disqus-url="http://thenextweb.com/google/2013/05/03/fullscreen-beam-launches-first-youtube-app-for-google-glass-with-public-or-private-sharing/"></div>
            </a>
        </div>
        <div>
            <a href="http://thenextweb.com/apps/2013/05/04/traktor-dj/">
                <h2>Traktor DJ: Native Instruments remixes its impressive DJ software for iPhone</h2>
                <div class="count-comments" data-disqus-url="http://thenextweb.com/apps/2013/05/04/traktor-dj/"></div>
            </a>
        </div>
        <div>
            <a href="http://thenextweb.com/video/2013/05/04/ninja-innovation-in-the-21st-century-with-gary-shapiro-of-the-consumer-electronics-association-at-tnw2013-video/">
                <h2>Ninja innovation in the 21st Century with the Consumer Electronics Association&#8217;s Gary Shapiro [Video]</h2>
                <div class="count-comments" data-disqus-url="http://thenextweb.com/video/2013/05/04/ninja-innovation-in-the-21st-century-with-gary-shapiro-of-the-consumer-electronics-association-at-tnw2013-video/"></div>
            </a>
        </div>
        <button type="button" id="get-counts-button">Get Comment Counts</button>
    </body>
</html>

Variables:

<script src="http://code.jquery.com/jquery-1.8.3.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
  var disqusPublicKey = "YOUR_PUBLIC_KEY";
  var disqusShortname = "thenextweb"; // Replace with your own shortname

  var urlArray = [];
  $('.count-comments').each(function () {
    var url = $(this).attr('data-disqus-url');
    urlArray.push('thread:link='+url);
  });
});
</script>

Making the Request API

$('#get-counts-button').click(function () {

    $.ajax({
        type: 'GET',
        url: 'https://disqus.com/api/3.0/threads/set.json?'+urlArray.join('&')+'&forum='+disqusShortname+'&api_key='+disqusPublicKey,
        cache: false,
        dataType: 'json',
        success: function (result) {

          for (var i in result.response) {

            var countText = " comments";
            var count = result.response[i].posts;

            if (count == 1) {
              countText = " comment";
            }

            $('[data-disqus-url="' + result.response[i].link + '"]').html('<h4>' + count + countText + '</h4>');
          }
        }
    });
});
like image 64
3 revs, 3 users 86% Avatar answered Oct 20 '22 18:10

3 revs, 3 users 86%


Disqus have web api which allows developers to communicate with Disqus data from within their own applications.

http://disqus.com/api/docs/

http://disqus.com/api/docs/forums/listThreads/

Also you can use http://disqus.com/api/console/ to test api

I use https://github.com/disqus/disqus-php

require('disqusapi/disqusapi.php');
$disqus = new DisqusAPI('yoursecretkey');
print_r($disqus->forums->listThreads(array('forum'=>'your_ shortname')));
like image 3
Nish Avatar answered Oct 20 '22 17:10

Nish


I used this to get the comment count:

http://help.disqus.com/customer/portal/articles/565624

It's updates a link you set in page: Second article

The content of the link 'Second article' will be replaced with the comment count. i.e "22 Comments". Than use ajax to update you're db with the comment count.

like image 1
Grig Avatar answered Oct 20 '22 18:10

Grig