<s> from __future__ import unicode_literals <EOL> from datetime import datetime <EOL> import json <EOL> import time <EOL> from optparse import make_option <EOL> from django . core . management . base import CommandError <EOL> from mezzanine . blog . management . base import BaseImporterCommand <EOL> class Command ( BaseImporterCommand ) : <EOL> """<STR_LIT>""" <EOL> option_list = BaseImporterCommand . option_list + ( <EOL> make_option ( "<STR_LIT>" , "<STR_LIT>" , dest = "<STR_LIT>" , <EOL> help = "<STR_LIT>" ) , <EOL> make_option ( "<STR_LIT>" , "<STR_LIT>" , dest = "<STR_LIT:username>" , <EOL> help = "<STR_LIT>" ) , <EOL> make_option ( "<STR_LIT>" , "<STR_LIT>" , dest = "<STR_LIT:password>" , <EOL> help = "<STR_LIT>" ) , <EOL> make_option ( "<STR_LIT>" , "<STR_LIT>" , dest = "<STR_LIT>" , <EOL> help = "<STR_LIT>" ) , <EOL> ) <EOL> help = "<STR_LIT>" <EOL> def request ( self , path , data = None ) : <EOL> try : <EOL> import requests <EOL> except ImportError : <EOL> raise CommandError ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> data = data or { } <EOL> params = { <EOL> '<STR_LIT>' : self . api_token <EOL> } <EOL> params . update ( data ) <EOL> url = "<STR_LIT>" % path <EOL> r = requests . get ( url , <EOL> data = params , <EOL> auth = ( self . username , self . password ) <EOL> ) <EOL> if r . text . startswith ( "<STR_LIT>" ) : <EOL> raise CommandError ( r . text ) <EOL> try : <EOL> response = json . loads ( r . text ) <EOL> return response <EOL> except : <EOL> raise CommandError ( r . text ) <EOL> def handle_import ( self , options ) : <EOL> self . api_token = options . get ( "<STR_LIT>" ) <EOL> self . username = options . get ( "<STR_LIT:username>" ) <EOL> self . password = options . get ( "<STR_LIT:password>" ) <EOL> hostname = options . get ( "<STR_LIT>" ) <EOL> sites = self . request ( '<STR_LIT>' ) <EOL> site = None <EOL> for s in sites : <EOL> if s [ '<STR_LIT>' ] == hostname : <EOL> site = s <EOL> time . sleep ( <NUM_LIT:2> ) <EOL> break <EOL> if not hostname and not site : <EOL> if len ( sites ) == <NUM_LIT:1> : <EOL> site = sites [ <NUM_LIT:0> ] <EOL> else : <EOL> raise CommandError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> ) <EOL> page = <NUM_LIT:1> <EOL> while True : <EOL> path = '<STR_LIT>' % site [ '<STR_LIT:id>' ] <EOL> time . sleep ( <NUM_LIT:2> ) <EOL> posts = self . request ( path , data = { '<STR_LIT>' : page } ) <EOL> if not posts : <EOL> break <EOL> for post in posts : <EOL> content = post [ '<STR_LIT>' ] <EOL> title = post [ '<STR_LIT:title>' ] <EOL> old_url = post [ '<STR_LIT>' ] <EOL> tags = [ t [ '<STR_LIT:name>' ] for t in post [ '<STR_LIT>' ] ] <EOL> pub_date = datetime . strptime ( <EOL> post [ '<STR_LIT>' ] [ : - <NUM_LIT:6> ] , <EOL> "<STR_LIT>" <EOL> ) <EOL> self . add_post ( <EOL> title = title , <EOL> content = content , <EOL> pub_date = pub_date , <EOL> tags = tags , <EOL> old_url = old_url <EOL> ) <EOL> if not post [ '<STR_LIT>' ] : <EOL> continue <EOL> path = "<STR_LIT>" % ( site [ '<STR_LIT:id>' ] , post [ '<STR_LIT:id>' ] ) <EOL> time . sleep ( <NUM_LIT:2> ) <EOL> comments = self . request ( path ) <EOL> for comment in comments : <EOL> post = None <EOL> email = "<STR_LIT>" <EOL> pub_date = datetime . strptime ( <EOL> comment [ '<STR_LIT>' ] [ : - <NUM_LIT:6> ] , <EOL> "<STR_LIT>" <EOL> ) <EOL> website = "<STR_LIT>" <EOL> if '<STR_LIT:user>' in comment : <EOL> website = comment [ '<STR_LIT:user>' ] [ '<STR_LIT>' ] <EOL> name = comment [ '<STR_LIT:user>' ] [ '<STR_LIT>' ] <EOL> else : <EOL> name = comment [ '<STR_LIT:name>' ] <EOL> website = "<STR_LIT>" % name <EOL> body = comment [ '<STR_LIT:body>' ] <EOL> self . add_comment ( <EOL> post = post , <EOL> name = name , <EOL> email = email , <EOL> pub_date = pub_date , <EOL> website = website , <EOL> body = body <EOL> ) <EOL> page += <NUM_LIT:1> </s>
