<s> from django . conf import settings <EOL> from django . contrib . comments . models import Comment <EOL> from django . contrib . contenttypes . models import ContentType <EOL> from django . contrib . sites . models import Site <EOL> from django . core . management . base import NoArgsCommand <EOL> from articles . models import Article <EOL> import simplejson as json <EOL> import re <EOL> import string <EOL> import sys <EOL> import urllib <EOL> import urllib2 <EOL> NONPRINTABLE_RE = re . compile ( '<STR_LIT>' % string . printable ) <EOL> class Command ( NoArgsCommand ) : <EOL> help = """<STR_LIT>""" <EOL> _forum_api_key = { } <EOL> def handle_noargs ( self , ** opts ) : <EOL> if hasattr ( settings , '<STR_LIT>' ) : <EOL> self . forum_id = self . determine_forum ( ) <EOL> self . import_comments ( self . forum_id ) <EOL> else : <EOL> sys . exit ( '<STR_LIT>' ) <EOL> def get_value_from_api ( self , url , args = { } , method = '<STR_LIT:GET>' ) : <EOL> params = { <EOL> '<STR_LIT>' : settings . DISQUS_USER_API_KEY , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> params . update ( args ) <EOL> for key , val in params . items ( ) : <EOL> if isinstance ( val , ( str , unicode ) ) : <EOL> params [ key ] = NONPRINTABLE_RE . sub ( '<STR_LIT>' , val ) <EOL> data = urllib . urlencode ( params ) <EOL> additional = '<STR_LIT>' <EOL> if method != '<STR_LIT:POST>' : <EOL> additional = '<STR_LIT>' % data <EOL> data = None <EOL> url = '<STR_LIT>' % ( url , additional ) <EOL> try : <EOL> handle = urllib2 . urlopen ( url , data ) <EOL> except urllib2 . HTTPError , err : <EOL> print '<STR_LIT>' % ( method , url , args ) <EOL> return None <EOL> else : <EOL> json_obj = json . loads ( handle . read ( ) ) [ '<STR_LIT:message>' ] <EOL> handle . close ( ) <EOL> return json_obj <EOL> def determine_forum ( self ) : <EOL> forums = self . get_value_from_api ( '<STR_LIT>' ) <EOL> if len ( forums ) == <NUM_LIT:0> : <EOL> sys . exit ( '<STR_LIT>' ) <EOL> elif len ( forums ) == <NUM_LIT:1> : <EOL> forum_id = forums [ <NUM_LIT:0> ] [ '<STR_LIT:id>' ] <EOL> else : <EOL> possible_ids = tuple ( forum [ '<STR_LIT:id>' ] for forum in forums ) <EOL> forum_id = None <EOL> while forum_id not in possible_ids : <EOL> if forum_id is not None : <EOL> print '<STR_LIT>' <EOL> print '<STR_LIT>' <EOL> for forum in forums : <EOL> print '<STR_LIT>' % ( forum [ '<STR_LIT:id>' ] , forum [ '<STR_LIT:name>' ] ) <EOL> forum_id = raw_input ( '<STR_LIT>' ) <EOL> return forum_id <EOL> @ property <EOL> def forum_api_key ( self ) : <EOL> if not self . _forum_api_key . get ( self . forum_id , None ) : <EOL> self . _forum_api_key [ self . forum_id ] = self . get_value_from_api ( '<STR_LIT>' , { '<STR_LIT>' : self . forum_id } ) <EOL> return self . _forum_api_key [ self . forum_id ] <EOL> def import_comments ( self , forum_id ) : <EOL> print '<STR_LIT>' % self . forum_id <EOL> article_ct = ContentType . objects . get_for_model ( Article ) <EOL> for comment in Comment . objects . filter ( content_type = article_ct ) : <EOL> article = comment . content_object <EOL> thread_obj = self . get_value_from_api ( '<STR_LIT>' , { '<STR_LIT>' : article . id , '<STR_LIT:title>' : article . title , '<STR_LIT>' : self . forum_api_key } , method = '<STR_LIT:POST>' ) <EOL> thread = thread_obj [ '<STR_LIT>' ] <EOL> if thread_obj [ '<STR_LIT>' ] : <EOL> self . get_value_from_api ( '<STR_LIT>' , { <EOL> '<STR_LIT>' : self . forum_api_key , <EOL> '<STR_LIT>' : thread [ '<STR_LIT:id>' ] , <EOL> '<STR_LIT:title>' : article . title , <EOL> '<STR_LIT:url>' : '<STR_LIT>' % ( Site . objects . get_current ( ) . domain , article . get_absolute_url ( ) ) , <EOL> } , method = '<STR_LIT:POST>' ) <EOL> print '<STR_LIT>' % article . title <EOL> comment_obj = self . get_value_from_api ( '<STR_LIT>' , { <EOL> '<STR_LIT>' : thread [ '<STR_LIT:id>' ] , <EOL> '<STR_LIT:message>' : comment . comment , <EOL> '<STR_LIT>' : comment . user_name , <EOL> '<STR_LIT>' : comment . user_email , <EOL> '<STR_LIT>' : self . forum_api_key , <EOL> '<STR_LIT>' : comment . submit_date . strftime ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : comment . ip_address , <EOL> '<STR_LIT>' : comment . user_url , <EOL> '<STR_LIT:state>' : self . get_state ( comment ) <EOL> } , method = '<STR_LIT:POST>' ) <EOL> print '<STR_LIT>' % ( article , comment . user_name , comment . submit_date ) <EOL> def get_state ( self , comment ) : <EOL> """<STR_LIT>""" <EOL> if comment . is_public and not comment . is_removed : <EOL> return '<STR_LIT>' <EOL> elif comment . is_public and comment . is_removed : <EOL> return '<STR_LIT>' <EOL> elif not comment . is_public and not comment . is_removed : <EOL> return '<STR_LIT>' <EOL> else : <EOL> return '<STR_LIT>' </s>
