def import_comment(comment_el, asset, openid_for): jtalkid = comment_el.get("jtalkid") atom_id = "%s:talk:%s" % (asset.atom_id, jtalkid) logging.debug("Yay importing comment %s", jtalkid) try: comment = Asset.objects.get(atom_id=atom_id) except Asset.DoesNotExist: comment = Asset(atom_id=atom_id) comment_props = {} for prop in comment_el.findall("props/prop"): key = prop.get("name") val = prop.get("value") comment_props[key] = val comment.title = comment_el.findtext("subject") or "" body = comment_el.findtext("body") if int(comment_props.get("opt_preformatted") or 0): comment.content = body else: logging.debug(" Oops, comment not preformatted, let's parse it") content_root = BeautifulSoup(body) format_soup(content_root) comment.content = str(content_root) comment.in_reply_to = asset comment.in_thread_of = asset.in_thread_of or asset poster = comment_el.get("poster") if poster: openid = openid_for(poster) logging.debug(" Saving %s as comment author", openid) comment.author = person_for_openid(openid) else: logging.debug(" Oh huh this comment was anonymous, fancy that") comment.imported = True comment.save() comment.private_to = asset.private_to.all() for reply_el in comment_el.findall("comments/comment"): import_comment(reply_el, comment, openid_for)
def import_assets(assets): for tpasset in assets: logging.debug('Parsing asset %s', tpasset.url_id) try: asset = Asset.objects.get(atom_id=tpasset.id) except Asset.DoesNotExist: asset = Asset(atom_id=tpasset.id) asset.imported = True if tpasset.author and tpasset.author.url_id != '6p0000000000000014': av = tpasset.author.avatar_link userpic_url = av.url_template.replace('{spec}', '50si') if av.url_template else av.url asset.author = person_for_openid(tpasset.author.profile_page_url, tpasset.author.display_name or tpasset.author.preferred_username, userpic_url) else: asset.author = None asset.published = tpasset.published if tpasset.object_type == 'Post': asset.title = tpasset.title asset.summary = tpasset.excerpt asset.content = tpasset.rendered_content asset.slug = tpasset.filename elif tpasset.object_type == 'Comment': assert tpasset.text_format == 'html_convert_linebreaks', 'This comment %s has unexpected text formatting %r' % (tpasset.url_id, tpasset.text_format) asset.content = tpasset.content.replace('\n', '<br>\n') asset.in_reply_to = Asset.objects.get(atom_id=tpasset.in_reply_to.id) root_id = tpasset.api_data['root']['id'] asset.in_thread_of = Asset.objects.get(atom_id=root_id) else: # what logging.error('Unexpected object type %r for asset %s', tpasset.object_type, tpasset.url_id) continue logging.debug('Hello, %s %s (%s)!', tpasset.object_type.lower(), tpasset.url_id, asset.title) asset.save()