Python Libraries: Self-Vote Checking, The FAST Version!

View this thread on: d.buzz | hive.blog | peakd.com | ecency.com
·@slobberchops·
0.000 HBD
Python Libraries: Self-Vote Checking, The FAST Version!
![Cover.JPG](https://files.peakd.com/file/peakd-hive/slobberchops/23t78uMBWZF39zdb9yzMK9DxgJx2bwM3G2Tbxm6C41ZPrUAYXQYT2hBkgezWpvvkkvcWU.JPG)

![RedLine.png](https://files.peakd.com/file/peakd-hive/slobberchops/BqIuXs6C-RedLine.png)

**...'THE SLOBBERCHOPS WHITELIST'...**

Would you like to be whitelisted and eligible for votes by me (and a bunch of other accounts?). Drop me a comment on this post, and I *may* add you (subject to account checks). 

Twice or three times a day I manually run the BOT until the voting thresholds on all the accounts have been breached. You may get a vote, or you may be passed by. 

A vote depends on passing the checks I have put in place, and if your name is randomly picked. There's nothing to lose, you get votes, could be small or large and I get curation rewards. 

![RedLine.png](https://files.peakd.com/file/peakd-hive/slobberchops/BqIuXs6C-RedLine.png)

**DISCLAIMER:** Self-voting is a personal preference. It is completely up to you what you vote for and who. Never let anyone tell you what to do with your stake, it's yours and yours alone.

After looking at some [code](https://peakd.com/hive-163521/@gwajnberg/using-beem-library-python-to) from @gwajnberg, I figured this was it! A self-voting checking code routine, something I had been putting off for a while due to combination of sullied procrastination laced with pure apathy.

After some farting about, I managed to get @gwajnberg's routine working and work it did albeit very slowly. My last patch had increased the speed of the HIVE Voting BOT, by ensuring accounts were only processed once, so I was loathe to add anything to slow it down.

<center>
![image.png](https://files.peakd.com/file/peakd-hive/slobberchops/23tcP5UHCsU3mfXsjEjSV7caQCjxrYQ2CEkEooX3BvaF9RuTfPZFL2whS1w3Q7wUZm6VR.png)
...***'Chat-GPT is far too polite, I would have told myself to fuck off'...***
</center>

It was time to make something quick and efficient and that meant HIVE-SQL. Although things are starting to click, SQL is hardly my forte, so I asked Chat-GPT for some help.

A fat lot of fucking good that did, and after a while I was calling that AI a useless bastard, a statement it tends to shrug off with ease.  Wrong result, even though I explained it properly. 

Garbage-in, Garbage out @steddyman tells me. It's true sometimes.

Taking things back to basic, I gradually built up the code and requested a single output, otherwise when I call it from Python I get all this other bullshit that's not needed. 

	SELECT 
    ROUND(
        FLOOR((CAST(SUM(CASE WHEN voter = author THEN weight ELSE 0 END) AS FLOAT) 
        / CAST(SUM(weight) AS FLOAT)) * 1000 * 100) / 1000, 2
    ) AS self_vote_percentage
	FROM txvotes
	WHERE 
    	voter = 'greedy' 
    	AND weight > 0 
    	AND timestamp > GETDATE() - 7;

This returns what I want and nothing else. I am not interested in the parameters used for the calculations, just the result code.

![image.png](https://files.peakd.com/file/peakd-hive/slobberchops/23tcP5UHFJVbcF9fPit4FjtP5RS27tvYfusWM4L5GaDmwvZCwKtjSTjPBiAHj8iPj5stH.png)

Now it was a question of getting it into Python. For this, you will need a valid HIVE-SQL account, and this can be obtained [here]( https://hivesql.io/registration/). 

It will cost you 1 HBD for an account and don't lose your credentials or it will cost you another to get them reset. I speak from personal experience.

To date, I had only a single SQL-HIVE routine in the HIVE Voting BOT, and now there were two calls. The SQL routine had to be callable using parameters so I had to make several adjustments.

I hate duplicating code, it's shitty practice and something I have fallen foul of in past scripts, so I make a point of avoiding it now.

	def get_hiveSQL_data(SQLCommand, conn): 
	# Accepts SQL queries and returns values
    
    cursor = conn.cursor()
    cursor.execute(SQLCommand)
    result = cursor.fetchone()
    cursor.close
    conn.close

    if len(result) == 1:
        return (result[0]) if result else (None)
    elif len(result) == 2:        
        return (result[0], result[1]) if result else (None, None)
    
What boring-looking code, and where's the fucking swearing?

It is boring but is now callable, and depending on the parameters will return up to two values. Unlike many other languages Python allows separate values to be passed back to the function call, it's one aspect I really like about it.

		# Test # 1 - Are you a Self-Voting Motherfucker? - How greedy can you be?
        # Gets the Self-Voting Percentage
        SQLCommand = f'''
        SELECT 
        ROUND(
                FLOOR((CAST(SUM(CASE WHEN voter = author THEN weight ELSE 0 END) AS FLOAT) 
                / CAST(SUM(weight) AS FLOAT)) * 1000 * 100) / 1000, 2
        ) AS self_vote_percentage
        FROM txvotes
        WHERE 
            voter = '{account}' 
            AND weight > 0 
            AND timestamp > GETDATE() - 7;
            '''            

        self_vote_percentage = get_hiveSQL_data(SQLCommand, connection_string)

When the call is like this, it makes a little more sense, much more...

	connection_string = pypyodbc.connect(
	    f'Driver={{ODBC Driver 18 for SQL Server}};'
	    f'Server=vip.hivesql.io;'
	    f'Database=DBHive;'
	    f'TrustServerCertificate=yes;'
	    f'uid=Hive-{mainaccount};'
	    f'pwd={sql_password}'
	)

conection_string only needs to be defined once, and so is close to the top of the script within the declarations area.

	import pypyodbc

	def get_self_vote_percentage(account): 

    conn = pypyodbc.connect(
        'Driver={ODBC Driver 18 for SQL Server};'
        'Server=vip.hivesql.io;'
        'Database=DBHive;'
        'TrustServerCertificate=yes;'
        'uid=Hive-username;'
        'pwd=password'
    )
    
    cursor = conn.cursor()

    SQLCommand = f'''
    SELECT 
    ROUND(
        FLOOR((CAST(SUM(CASE WHEN voter = author THEN weight ELSE 0 END) AS FLOAT) 
        / CAST(SUM(weight) AS FLOAT)) * 1000 * 100) / 1000, 2
    ) AS self_vote_percentage
    FROM txvotes
    WHERE 
        voter = '{account}' 
        AND weight > 0 
        AND timestamp > GETDATE() - 7;
    '''

    cursor = conn.cursor()
    cursor.execute(SQLCommand)
    result = cursor.fetchone()
    cursor.close
    conn.close

    return result[0]

	account = "greedy"
	self_vote_percentage = get_self_vote_percentage(account)
	print(f' Account {account} has a self-vote percentage of {self_vote_percentage}%')

To make it easier for you to use this yourself (above), I isolated the whole routine so it can be modified to your needs.

Replace 'greedy' with an account of your choice, and change the name and password to your credentials.

![HowGreedyCanUBe0.JPG](https://files.peakd.com/file/peakd-hive/slobberchops/Eo42wHDche2APunuRsLbBaTeAG6hsf28bnQZATuUFR3GAfbbokimpCFgw38wJh5nNWf.JPG)

Adding the routine to the HIVE Voting BOT was not a big deal. I have completely rewritten it and made it a lot more modular than the abomination that @felixxx once cast his eyes on about a year ago. 

It was time for some fun, and I was pleasantly surprised at the amount of accounts that didn't self-vote at all.

![cutoffs.JPG](https://files.peakd.com/file/peakd-hive/slobberchops/Eo6CGRLPkou3ZytNyzqCAB6iFCVZdkwqcpTXEYsErkc8uTnfgLP1kEjuiLtZVvDmVVk.JPG)

First, declare some cut-off values, there's little left to the imagination here.

.. and then running it in anger which is always fun. It picked up just two authors who like to vote themselves a little more than they should and the BOT does not hold back.

![greedy.JPG](https://files.peakd.com/file/peakd-hive/slobberchops/Eo1uZJTRxBWUAfRRVQCfBatwfkUCmjkWECzFm1G9Ke3xrjVc2y7RXGDmBVeFD7tdHZf.JPG)

Nothing personal to this user..., quit the self-voting and it will forget about you in a week, and you might just get votes again. It has a short-lived memory and is completely non judgemental... honest!

Another routine added; I think I might just give all the 0% self-voters authors a bonus of 1. 

Lastly... this function is no slouch, it's rapid!

![RedLine.png](https://files.peakd.com/file/peakd-hive/slobberchops/BqIuXs6C-RedLine.png)

Do you like posting your Urbex content and photography for **FREE** on Facebook and YouTube? I like to get some form of reward for my work and every time I create I do just that. Take a look at **[The Urbex Community on HIVE](https://peakd.com/c/hive-104387/created)**.

If you want to keep creating for **FREE** then ignore what you are reading. If you want to be like me and gain something other than **BUGGER ALL** for your work then click **[here](https://youtu.be/0pnFg4igYuY)** and learn about posting on the HIVE blockchain.

<center>
My Urban Exploration Tales can be found directly on the internet via my
Website: '[Tales of the Urban Explorer](https://talesoftheurbanexplorer.co.uk/)'.
</center>

![RedLine.png](https://files.peakd.com/file/peakd-hive/slobberchops/BqIuXs6C-RedLine.png)

![TalesLogo.JPG](https://files.peakd.com/file/peakd-hive/slobberchops/23tbkz42cJ8HwULaL9SPMgb78qc5GL7ZBfC4nrsp2sk8ytmsQa4NJAhupa8zpu19gUgZ1.JPG)

![RedLine.png](https://files.peakd.com/file/peakd-hive/slobberchops/BqIuXs6C-RedLine.png)

<center>
![CurieCurator.jpg](https://files.peakd.com/file/peakd-hive/slobberchops/f5zec6UG-CurieCurator.jpg)
</center>

![RedLine.png](https://files.peakd.com/file/peakd-hive/slobberchops/BqIuXs6C-RedLine.png)

<div class="pull-left">

![Drooling Maniac.JPG](https://steemitimages.com/DQmNfnfWNzzjZDZ7b1PukhSHLgL5g55apzyDpT4Jp7dP2CH/Drooling%20Maniac.JPG)

</div>

<div class="pull-right">

If you found this article so invigorating that you are now a positively googly-eyed, drooling lunatic with dripping saliva or even if you liked it just a bit, then please upvote, comment, rehive, engage me or all of these things.

</div>
👍 , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,