Making a script to get updates when your voting power is 100% Part 04

Hello everyone I hope you are all doing well!

Sorry about not posting for a while, I got quite busy with the school as you may know yourself.

In the last post we were able to grab a users information from https://hivestats.io/@username

By using my code you should of seen the following info displayed:

python output 
---------------
User: armoredbanana 
Vote Power: 98.00%
Time untill full: 2 hours 24 minutes
Effective HP: 21.56 HP
Actual HP: 11.5 HP
---------------

As you can see we were able to get the Time Until Full in a string format. This is not very useful if we want to check back in that given time.

In today's post you should see how we convert the Full Hive Power from a string into a integer that will tell you exactly how many minutes you need to wait. This will be useful in the next tutorial where you will learn how to send an email to yourself when your hive power is full!

Lets get into it!

(before I dive right into it I want to mention I cleaned up my code a bit and made it object oriented. You can find the code at this github repo:

Converting String to Integer
This can be a daunting task, but with a few different if statements it wont be long until we have our integer. Here is the function I created.

python code for converting string to integer
# gets the number of minutes left for the user to have full hive power again
    def get_time(self):
        string = str(self.time_to_100.text)
        temp = string.replace(" ", "")
        self.total_minute_value = 0
        
        if (temp.find("Full") == 0):
            self.total_minute_value = 0

        # Getting the number of minutes remaining based on the day value
        if (temp.find("day") != -1):
            day_index = temp.find("day")
            day_value = int(temp[day_index-1])
            minute_day_value = day_value * 1440
            self.total_minute_value += minute_day_value

            # Getting the number of minutes remaining based on the hour value
        if (temp.find("hour") != -1):
            hour_index = temp.find("hour")
           
                
            if (temp[hour_index-2].isdigit()):
                hour_value = int(temp[hour_index-2] + temp[hour_index-1])
                minute_hour_value = hour_value * 60
                self.total_minute_value += minute_hour_value

            elif (temp[hour_index-1].isdigit()):
                hour_value = int(temp[hour_index-1])
                minute_hour_value = hour_value * 60
                self.total_minute_value += minute_hour_value

            # Getting the number of minutes remaining based on the minute value
        if (temp.find("minute") != -1):
            minute_index = temp.find("minute")
            if (temp[minute_index-2].isdigit()):
                minute_value = int(temp[minute_index-2] + temp[minute_index-1])
                self.total_minute_value += minute_value

            elif (temp[minute_index-1].isdigit()):
                minute_value = int(temp[minute_index-1])
                self.total_minute_value += minute_value

        return self.total_minute_value

Now this looks like a lot so let me break it down for you.

  1. First we need to load our string (time_to_100) into a string.

  2. Now this string contains spaces, which we can replace with a great function in python. "string_variable.replace(" ", ""). Using this function we replace every space with a no space.

  3. Next we check if its full. In python you can find a list of strings (aka words) by using the string_variable.find() function. If our hive power is full, on hive stats it displays it as "Full" so we search for that word. IF that word is present the .find("Full") function returns the index of the first letter in our string which will be 0.
    Thats why we check if temp.find("Full") == 0. If so know we are full and we make the total_minute_value = 0

  4. IF its not full we need to keep on checking. IF the word we are searching for does not exist in our string, then the .find() function will return a -1 value. So IF temp.find("day") != -1 (temp.find("day") IS NOT EQUAL to -1) then we know the word day is in our string. We are able to easily convert the string into a integer day_value = int(temp[day_index-1]) which we then must convert to minutes. 1 day has 1440 minutes which is an easy multiplication.

  5. We must keep doing this for hours and minutes as well. The tricky part with hours and minutes is that it can be more than one integer. We can get around this by checking if there are two numbers by using the .isdigit() function. IF the index of our string is a digit, the .isdigit() function will return TRUE. IF its not a digit, it will return FALSE. Through this we can establish how many digits we are dealing with and accommodate our if statements as seen in the code below:

python code for converting hours into minutes
# Getting the number of minutes remaining based on the hour value
        if (temp.find("hour") != -1):
            hour_index = temp.find("hour")
           
                
            if (temp[hour_index-2].isdigit()):
                hour_value = int(temp[hour_index-2] + temp[hour_index-1])
                minute_hour_value = hour_value * 60
                self.total_minute_value += minute_hour_value

            elif (temp[hour_index-1].isdigit()):
                hour_value = int(temp[hour_index-1])
                minute_hour_value = hour_value * 60
                self.total_minute_value += minute_hour_value

At the end of all of this, we load all of our minutes into a total_minute_value variable to easily know how many minutes we have to wait until our hive power is full.

Now when you call the function wait_time = object.get_time() it will return an integer value that represents the amount of minutes we need to wait until our hive power is full again. As I mentioned earlier this can be very helpful when we will send ourselves an email to go like again!

python code 

import updater_mod
import multiprocessing
import time
import schedule
import smtplib, ssl

#100%_hivepower_0%


# object armoredbanana

user1 = updater_mod.Hive_power_tracker(username = "armoredbanana")
user1.get_user_data()
check_backc_time_user_1 = user1.get_time()
print(user1)

# object trostparadox
user2 = updater_mod.Hive_power_tracker(username = "trostparadox")
user2.get_user_data()
user2.get_time()
print(user2)

# object bhanutejap
user3 = updater_mod.Hive_power_tracker(username = "bhanutejap")
user3.get_user_data()
user3.get_time()
print(user3)

updater_mod is the code I posted above at the very beginning. This is our class that we create our objects from. If you are not familiar with object oriented programming, I strongly recommend that you learn about that first before anything else as it makes replicating users in this case really easy!

In the code I created 3 different objects called user1, user2, user3 to show the simplicity of getting multiple users information using object oriented programming or OOP for short.

python output of the code above
--------------- 
User: armoredbanana 
Vote Power: 98.36% 
Time untill full: 1 hour 58 minutes 
Effective HP: 21.56 HP 
Actual HP: 11.5 HP 
Full Hive Power in:  118  minutes
---------------
--------------- 
User: trostparadox 
Vote Power: 94.59% 
Time untill full: 6 hours 29 minutes 
Effective HP: 104,775.67 HP 
Actual HP: 5,496.05 HP 
Full Hive Power in:  389  minutes
---------------
--------------- 
User: bhanutejap 
Vote Power: 60.34% 
Time untill full: 1 day 23 hours 35 minutes 
Effective HP: 0 HP 
Actual HP: 0 HP 
Full Hive Power in:  2855  minutes
---------------

Here you can see @armoredbana, @trostparadox,and @bhanutejap
info. It displays it to us now nicely in minutes which again is very useful for our next tutorial.

Troubles
I had some issues with getting the gecodriver in my path, but it was easily solved by the following line of code in the updater_mod.py

python code in updater_mod.py
self.driver = webdriver.Firefox(executable_path = r'C:\Users\thoma\Documents\Blockchain\webscraping\geckodriver.exe', options=options) #this is the path where my geckodriver exists

I also had quite a few errors when converting hours and days because sometimes there are 12 hours remaining but other times there are only 8 hours remaining. That one singe digit makes a big difference.



0
0
0.000
3 comments
avatar

I enjoy learning from you! Keep posting and sharing. The Blockchain is such a great tool.

0
0
0.000
avatar

Thanks a lot! And yes its a great place! Check out my next post coming later today!

0
0
0.000