def upsert_comment(mongo, identifier): """ Upsert root post or comment. """ with suppress(PostDoesNotExist): p = Post(identifier) if p.is_comment(): return mongo.Comments.update({'identifier': p.identifier}, p.export(), upsert=True) return mongo.Posts.update({'identifier': p.identifier}, p.export(), upsert=True)
def test_post_refresh(): """ Post should load correctly if passed a dict or string identifier. """ p1 = Post('https://steemit.com/marketing/@steemitblog/' 'marketing-w-mitchell-a-steem-ecosystem') p2 = Post({ 'author': 'steemitblog', 'permlink': 'marketing-w-mitchell-a-steem-ecosystem' }) # did post load? assert 'json_metadata' in p1 and 'json_metadata' in p2 # are posts the same assert p1.export() == p2.export()
def upsert_comment(mongo, identifier): """ Upsert root post or comment. """ with suppress(PostDoesNotExist): p = Post(identifier) update = {'$set': strip_dot_from_keys(p.export())} if p.is_comment(): return mongo.Comments.update( {'identifier': p.identifier}, update, upsert=True) return mongo.Posts.update({'identifier': p.identifier}, update, upsert=True)
def upsert_post(mongo, post_identifier, steem=None): with suppress(PostDoesNotExist): p = Post(post_identifier, steem_instance=steem) # scrape post and its replies entry = { **p.export(), 'replies': [], # 'replies': [x.export() for x in _fetch_comments_flat(p)], } return mongo.Posts.update({'identifier': p.identifier}, entry, upsert=True)
def upsert_comment_chain(mongo, identifier, recursive=False): """ Upsert given comments and its parent(s). Args: mongo: mongodb instance identifier: Post identifier recursive: (Defaults to False). If True, recursively update all parent comments, incl. root post. """ with suppress(PostDoesNotExist): p = Post(identifier) if p.is_comment(): mongo.Comments.update({'identifier': p.identifier}, p.export(), upsert=True) parent_identifier = '@%s/%s' % (p.parent_author, p.parent_permlink) if recursive: upsert_comment_chain(mongo, parent_identifier, recursive) else: upsert_comment(mongo, parent_identifier) else: return mongo.Posts.update({'identifier': p.identifier}, p.export(), upsert=True)