Django: Export comments to Disqus WXR / XML format

0 Comments

With the help of the django-disqus module, it's pretty easy to export our comments out to Disqus.

First you'll need to install the module django-disqus (v0.4.3 at time of writing).

The example provided was used when I switched www.thatawesomeshirt.com over to Disqus. The class used is loosely based off the regular class used in the syndication module.

urls.py

1.from tas.feeds import ShirtCommentsWxrFeed   
2.     
3.urlpatterns += patterns('',    
4.  ('^export/comments/', ShirtCommentsWxrFeed()),    
5.)

feeds.py

from disqus.wxr_feed import ContribCommentsWxrFeed
class ShirtCommentsWxrFeed(ContribCommentsWxrFeed):
def get_object(self, request): #Fetch the current site
from django.contrib.sites.models import Site
self.site = Site.objects.get_current()
return self.site
def items(self, obj):
# Fetch all shirts which have valid comments
return Shirt.objects.valid().filter(pk__in = Shirt.objects.filter(comments__site = obj))
def item_title(self, item):
print "*** title", item.title
return item.title
def item_link(self, item):
site = self.site
url = Url(item.get_absolute_url())
url.domain = self.site.domain
return url.url
def item_comment_status(self, item):
return "open"
def item_pubdate(self, item):
return item.created
def item_guid(self, item):
"""
The path to the comments thread.
"""
return "/shirt/{0}".format(item.pk) # aka item.disqus_identifier
def item_comments(self, item):
# Select the valid comments for this shirt
from django.contrib.comments.models import Comment
return Comment.objects.for_model(item).filter(is_public = True, is_removed = False)
# Comment information
def comment_id(self, comment):
print "comment", comment.pk
return comment.pk
def comment_user_id(self, comment):
return comment.user.pk if comment.user else 0
def comment_user_name(self, comment):
return comment.user_name
def comment_user_email(self, comment):
if comment.user:
return comment.user.email.lower()
else:
return comment.user_email.lower()
def comment_user_url(self, comment):
# Comment author's homepage URL
return comment.user_url
def comment_ip_address(self, comment):
return comment.ip_address
def comment_submit_date(self, comment):
return comment.submit_date
def comment_comment(self, comment):
return comment.comment
def comment_is_approved(self, comment):
return '1' # '0' for False
def comment_parent(self, comment):
# Should match comment_id() if you support it
return 0
view raw feeds.py hosted with ❤ by GitHub

As you can see, this is where all the heavy lifting happens.

Once you have the output, you can import it more than once without creating duplicates. I believe duplicate detection is done using the value from comment_id().

image

In your Disqus admin:

  • Go to Discussions > Import > Generic (WXR) (using WordPress one will give you strange errors regarding the thread)
  • Upload the WXR file generated
  • Wait until it's done

I've noticed that files which are less than 10mb are processed rather quickly. Anything closer to the 50mb limit will take almost 24hrs to process.

All in all, it's probably one of the best processes for migrating comments that I've used so far.

like-a-boss_o_177339

Almost as good as this guy.

Sources

 
Copyright © Twig's Tech Tips
Theme by BloggerThemes & TopWPThemes Sponsored by iBlogtoBlog