Creating a follower/following bot using Python part #3 (full code)

in #steemit7 years ago

Follow a follower using Python #3.jpg
Hi Steemians,
Final part and follows on from parts 1,2 and, 2.5 (links below) where I’m attempting to create a simple script that would list my followers and if I wasn’t following then print the names to the shell and then automatically follow them.

I have split this into 4 parts as it got a bit long for an all in one
part 1 getting a list of my followers
part 2 getting a list of who I’m following
part 2.5 not really a part just a bit of code tidying up
part 3 comparing lists and following – this post

To recap the flow I’m working to is:

  • list of my follower account names
  • Create a list of account names of all the people I’m following
  • Check to see if the follower is on the following list if not:
    • Print the account name to the shell
    • Automatically follow the account

So far I have managed to:

  • get steem installed – this took far longer than I expected (due to my incompetence)
  • Have successfully got to print to the shell a list of my followers
  • Have successfully got to print to the shell a list of who I’m following
  • Tidied up the code

I have a list of who has followed me (myFollowers) and a list of who I am following (myFollowing). What need to do next is to check if those who have followed me are not on my following list. If they are not then print these account names to the shell.

print ("***COMPAREING LISTS")
for name5 in myFollowerNameList:            #for everyone who is follows me
    if name5 not in myFollowingNameList:    #check if I'm following them
        print("Need to follow: " + name5)   #if not the print the name

Wow! that bit was easy. Now I just need to add a few things so I can automatically follow the accounts back.

I needed to add in my posting key and change the steem object

# Variables
my_steem_posting_key = 'MY_STEEM_POSTING_KEY'
steem = Steem(keys = my_steem_posting_key)

Then the code to follow back

print ("***COMPAREING LISTS")
for name5 in myFollowerNameList:            #for everyone who is follows me
    if name5 not in myFollowingNameList:    #check if I'm following them
        print("Need to follow: " + name5)   #if not the print the name
        try:
            steem.follow(name5, what = ['blog'], account = account)
            print ('Followed ({}) succsessfully'.format(name5))
            time.sleep(10)
        except Exception as e:
            print('Unable to follow + str(name5))

followfollowpic.png

Hooray! it works - the full code is:

# Import Statements
import time
from steem import Steem

# Variables
my_steem_posting_key = '5YOUR_POSTING_KEY'
steem = Steem(keys = my_steem_posting_key)
account = 'garethdear'

# Function - get count of followers
def get_my_followers():
    followerCount = steem.get_follow_count(account)['follower_count']
    return followerCount

# Function - get count of following
def get_my_following():
    followingCount = steem.get_follow_count(account)['following_count']
    return followingCount

# Function - get a list of my followers
def get_my_followers_list(followerCount):
    followers = []
    followersStart = 0
    while followerCount > 0:
        tempFollowers = steem.get_followers(account, followersStart, 'blog', 100)
        if (followerCount > 100):
            followersStart = tempFollowers[99]['follower']
            del tempFollowers[99]
        followers.extend(tempFollowers)
        followerCount -=100

    myFollowerNameList = []
    for aFollower in followers:
        name1 = aFollower['follower']
        myFollowerNameList.append(name1)

    return myFollowerNameList

# Function - get a list of who I'm following
def get_my_following_List(followingCount):
    following = []
    followingStart = 0
    while followingCount > 0:
        tempFollowing = steem.get_following(account, followingStart, 'blog', 100)
        if (followingCount > 100):
            followingStart = tempFollowing[99]['following']
            del tempFollowing[99]
        following.extend(tempFollowing)
        followingCount -=100

    myFollowingNameList = []
    for aFollower in following:
        name2 = aFollower['following']
        myFollowingNameList.append(name2)

    return myFollowingNameList

#Get the counts of followers and following
print("Getting my followers...")
followerCount = get_my_followers()      #Get the count of followers
print("Getting my following...")
followingCount = get_my_following()     #Get the count of following

#Get the accounts in the lists
print("Getting my follower list...")
myFollowerNameList = get_my_followers_list(followerCount)    #Get the names of my followers
print("Getting my following list...")
myFollowingNameList = get_my_following_List(followingCount)   #Get the names of who is following me

print ("***COMPAREING LISTS")
for name5 in myFollowerNameList:            #for everyone who is follows me
    if name5 not in myFollowingNameList:    #check if I'm following them
        print("Need to follow: " + name5)   #if not the print the name
        try:
            steem.follow(name5, what = ['blog'], account = account)
            print ('Followed ({}) succsessfully'.format(name5))
            time.sleep(10)
        except Exception as e:
            print('Unable to follow {}'.format(name5))

Please remember I’m a newbie to python but now this is done I’m looking to play with the other Steemit functions using Python.

Have fun and bye for now
Gareth

Part1 – https://steemit.com/steem/@garethdear/creating-a-follow-a-follower-script-using-python-1-followers
Part 2 – https://steemit.com/steemit/@garethdear/creating-a-follow-a-follower-script-using-python-2-following
Part 2.5 - https://steemit.com/steemit/@garethdear/creating-a-follow-a-follower-script-using-python-2-5-code-tidy-up

Sort:  

Great stuff! I had to run the script three times it would timeout at get following the follower then list, eventually ran all the way thru, is it a timeout because of network? Is there a way to set it up to retry rather than fail?

Hi @enki74, Glad you got it to work in the end. The short answer is yes, you can put in a try statement and if it fails then you can retry when you left off. I'm not at my computer at the moment but will try to post the code tomorrow.

not sure whats happening now, any ideas with this?

Getting my followers...
Getting my following...
Getting my follower list...
Traceback (most recent call last):
  File "./followafollower.py", line 66, in <module>
    myFollowerNameList = get_my_followers_list(followerCount)    #Get the names of my followers
  File "./followafollower.py", line 27, in get_my_followers_list
    followersStart = tempFollowers[99]['follower']
TypeError: 'NoneType' object is not subscriptable

Great stuff! I had to run the script three times it would timeout at get following the follower then list, eventually ran all the way thru, is it a timeout because of network? Is there a way to set it up to retry rather than fail?

Nice job!
Where are the docs ?

Nice... Although coding was gibberish to me... Still i appreciate the effort... Cheers...

What a cool script! It will contain my username next time you run it ;)

Coin Marketplace

STEEM 0.20
TRX 0.14
JST 0.030
BTC 67364.26
ETH 3322.90
USDT 1.00
SBD 2.71