import logging from pg_discuss.app import app_factory app = app_factory() if __name__ == '__main__': app.script_manager.run() else: # If not running a management command, set log level and log configuration. # Set up logging if not app.debug: app.logger.addHandler(logging.StreamHandler()) app.logger.setLevel(app.config['LOGLEVEL']) # Log configuration parameters app.logger.info('Configuration parameters:\n{}'.format( '\n'.join([k + '=' + str(v) for k, v in sorted(app.config.items()) if k not in app.config['DO_NOT_LOG_VARS']]))) app.logger.info('Found extensions/drivers:\n{}'.format( '\n'.join([e.name for e in app.ext_mgr_all.extensions]))) app.logger.info('Enabled IdentityPolicy driver:\n{}'.format( app.identity_policy_loader.extensions[0].name)) app.logger.info('Enabled CommentRenderer driver:\n{}'.format( app.comment_renderer_loader.extensions[0].name))
def load_got_six_weeks(): app = app_factory() thread_cid = 'got_six_weeks' thread_id = 1 to_insert = [] # First, change comment.id to a bigint to handle reddit's 36-bit # ids. Note this will succeed even when the column is already a bigint. with app.app_context(): db.engine.execute(sa.text(''' ALTER TABLE comment ALTER COLUMN id TYPE bigint ''')) db.engine.execute(sa.text(''' ALTER TABLE comment ALTER COLUMN parent_id TYPE bigint ''')) db.engine.execute(sa.text(''' ALTER TABLE identity_comment ALTER COLUMN comment_id TYPE bigint ''')) with open('got_six_weeks.json') as f: comments = json.loads(f.read()) submission_id = None for i, c in enumerate(comments): id = int(c['id'], 36) text = c['body'] created = datetime.datetime.fromtimestamp(c['created']) custom_json = { 'author': c['author'], } if c['author'] != '[deleted]': custom_json['hash'] = hash(c['author']) comment_to_insert = { 'id': id, 'thread_id': thread_id, 'text': text, 'created': created, 'custom_json': custom_json, } # Ignore parent_id for comments which reply to the submission. # The first comment is a reply to the submission, so we # can track the submission id that way. if i == 0: submission_id = c['parent_id'] comment_to_insert['parent_id'] = None elif c['parent_id'] == submission_id: comment_to_insert['parent_id'] = None else: parent_id = int(c['parent_id'][3:], 36) comment_to_insert['parent_id'] = parent_id to_insert.append(comment_to_insert) db.engine.execute( tables.thread.insert(), **{ 'id': 1, 'client_id': thread_cid, }) db.engine.execute( tables.comment.insert(), to_insert, )