24 Hour Summary CryptoBot(Python) Update#2 - Code IncludedsteemCreated with Sketch.

in #steemdev7 years ago (edited)

24 Hour Summary CryptoBot

Please note that I originally posted this in #bitcoin and now I'm posting this is #steemdev after someone recommended it. I hope it gets more attention in this category.

Recently I created a bot which allowed you to comment on my post with the name of a listed cryptocurrency, the bot would then retrieve a 24 hour summary on the commented cryptocurrency in real time from Bittrex using the Bittrex API. All markets are Bitcoin markets. The bot had a few issues which prevented the bot from retrieving the summaries.

Issues Fixed

  1. Added in case insensitive dictionary search for the currencies because the previous bot could only retrieve currencies that were typed in the correct case. The bot could not retrieve, for example, a summary of Ethereum if Ethereum was spelled "ethereum"(lower case 'E').

  2. Added in the Bitcoin currency symbol.

Coin List

coins.png

Any of the coins in the above list can be commented on this post in any case eg . "steem"

Example Comment and Reply

version2screenshot.png

The Code

Before you go over the following code thinking "What the actual F#ck!?" just note the following:

  1. I learned Python from scratch in order to play around with the Steemit and Bittrex API's

  2. I'm aware that there are numerous redundant code statements, since I'm new to Python I tried to make it as less condensed as possible.

  3. This code can not just be copied, pasted and ran since you need to set up your environment first

  4. The code does not have proper exit code

Without further adieu, here we go:

from steem import Steem
import json
from urllib.request import urlopen 
from forex_python.bitcoin import BtcConverter
import os
from dateutil import parser
import time

#create steem instance
s = Steem()
#create BtcConverter instance
b = BtcConverter()
#get the Bitcoin currency symbol
symbol = b.get_symbol()+" "

#unlock wallet
os.environ["UNLOCK"]="your-super-dooper-secret-wallet-passphrase"

runMe = True
 
#I did not actually type out all the currencies and their corresponding markets
cryptos = {'evergreencoin': 'BTC-EGC', 'lunyr': 'BTC-LUN', 'pinkcoin': 'BTC-PINK'....}

def commentResult(crypto, author, permlink):
    #url to query the Bittrex API for market info
    url = "https://bittrex.com/api/v1.1/public/getmarketsummary?market="+cryptos[crypto]
    print("Getting data from Bittrex")
    #open url
    response = urlopen(url).read().decode('utf-8')
    #disect dictionary to get market data
    formatted = json.loads(response)['result'][0]
    marketName = str(formatted['MarketName'])
    high = symbol+str(float('%0.8f' % formatted['High'])) 
    low = symbol+str(float('%0.8f' % formatted['Low']))
    volume = str(formatted['Volume'])
    last = symbol+str(float('%0.8f' % formatted['Last']))
    baseVolume = str(formatted['BaseVolume'])
    timeStamp =str(formatted['TimeStamp']) 
    bid = symbol+str(float('%0.8f' % formatted['Bid'])) 
    ask = symbol+str(float('%0.8f' % formatted['Ask']))
    openBuyOrders = str(formatted['OpenBuyOrders'])
    openSellOrders = str(formatted['OpenSellOrders']) 
    
    print("Setting up strings")
    
    devider = "___________________________________ \n"
    string1 = "Market Name: "+marketName+"\n"
    string2 = "High: "+high+"\n"
    string3 = "Low: "+low+"\n"
    string4 = "Volume: "+volume+"\n"
    string5 = "Last Price: "+last+"\n"
    string6 = "Base Volume: "+baseVolume+"\n"
    string7 = "TimeStamp: "+timeStamp+"\n"
    string8 = "Bid: "+bid+"\n"
    string9 = "Ask: "+ask+"\n"
    string10 = "Open Buy Orders: "+openBuyOrders+"\n"
    string11 = "Open Sell Orders: "+openSellOrders+"\n"
    
    print("commenting on post: Correct currency")
    message = string1+devider+string2+string3+string5+devider+string4+string6+devider+string8+string9+devider+string10+string11+devider+string7
    s.post("", message, "benniebanana", "", "@"+author+"/"+permlink)
    print("Commented on new post: Correct")
    print("Sleeping for 20 seconds after commenting")
    time.sleep(20)
    print("Done sleeping")
    

while runMe == True:
    #get replies on post
    data = s.get_content_replies("benniebanana", "<permlink of post >")
    #check for new comment id's
    for c in data:
                
        idLog = open("/home/myComputerName/Desktop/log/log.txt", "r+") #Should probably change this
        latestId = int(idLog.readlines()[-1])
        if c['id'] > latestId:
            print("New post found")
            crypto = c['body'].lower()
            author = c['author']
            permlink = c['permlink']
            #check if crypto is in dictionary
            if crypto in cryptos:
                commentResult(crypto, author, permlink)
                idLog.write(str(c['id'])+"\n")  
                print("log updated")     
                
            else:
                print("Currency not found")
                idLog.write(str(c['id'])+"\n") 
                print("log updated ")
                
            
        else: 
            print("no new comments")
     
    
#close log file
idLog.close()

Please Note

  • This was in no way created to help anyone make trading decisions, this was purely a little project I tackled to keep myself busy and should not be used to make any trading decisions off.
  • If you don't get an instant reply it's because I have to wait 20 seconds after replying before I may reply again so botty needs to sleep a little in between replies to prevent him from crashing and causing my laptop to explode.
  • I can not guarantee 24/7 uptime of the bot because I have not tested my code extensively, as I mentioned before, this is merely an experiment and not some enterprise level software but I will be keeping an eye on him to make sure he stays alive for those who want to test him out a little. Please don't downvote or flag this post if the bot doesn't work, I'll try my best to keep the bot running.
  • Feel free to comment, besides a cryptocurrency name, for me to reply back on(Not the bot).
  • You will not get a summary if you misspell a coin name(case does not matter as long as the spelling is correct)

Enjoy!

Be sure to follow me for more of these little projects

Sort:  

Great work. Helps a lot for someone new to steem and python to learn. I am sure it will benefit a lot of others as well.

Thank you for the reply and the support, I'm also still learning Python and it's a super fun language to be learning. I'm glad I could post something of value. I'll be doing more of these posts in the future after seeing the response to this post.

This is great. I learned a lot from the code you provided, thanks.

Thank you for the feedback, appreciated. I'll definitely be doing more of these projects and sharing them with the community.

Hi, sorry that you didn't get a reply but it's "dogecoin". Feel free to try again. The bot is still running on this post

Market Name: BTC-STEEM


High: ฿ 0.00084347
Low: ฿ 0.00080598
Last Price: ฿ 0.00082089


Volume: 254593.61232136
Base Volume: 208.10888537


Bid: ฿ 0.00082098
Ask: ฿ 0.00082499


Open Buy Orders: 848
Open Sell Orders: 2243


TimeStamp: 2017-06-17T20:26:57.47

Market Name: BTC-GNT


High: ฿ 0.0002295
Low: ฿ 0.00020145
Last Price: ฿ 0.00022852


Volume: 3069040.02422301
Base Volume: 678.112274


Bid: ฿ 0.00022852
Ask: ฿ 0.00022854


Open Buy Orders: 907
Open Sell Orders: 1488


TimeStamp: 2017-06-17T21:42:33.867

Congratulations @benniebanana! You have completed some achievement on Steemit and have been rewarded with new badge(s) :

Award for the number of upvotes

Click on any badge to view your own Board of Honnor on SteemitBoard.
For more information about SteemitBoard, click here

If you no longer want to receive notifications, reply to this comment with the word STOP

By upvoting this notification, you can help all Steemit users. Learn how here!

Nice work, totally over my head but impressive nonetheless!

Thank you for the support, appreciated

Very cool indeed. Thanks for sharing this and this stuff goes way over my head so you know a lot more than you give yourself credit for. Well done!

Thank you very much. I appreciate the feedback

Hi sorry this bot is inactive

Ok no worries I assume you just got bored of having it running ?

I had it running on a Raspberry pi 3 but I eventually formatted it and used it for something else

Coin Marketplace

STEEM 0.20
TRX 0.13
JST 0.030
BTC 65133.17
ETH 3480.37
USDT 1.00
SBD 2.52