def run(tags): log.info("Follow mode activated", tags=tags) if tags is None or len(tags) < 1: raise ValueError("You must specify at least one tag") log.debug("initializing...") steem = Steem(keys=[cred.key]) account = Account(cred.id, steem) chain = Blockchain(steem) log.debug("ready", steem=steem, account=account, blockchain=chain) log.info("Gathering our following list...") following = account.get_following() pending = [] log.info("Following list retrieved", count=len(following)) log.info("Watching for new posts...") while True: stream = map(Post, chain.stream(filter_by=['comment'])) try: for post in stream: count = len(pending) if count > 0: copy = list(pending) for i in range(count): if have_bandwidth(steem, account): user = copy[i] log.info("following user", user=user) steem.follow(user, account=cred.id) del pending[0] else: log.warn("Waiting for more bandwidth before following another user") break if post.is_main_post(): log.debug("found a top-level post", author=post.author, tags=post.tags) if post.author != cred.id: for tag in tags: if tag in post.tags: if post.author not in following: pending.append(post.author) following.append(post.author) break except PostDoesNotExist as e: log.debug("Post has vanished", exception=e) except RPCError as e: log.error("RPC problem while streaming posts", exception=e)
def watch(self, tags): log.info("Watching for new posts...") while True: stream = map(Post, self.chain.stream(filter_by=['comment'])) try: for post in stream: self.process() if self.first_vote is not None: elapsed = datetime.utcnow() - self.first_vote if self.votes_today > 11 or elapsed >= timedelta( hours=24): wait = timedelta(hours=24) - elapsed if wait.total_seconds() > 0: log.info( "Maximum votes reached for today, going to sleep now", wait=wait) sleep(wait.total_seconds()) log.info("New day!") self.first_vote = None self.votes_today = 0 self.max_payout = Decimal("0") break if post.is_main_post(): log.debug( "found a top-level post", post=post, elapsed=post.time_elapsed(), tags=post.tags, total_payout=post.get("total_payout_value"), pending_payout=post.get("pending_payout_value")) for tag in tags: if tag in post.tags: log.debug( "found a possible curation candidate", post=post) self.posts[post.identifier] = post break except PostDoesNotExist as e: log.debug("Post has vanished", exception=e) except RPCError as e: log.error("RPC problem while streaming posts", exception=e)
def talk(): text = request.args.get('text') if text is None or text == '': text = request.get_data().decode('us-ascii') #log.debug("you said: ", text) log.debug("talk", text=text, chatservice=chatservice) if chatservice is not None: try: response = chatservice.talk(text) log.info("server says: ", response) return response except: log.error() return "oops, malfunction" else: return "FlaskServer is working and your message received, but no chatbot service was provided"
def process(self): try_again = {} local_max_payout = Decimal("0") local_max_post = None for key, post in self.posts.items(): try: now = datetime.utcnow() if now - post['created'] >= timedelta(minutes=27): if now - post['created'] < timedelta(minutes=30): post.refresh() payout = Decimal( post.get("pending_payout_value").amount) if payout >= self.min_payout and payout > local_max_payout: local_max_payout = payout local_max_post = post else: # post is not mature enough yet, check it again later try_again[post.identifier] = post except PostDoesNotExist as e: log.debug("Post has vanished", exception=e) except RPCError as e: log.error("RPC problem while refreshing post", exception=e) self.posts = try_again if local_max_post is not None and local_max_payout > self.max_payout: log.info("Upvoting post #{}".format(self.votes_today + 1), post=local_max_post, elapsed=local_max_post.time_elapsed(), payout=local_max_payout) local_max_post.upvote(voter=account.id) self.max_payout = local_max_payout self.votes_today += 1 if self.first_vote is None: self.first_vote = now
log.warn("Not logging into the database.") return False def write_data(self, key, value): """Should have an H value as well.""" self.database.set(key, value) return True def read_data(self, key): """A basic read of redis data with a utf check.""" try: value = self.database.get(key).decode('UTF-8') except: try: value = self.database.get(key) except Exception as e: log.fatal("Damnit on the reverse lookup!") value = 'unk' return value if __name__ == '__main__': log.info("Starting the Database Testing") try: app = Database() if app.main(): sys.exit("Everything checks out.") except Exception as e: log.error("and thats okay too.") sys.exit(e)
assert value == new_value log.debug("Passed Stanity Check") return True def main(self): """This kind of thing should be standardized.""" if self.sanity(): # Add the path to files in the config.yaml dataset = self.dataReader.make_buckets() print(dataset) return True return False def main(): """Launcher for the app.""" options = Options() # test app = Chatter(options) if app.main(): sys.exit('Alphagriffin.com | 2017') return True if __name__ == '__main__': try: os.system('clear') main() except Exception as e: log.error(e) sys.exit("and thats okay too.")
from ag.boiler.curate import run run(argv[2:]) elif argv[1] == 'timely': from ag.boiler.timely import run run() elif argv[1] == 'market': if len(argv) < 5: usage() exit(4) from ag.boiler.market import run run(argv[2:]) elif argv[1] == 'market-ath': if len(argv) != 4: usage() exit(5) from ag.boiler.market import all_time_high all_time_high(argv[2], argv[3]) else: log.error("unknown command", command=argv[1]) print("boiler: unknown command: " + argv[1]) usage() exit(99)
def run(): log.info("Timely post mode activated") log.debug("initializing...") steem = Steem(keys=[cred.key]) account = Account(cred.id, steem) chain = Blockchain(steem) commit = Commit(steem) log.debug("ready", steem=steem, account=account, blockchain=chain, commit=commit) # Because subsequent edits to a post show up as separate post entries in the blockchain, # we'll keep a list of candidates keyed by the post identifier which the edits share. candidates = {} log.info("Checking post history...") history = map(Post, account.history(filter_by=['comment'])) # FIXME: use steem.get_posts() instead? for post in history: if post.is_main_post(): log.debug("found a top-level post", post=post, tags=post.tags) if post.tags[0] == cred.id and 'boiled' not in post.tags: candidates[post.identifier] = post if len(candidates) > 0: log.info("Found one or more historical posts to process", posts=candidates) deleting = [] for key, post in candidates.items(): result = process(commit, post) if result or result is None: deleting.append(key) for key in deleting: del candidates[key] log.info("Watching for new posts...") while True: stream = map(Post, chain.stream(filter_by=['comment'])) try: for post in stream: if post.is_main_post() and post.author == cred.id: log.debug("found a top-level post", post=post, tags=post.tags) if len(post.tags) == 2 and post.tags[0] == cred.id and post.tags[1] == cred.id: candidates[post.identifier] = post deleting = [] for key, post in candidates.items(): result = process(commit, post) if result or result is None: deleting.append(key) for key in deleting: del candidates[key] except PostDoesNotExist as e: log.debug("Post has vanished", exception=e) except RPCError as e: log.error("RPC problem while streaming posts", exception=e)
def summarize(self, title, tags): log.info("Summarizing market...", symbol=self.symbol, against=self.against) if self.testing: log.info("TESTING MODE ENABLED") ticker = self.api.ticker() try: ticker = ticker[self.against + '_' + self.symbol] except KeyError as e: log.error("Currency pair not found in ticker data", symbol=self.symbol, against=self.against, exception=e) raise ValueError("Currency pair not found in ticker data") tz = get_localzone() now = datetime.now(tz) nowstr = now.strftime('%Y-%m-%d %H:%M:%S %Z') log.debug("got ticker data", now=nowstr, ticker=ticker) last = Decimal(ticker['last']) if self.against == 'USDT' or self.against == 'USD': symbol = '$' quant = Decimal('0.00') else: symbol = '' quant = Decimal('0.00000000') laststr = symbol + str(last.quantize(quant)) log.debug("last trade", value=laststr) ath = None newath = False nowfile = path.join( dir, 'market.' + self.symbol + '-' + self.against + '.time') lastfile = path.join( dir, 'market.' + self.symbol + '-' + self.against + '.last') img_url = None if path.exists(nowfile) and path.exists(lastfile): prev = True with open(nowfile, 'r') as infile: prev_now = datetime.fromtimestamp(int( infile.readline().strip()), tz=tz) with open(lastfile, 'r') as infile: prev_last = Decimal(infile.readline().strip()) prev_permlink = self.make_permlink(prev_now) prev_nowstr = prev_now.strftime('%Y-%m-%d %H:%M:%S %Z') change_price = last - prev_last if change_price < Decimal('0'): change_pricestr = symbol + str( change_price.copy_negate().quantize(quant)) else: change_pricestr = symbol + str(change_price.quantize(quant)) change_pct = (Decimal('100') * change_price / prev_last).quantize( Decimal('0.00')) if change_pct < Decimal('0'): change_pctstr = str(change_pct.copy_negate()) + '%' else: change_pctstr = str(change_pct) + '%' highest = last lowest = last fig = plt.figure(figsize=(10, 7), facecolor='k') ax = fig.add_subplot(1, 1, 1) rect = ax.patch rect.set_facecolor('k') img_title = self.symbol + '-' + self.against + ' at ' + nowstr plt.title(img_title) ax.xaxis_date() plt.xticks(rotation=25) ax.xaxis.set_major_formatter(DateFormatter('%Y-%m-%d %H:%M')) # first graph 30-minute candlesticks log.info("Plotting 30-minute candlesticks...") data = self.api.chartData(pair=self.against + '_' + self.symbol, start=int(prev_now.strftime("%s")) + 1, period=1800) if len(data) < 0: raise ValueError("No data returned") elif len(data) == 1: try: error = data['error'] log.error("Received error from API", error=error) raise ValueError( "Received error from API: {}".format(error)) except KeyError: if int(data[0]['date']) == 0: raise ValueError( "Too soon! You must wait at least 30 minutes between summaries for candlesticks." ) for row in data: high = Decimal(row['high']) if high > highest: highest = high low = Decimal(row['low']) if low < lowest: lowest = low time = datetime.fromtimestamp(int(row['date'])) popen = Decimal(row['open']) close = Decimal(row['close']) if close >= popen: color = 'g' else: color = 'r' vline = Line2D(xdata=(time, time), ydata=(low, high), linewidth=1.5, color=color, antialiased=False) oline = Line2D(xdata=(time, time), ydata=(popen, popen), linewidth=1, color=color, antialiased=False, marker=TICKLEFT, markersize=7) cline = Line2D(xdata=(time, time), ydata=(close, close), linewidth=1, color=color, antialiased=False, marker=TICKRIGHT, markersize=7) ax.add_line(vline) ax.add_line(oline) ax.add_line(cline) # then graph 5-minute lines log.info("Plotting 5-minute lines...") data = self.api.chartData(pair=self.against + '_' + self.symbol, start=int(prev_now.strftime("%s")) + 1, period=300) if len(data) < 0: raise ValueError("No data returned") elif len(data) == 1: try: error = data['error'] log.error("Received error from API", error=error) raise ValueError( "Received error from API: {}".format(error)) except KeyError: if int(data[0]['date']) == 0: raise ValueError( "Too soon! You must wait at least 5 minutes between summaries." ) begin = None for row in data: high = Decimal(row['high']) if high > highest: highest = high low = Decimal(row['low']) if low < lowest: lowest = low time = int(row['date']) popen = Decimal(row['open']) close = Decimal(row['close']) if begin is None: begin = popen line = Line2D(xdata=(datetime.fromtimestamp(time), datetime.fromtimestamp(time + 300)), ydata=(begin, close), linewidth=0.7, color='#FFFF00', antialiased=True) ax.add_line(line) begin = close higheststr = symbol + str(highest.quantize(quant)) loweststr = symbol + str(lowest.quantize(quant)) athfile = path.join( dir, 'market.' + self.symbol + '-' + self.against + '.ath') if path.exists(athfile): with open(athfile, 'r') as infile: ath = Decimal(infile.readline().strip()) if highest > ath: ath = highest newath = True if not testing: with open(athfile, 'w') as out: out.write(str(ath)) ax.xaxis.grid(True, color='#555555', linestyle='dotted') ax.yaxis.grid(True, color='#555555', linestyle='solid') plt.tight_layout() ax.autoscale_view() # save image to file or memory buffer if self.testing: imgfile = '/tmp/' + self.symbol + '-' + self.against + '.png' fig.savefig(imgfile) log.info("Market graph PNG saved", file=imgfile) else: img = io.BytesIO() fig.savefig(img, format='png') img.seek(0) plt.close(fig) # now upload result to imgur if not self.testing: log.info("Uploading plot to imgur...") img_b64 = base64.standard_b64encode(img.read()) client = 'bbe2ecf93d88915' headers = {'Authorization': 'Client-ID ' + client} imgur_data = {'image': img_b64, 'title': img_title} req = Request(url='https://api.imgur.com/3/upload.json', data=urlencode(imgur_data).encode('ASCII'), headers=headers) resp = urlopen(req).read() resp = json.loads(resp) log.debug("Got response from imgur", resp=resp) if resp['success'] == True: img_url = resp['data']['link'] log.info("Image uploaded successfully", url=img_url) else: log.error("Non-successful response from imgur", resp=resp) raise ValueError("Non-successful response from imgur") else: prev = False body = "Market Summary for " + self.symbol body += "\n==" body += "\n* All prices in *" + self.against + "*" body += "\n---" body += "\n" if prev: if change_pct > Decimal('0'): body += "\nUp " + change_pctstr title += ": Up " + change_pctstr elif change_pct < Decimal('0'): body += "\nDown " + change_pctstr title += ": Down " + change_pctstr else: body += "\nFlat" title += ": Flat" if newath: body += " (New All Time High Achieved)" title += " -- New All Time High!" body += "\n-" body += "\n" + self.symbol + " **" if change_price > Decimal('0'): body += "gained " + change_pricestr elif change_price < Decimal('0'): body += "lost " + change_pricestr else: body += "had no change" body += "** since the [last market summary]" body += "(https://steemit.com/@" + account.id + "/" + prev_permlink + ")" if change_pct > Decimal('0'): body += ", a change of **" + change_pctstr + "**" elif change_pct < Decimal('0'): body += ", a change of **-" + change_pctstr + "**" body += "." else: body += "\n*This is the first market summary, so no previous comparison data is available.*" body += "\n" body += "\n* Last trade: *" + laststr + "*" if prev: body += "\n* Highest trade: *" + higheststr + "*" if newath: body += " (new all time high)" body += "\n* Lowest trade: *" + loweststr + "*" if img_url is not None: body += "\n" body += "\n[![market activity plot](" + img_url + ")](" + img_url + ")" body += "\n" body += "\n---" body += "\n" body += "\n* Snapshot taken at *" + nowstr + "*" if prev: body += "\n* Previous snapshot: *[" + prev_nowstr + "]" body += "(https://steemit.com/@" + account.id + "/" + prev_permlink + ")*" body += "\n* Quote data from [Poloniex](http://poloniex.com)" body += "\n" body += "\n<center>Happy trading... stay tuned for the next summary!</center>" body += "\n" body += "\n---" body += "\n<center>*This market summary produced automatically by:" body += "\n[![Alpha Griffin logo](http://alphagriffin.com/usr/include/ag/favicon/favicon-128.png)" body += "\nAlpha Griffin Boiler bot](https://github.com/AlphaGriffin/boiler)" body += "\nv" + __version__ + "*</center>" if self.testing: print(body) permlink = self.make_permlink(now) tries = 0 post = None while tries < self.max_tries: try: log.info("Posting summary...", permlink=permlink, title=title, last=laststr, tags=tags) if self.testing: log.warn("Not actually going to post (testing mode)") break post = self.commit.post(permlink=permlink, title=title, author=account.id, body=body, tags=tags, self_vote=True) break except RPCError as e: log.warn( "Got RPC error while posting, trying again in 1 minute...", exception=e) tries += 1 sleep(60) if post is not None: log.info("Summary posted successfully", post=post) with open(nowfile, 'w') as out: out.write(now.strftime("%s")) with open(lastfile, 'w') as out: out.write(str(last)) return True else: if not self.testing: log.error("Failed to post summary") return False