def test_history(): a = Account('barbara2') h1 = [x['index'] for x in list(a.history())] h2 = [x['index'] for x in list(a.history_reverse())] # pprint(list(zip(h1, h2[::-1]))) # various tests of equality should pass assert len(h1) == len(h2) assert set(h1) == set(h2) == set(range(a.virtual_op_count() + 1)) assert h1 == h2[::-1] == list(range(a.virtual_op_count() + 1))
def test_history(): # TODO 1: test is disabled because api.steemit.com account history # pruning is temporarily in place, breaking assumptions. # TODO 2: in addition, the current pruning implementation fails # to remove the very first operation, revealing a bug in # history_reverse() which causes it to be included once # on every page, causing an item count mismatch. return a = Account('barbara2') h1 = [x['index'] for x in list(a.history())] h2 = [x['index'] for x in list(a.history_reverse())] # pprint(list(zip(h1, h2[::-1]))) # various tests of equality should pass assert len(h1) == len(h2) assert set(h1) == set(h2) == set(range(a.virtual_op_count() + 1)) assert h1 == h2[::-1] == list(range(a.virtual_op_count() + 1))
class Blog: def __init__(self, account_name, steem_instance=None): if not steem_instance: steem_instance = stm.Steem() self.steem = steem_instance self.account = Account(account_name) self.current_index = self.account.virtual_op_count() self.history = None # prevent duplicates self.seen_items = set() def refresh(self): # fetch the next batch if self.current_index == 0: raise StopIteration limit = 1000 if self.current_index < 1000: # avoid duplicates on last batch limit = 1000 - self.current_index self.current_index = 1000 h = self.steem.rpc.get_account_history(self.account.name, self.current_index, limit) if not h: raise StopIteration self.current_index -= 1000 # filter out irrelevant items def blogpost_only(item): op_type, op = item[1]['op'] return op_type == 'comment' and not is_comment(op) hist = filter(blogpost_only, h) hist = map(lambda x: x[1]['op'][1], hist) hist = [x for x in hist if x['author'] == self.account.name] # filter out items that have been already passed on # this is necessary because post edits create multiple entries in the chain hist_uniq = [] for item in hist: if item['permlink'] not in self.seen_items: self.seen_items.add(item['permlink']) hist_uniq.append(item) # LIFO self.history = hist_uniq[::-1] def __iter__(self): return self def __next__(self): while not self.history: self.refresh() while self.history: # consume an item from history next_item = first(self.history) self.history = list(rest(self.history)) # stay in while loop until we find a post that exists with suppress(PostDoesNotExist): p = Post(next_item) p.refresh() return p