Creating a follow a follower script using Python #1 (followers)

in #steem7 years ago

titlePart1.jpg

Hi Steemians,
I have been experimenting with programming in Python and wanted to try 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.
Sounds simple doesn’t? Well for me it’s taken me hours to get it working(ish) so I thought I would write this to try and help others

Please bear in mind that I am still a newbie to Python (and Steemit) and there will be many other (and much more elegant) ways of coding this. Oh and to make things even more fun I thought I would do this using a Raspberry Pi 2 as we had one sitting around not doing much!

I have split this into 3.5 parts/posts as it got a bit long for an all in one:

  • part 1 getting a list of my followers – this post,
  • 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

First job was to update my version of python3.2 to one that works with steem [I used python3.5] and then sudo pip install –U steem

This took me a while to install and after messing about for a good [read many due to my incompetence] few hours finally got steem installed – hooray!

Next I started playing about with a couple of functions in the shell

pythonShell.jpg

Once I got bored with this, next was to work out the exact flow of what I wanted the script to do.

  1. Create a list of my follower account names
  2. Create a list of account names of all the people I’m following
  3. Check to see if the follower is on the following list if not:
    - Print the account name to the shell
    - Try to automatically follow the account

With that done it was time to play around getting and printing to the shell a list of my followers in the first instance. As the get_following function has a maximum return I had to split it up into chunks and append to a final list.
I have commented my code as much as possible.

# Import statements
from steem import Steem

# Set up variables
steem = Steem()         # steem
account = 'garethdear'  # my account name

# Returns the number of followers for the specified account
def get_my_followers(): 
    followerCount = steem.get_follow_count(account)['follower_count'] #get the number of followers
    return followerCount    #return the number

followerCount = get_my_followers()                      #call the get_my_followers function
print("My followers count is: " + str(followerCount))   #print out the number

print ("Getting my followers")  #helper message
followers = []                  #list to hold the followers
followersStart = 0              #start at zero for the count
while followerCount > 0:        #while the follower count is greater than zero
    tempFollowers = steem.get_followers(account, followersStart, 'blog', 100)   #get the next 100 followers
    if (followerCount > 100):   #if the followerCount is still greater than 100
        followersStart = tempFollowers[99]['follower'] #set the count to the last in that section
        del tempFollowers[99]   #remove the last follower in that section as becomes the first in the next
    followers.extend(tempFollowers) #add tempFollowers to the end of the followers list
    followerCount -=100         #decrease the followerCount by 100

myFollowerNameList = []                 #new list to hold just the account names
for aFollower in followers:             #loop each follower
    name1 = aFollower['follower']       #extract the follower name
    myFollowerNameList.append(name1)    #add to the the list

# Print out the names
for name in myFollowerNameList:     #loop each name in the list
    print(name) 

Now to run the code...
pythonGetFollower.jpg

Double hooray! its working!– Part 1 complete now on to part 2
I’ll post this in a day or so.

Bye for now
Gareth

Sort:  

sweeeeeeeeeeeeeeeeet :)) thanks

Python Looks Very Easy Than F**king C++, LOL

Great stuff and thanks for the follow..followed you back :)

Great post! Thank you.

Coin Marketplace

STEEM 0.16
TRX 0.15
JST 0.029
BTC 57971.70
ETH 2448.51
USDT 1.00
SBD 2.34