Skip to content

Commit

Permalink
new poster stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
Robert Meyer committed Mar 27, 2018
1 parent dde8b9c commit e175009
Show file tree
Hide file tree
Showing 9 changed files with 195 additions and 123 deletions.
25 changes: 19 additions & 6 deletions integration_tests/bchain/postdata_test.py
Expand Up @@ -7,13 +7,18 @@
from trufflepig import config
from trufflepig.testutils import random_data
from trufflepig.testutils.pytest_fixtures import steem
from trufflepig.bchain.poster import Poster


@pytest.mark.skipif(config.PASSWORD is None, reason="needs posting key")
def test_test_top10post(steem):

steem.wallet.unlock(config.PASSWORD)

poster = Poster(steem=steem,
account=config.ACCOUNT,
waiting_time=0.1)

posts = random_data.create_n_random_posts(10)
df = pd.DataFrame(posts)
df['predicted_reward'] = df.reward
Expand All @@ -25,16 +30,20 @@ def test_test_top10post(steem):

account = config.ACCOUNT

permalink = tbpd.post_topN_list(df, steem, account, date,
permalink = tbpd.post_topN_list(df, poster, date,
overview_permalink='iii')
tbpd.comment_on_own_top_list(df, steem, account, permalink)
tbpd.comment_on_own_top_list(df, poster, permalink)


@pytest.mark.skipif(config.PASSWORD is None, reason="needs posting key")
def test_test_all_top_with_real_data(steem):

steem.wallet.unlock(config.PASSWORD)

poster = Poster(steem=steem,
account=config.ACCOUNT,
waiting_time=0.1)

df = tpbg.scrape_hour_data(steem, stop_after=10)

df = tppp.preprocess(df)
Expand All @@ -46,10 +55,10 @@ def test_test_all_top_with_real_data(steem):

account = config.ACCOUNT

permalink = tbpd.post_topN_list(df, steem, account, date,
permalink = tbpd.post_topN_list(df, poster, date,
overview_permalink='jjj')
tbpd.comment_on_own_top_list(df, steem, account, permalink)
tbpd.vote_and_comment_on_topK(df, steem, account, permalink, K=1,
tbpd.comment_on_own_top_list(df, poster, permalink)
tbpd.vote_and_comment_on_topK(df, poster, permalink, K=1,
overview_permalink='jjj')


Expand All @@ -58,6 +67,10 @@ def test_test_top20_vote_and_comment(steem):

steem.wallet.unlock(config.PASSWORD)

poster = Poster(steem=steem,
account=config.ACCOUNT,
waiting_time=0.1)

posts = random_data.create_n_random_posts(10)
df = pd.DataFrame(posts)
df['predicted_reward'] = df.reward
Expand All @@ -67,7 +80,7 @@ def test_test_top20_vote_and_comment(steem):

account = config.ACCOUNT

tbpd.vote_and_comment_on_topK(df, steem, account, 'laida',
tbpd.vote_and_comment_on_topK(df, poster, 'laida',
overview_permalink='lll')


Expand Down
5 changes: 3 additions & 2 deletions integration_tests/bchain/postweeklyupdate_test.py
Expand Up @@ -8,6 +8,7 @@
from trufflepig import config
from trufflepig.testutils.random_data import create_n_random_posts
from trufflepig.testutils.pytest_fixtures import steem
from trufflepig.bchain.poster import Poster


def test_statistics():
Expand Down Expand Up @@ -71,9 +72,9 @@ def test_weekly_post(steem):
post_frame['sbd_bought_reward'] = 0
post_frame['bought_votes'] = 0

poster = Poster(account=config.ACCOUNT, steem=steem)
permalink = tppw.post_weakly_update(pipeline, post_frame,
account=config.ACCOUNT,
steem=steem,
poster=poster,
current_datetime=current_date)

assert permalink
19 changes: 14 additions & 5 deletions integration_tests/pigonduty_test.py
Expand Up @@ -5,26 +5,35 @@

from trufflepig import config
import trufflepig.pigonduty as tppd
from trufflepig.bchain.poster import Poster


@pytest.mark.skipif(config.PASSWORD is None, reason="needs posting key")
def test_call_a_pig(steem):
current_datetime = '2018-03-03-18:21:30'

pipeline = MockPipeline()
tppd.call_a_pig(steem=steem,account='trufflepig',
poster = Poster(steem=steem,
account=config.ACCOUNT,
waiting_time=0.1)

tppd.call_a_pig(poster=poster,
pipeline=pipeline, topN_permalink='www.test.com',
current_datetime=current_datetime, hours=0.1,
sleep_time=0.1, overview_permalink='dsfd')
overview_permalink='dsfd')


@pytest.mark.skipif(config.PASSWORD is None, reason="needs posting key")
def test_call_a_pig_empty_frame(steem):
aacs = (('smcaterpillar','question-is-there-an-api-to-upload-images-to-steemit'),)

poster = Poster(steem=steem,
account=config.ACCOUNT,
waiting_time=0.51)

pipeline = MockPipeline()
tppd.execute_call(comment_authors_and_permalinks=aacs,
steem=steem,account='trufflepig',
pipeline=pipeline, topN_permalink='www.test.com',
max_comments=1000, sleep_time=0.1,
overview_permalink='jdsakd')
max_comments=1000,
overview_permalink='jdsakd',
poster=poster)
61 changes: 25 additions & 36 deletions trufflepig/bchain/postdata.py
Expand Up @@ -9,6 +9,7 @@
import trufflepig.bchain.getdata as tfgd
import trufflepig.filters.textfilters as tftf
from trufflepig.utils import error_retry
from trufflepig.bchain.poster import Poster


logger = logging.getLogger(__name__)
Expand All @@ -17,15 +18,14 @@
PERMALINK_TEMPLATE = 'daily-truffle-picks-{date}'


def post_topN_list(sorted_post_frame, steem, account,
def post_topN_list(sorted_post_frame, poster,
current_datetime, overview_permalink, N=10):
""" Post the toplist to the blockchain
Parameters
----------
sorted_post_frame: DataFrame
steem: Steem object
account: str
poster: Poster
current_datetime: datetime
N: int
Size of top list
Expand All @@ -40,8 +40,8 @@ def post_topN_list(sorted_post_frame, steem, account,
logger.info('Creating top {} post'.format(N))
df.first_image_url = df.body.apply(lambda x: tftf.get_image_urls(x))

steem_per_mvests = Converter(steem).steem_per_mvests()
truffle_link = 'https://steemit.com/steemit/@{}/{}'.format(account,
steem_per_mvests = Converter(poster.steem).steem_per_mvests()
truffle_link = 'https://steemit.com/steemit/@{}/{}'.format(poster.account,
overview_permalink)

title, body = tfbp.topN_post(topN_authors=df.author,
Expand All @@ -59,25 +59,23 @@ def post_topN_list(sorted_post_frame, steem, account,
logger.info('Posting top post with permalink: {}'.format(permalink))
logger.info(title)
logger.info(body)
error_retry(steem.commit.post)(author=account,
title=title,
body=body,
permlink=permalink,
self_vote=True,
tags=tfbp.TAGS)
poster.post(body=body,
permalink=permalink,
title=title,
tags=tfbp.TAGS,
self_vote=True)

return permalink


def comment_on_own_top_list(sorted_post_frame, steem, account,
def comment_on_own_top_list(sorted_post_frame, poster,
topN_permalink, Kstart=10, Kend=25):
""" Adds the more ranks as a comment
Parameters
----------
sorted_post_frame: DataFrame
steem: Steem object
account: str
poster: Poster
topN_permalink: str
Kstart: int
Kend: int
Expand All @@ -95,36 +93,32 @@ def comment_on_own_top_list(sorted_post_frame, steem, account,

logger.info('Commenting on top {} post with \n '
'{}'.format(topN_permalink, comment))
time.sleep(25)
try:
post = Post('@{}/{}'.format(account, topN_permalink), steem)
# to pass around the no broadcast setting otherwise it is lost
# see https://github.com/steemit/steem-python/issues/155
post.commit.no_broadcast = steem.commit.no_broadcast
error_retry(post.reply)(body=comment, author=account)
poster.reply(body=comment,
parent_author=poster.account,
parent_permalink=topN_permalink)
except PostDoesNotExist:
logger.exception('No broadcast, heh?')


def vote_and_comment_on_topK(sorted_post_frame, steem, account,
def vote_and_comment_on_topK(sorted_post_frame, poster,
topN_permalink, overview_permalink, K=25):
"""
Parameters
----------
sorted_post_frame: DataFrame
steem: Steem object
account: str
poster: Poster,
topN_permalink: str
K: int
number of truffles to comment and upvote
"""
logger.info('Voting and commenting on {} top truffles'.format(K))
weight = min(800.0 / K, 100)
topN_link = 'https://steemit.com/@{author}/{permalink}'.format(author=account,
topN_link = 'https://steemit.com/@{author}/{permalink}'.format(author=poster.account,
permalink=topN_permalink)
truffle_link = 'https://steemit.com/steemit/@{}/{}'.format(account,
truffle_link = 'https://steemit.com/steemit/@{}/{}'.format(poster.account,
overview_permalink)

for kdx, (_, row) in enumerate(sorted_post_frame.iterrows()):
Expand All @@ -138,26 +132,21 @@ def vote_and_comment_on_topK(sorted_post_frame, steem, account,
rank=kdx + 1,
topN_link=topN_link,
truffle_link=truffle_link)
post = Post('@{}/{}'.format(row.author, row.permalink), steem)
# to pass around the no broadcast setting otherwise it is lost
# see https://github.com/steemit/steem-python/issues/155
post.commit.no_broadcast = steem.commit.no_broadcast

# Wait a bit Steemit nodes hate comments in quick succession
time.sleep(25)

post.upvote(weight=weight, voter=account)
post.reply(body=reply, author=account)
poster.reply(body=reply,
parent_author=row.author,
parent_permalink=row.permalink,
parent_vote_weight=weight)
except PostDoesNotExist:
logger.exception('Post not found of row {}'.format(row))
except VotingInvalidOnArchivedPost:
logger.exception('Post archived of row {}'.format(row))
except RPCError:
logger.exception('Could not post row {}. Reconnecting...'.format(row))
steem.reconnect()
poster.steem.reconnect()
except Exception:
logger.exception('W00t? row: {}. Reconnecting...'.format(row))
steem.reconnect()
poster.steem.reconnect()


def create_wallet(steem, password, posting_key,
Expand Down
82 changes: 82 additions & 0 deletions trufflepig/bchain/poster.py
@@ -0,0 +1,82 @@
import time
import logging

from steem.account import Account
from trufflepig.utils import error_retry

logger = logging.getLogger(__name__)


WAIT = 20.02

THRESHOLD = 'threshold'


class Poster(object):
"""A class to allow for posting and taking care of posting intervals"""
def __init__(self, steem, account, self_vote_limit=94, waiting_time=WAIT):
self.waiting_time = waiting_time
self.last_post_time = time.time() - self.waiting_time
self.steem = steem
self.account = account
self.self_vote_limit=self_vote_limit
logger.info('Poster ready for account {}, waiting time {} '
'and limit {}.'.format(account, waiting_time, self_vote_limit))

def check_if_self_vote(self):
voting_power = self.get_voting_power()
return voting_power >= self.self_vote_limit

def get_voting_power(self):
acc = Account(self.account, self.steem)
return acc.voting_power()

def post(self, body, title, permalink, tags, self_vote=False):
if self_vote == THRESHOLD:
self_vote = self.check_if_self_vote()
self.time2last_post()
logger.info('Posting: `{}` (`{}`)\n{}'.format(title, permalink, body))
return error_retry(self.steem.commit.post, retries=10,
sleep_time=4, errors=Exception)(author=self.account,
title=title,
body=body,
permlink=permalink,
self_vote=self_vote,
tags=tags)

def time2last_post(self):
now = time.time()
diff = now - self.last_post_time
if diff < self.waiting_time:
time.sleep(self.waiting_time - diff)
self.last_post_time = time.time()

def vote(self, author, permalink, weight):
identifier = '@{author}/{permalink}'.format(author=author,
permalink=permalink)
logger.info('Voting on {} with weight {}.'.format(identifier,weight))
error_retry(self.steem.commit.vote, retries=5,
sleep_time=4)(identifier=identifier,
weight=weight,
account=self.account)

def reply(self, body, parent_author, parent_permalink, self_vote=False,
parent_vote_weight=0):
identifier = '@{author}/{permalink}'.format(author=parent_author,
permalink=parent_permalink)

if parent_vote_weight:
self.vote(parent_author, parent_permalink, parent_vote_weight)

if self_vote == THRESHOLD:
self_vote = self.check_if_self_vote()

self.time2last_post()
logger.info('Replying to {} with\n{}'.format(identifier, body))
return error_retry(self.steem.commit.post, retries=10,
sleep_time=4, errors=Exception)("",
body,
json_metadata=None,
author=self.account,
reply_identifier=identifier,
self_vote=self_vote)

0 comments on commit e175009

Please sign in to comment.