 Hey everyone, thanks for coming and welcome to Automated Instagram with Python and Selenium And I hope you're a little bit excited because we'll be talking about some great stuff Including why you should use automation? What Selenium is we'll Actually do some Instagram automation right here and talk about a page object design pattern and after that we'll quickly take a look at my tool Instapy and Last but not least we will go into why open source is important and how you can level up your own open source Pro a portfolio and projects Like has been said I am Tim and I'm currently a CS student in Germany and the creator of Instapy And if you have any questions feel free to contact me on any of the social medias or directly we are Gmail I'll be happy to chat and if you have any questions Later today or tomorrow and you can't remember my face I got this white dot on the back of my head and It's really kind of to remember me Also make sure to follow me on Twitter because I'll be posting a slide still later But now without wasting any further time, let's get right into it. So why should you do automation? I'm pretty sure every single one in this room has at least one task here could and actually should automate but they don't do it a instead just do it by hand and I Interestingly got some emails through Instapy from social marketing companies that told me that asked me to automate their Instagram growth process because they have like One or two people sitting there and log into clients accounts Liking pictures commenting stuff and following people and they do it all by hand eight hours a day And I think this is really ridiculous Especially since automating stuff is most of the time one-time effort You don't have to do it every time you might have to make some slight adjustments But the big part is done and it will save you time The more the more often you have to do it especially since a lot of tasks are Easier law Automatable, then you actually think they are I Just recently had to send an email to every single contact on a CSV file And I could have gone in there and do this by hand But instead I just wrote a script to go through CSV file and send out an email to everything so I had time for more important stuff and Speaking of Instagram or Twitter Facebook. You could actually do something like this but as Things gets more complex. This will really explode in complexity and Has some disadvantages. That's why we will use a software only solution and We will use selenium here, and I don't know how many of you have here to selenium Could you bring hand sign? Oh? Okay, that's quite a lot for those who don't know Selenium is actually a testing tool for front ends, especially web front ends but you can also do it with Java front ends and Yeah, it's actually for testing your web apps For example, you could test the flask web app with it But it's also very good for automating boring tasks, which of course liking stuff on Instagram is if you want to do it efficiently and Who has he already here of react VHS or Angular probably everyone and you can't crawl websites which are dynamically rendered like Facebook Twitter Instagram with with simple requests. That's why selenium is also very interesting And since we're using Python here Installing it is as easy as pip install selenium, and we're now ready to do some real Instagram automation So let's just take a look at what we have to do. We have to start a new browser We have to navigate to Instagram once we're on Instagram We can log us in and once we're logged in we can then start to click these little hearty icons to actually like pictures and Since we're using selenium here, this is really easy and We have to simply import the web driver element from from selenium and once we've done that we can instantiate on a new browser and In our case, we will just use the chrome browser here, but you can use Firefox or check out the documentation which browsers are also supported and In the praises here you can pass the path to the chrome driver. This is not necessary, but gives you some nice security on different platforms and once we've got our browser object ready, we can then use the get method to navigate to any page in our case, of course, this is Instagram and once we're on Instagram we can then Get to our next step and Well, the sleep is just here to make sure the browser doesn't close instantly and we can see something and the browser that closes here for Clean exit, which is recommended by selenium Let's take a look at what happens so if we enter If we start the script, we will see that it opens a new browser and navigates to Instagram and if you're now on Instagram we can we see that there is There is no login form. It's simply the sign-up form and we have to make sure we get to the login form here You see there and in this case there is this little login link on the bottom of the page with which we can switch from the sign-up form to the login form and This is really good point to introduce a new concept here from selenium and this is finding and clicking elements So so we in order to have find an element on a page We can take a look at a page source and we have to find a nice signature of the element And in this case, it's just an a tag with the class of underscore FC and so on and the text of login so we use that knowledge to get the element we are a browser element and For chrome you can simply use the death tools as well as safari. They all have very nice death tools where you can search elements pretty efficiently If we translate this into code now We can use our browser object to find element by XPath and for those who don't know XPath is just query language for XML documents as HTML in this example and the XPath here is just like I said we will use the a-link as an anchor and We've searched for the a-link which hasn't had a name of login and text of login in this case And since we now have the login element we can simply do trigger a click on it Which will Actually trigger a click on the login element here and you can see this little flickering and This is just selenium clicking on the link login link to get us to the login form here Perfect, so we now in the login form and can log us in we now have only have to send our username and password to the Login form and once we've done that we are logged in to Instagram, so let's check that out. We first have to again Check the page source for our elements and we'll also introduce a new concept here Which is called action chains and it does what it is called after it simply Chains actions together and In this case, we will just look for the form element and the login button and If we yet and translate this into code again we will get something like this and again we will use the browser element to find stuff by XPath and For the inputs, it's just for slash forum slash div slash input Which is of course XPath for our input form and Then we can instantiate a new action chain element which will pass the browser and we then have in access to some method here, which are move to element click send keys and perform which will trigger the whole action chain to execute and If we take a look at the code from bottom to from the top to the bottom We first move to the first input element, which is the username. We then collect the input element And this will really have a more nature natural feeling and a more human feeling than just triggering clicks by JavaScript and After we clicked into this user element, we can then send our username to it in this case It's contacting John though and once we've done that we can move to the second form element, of course and send our password in there After we entered our password in username. We can then simply create a new action chain and of course click the element and I just added a second action chain here because You have to see that you can also use it for one element only. You don't have to have like ten elements or two or three So again, let's take a look at what this code snippet does And in this case, it will just go in and and to use name and password and click the login element here And of course this will trigger our Instagram to load the feet feet page and we are actually now locked in successfully This is where it gets interesting because once we're locked in we can't instantly start clicking on these hearty icons because we When we take a closer look at Instagram and Twitter of course and Facebook all have this feature There is this little circle loading but loading circle on the bottom of the page Which will only get new content once we reach the bottom of the page So there's not and too big of an overhead here and this will just dynamically load new content into our page and Depending on how many images how many stuff we want we have to first get get hold of that and Make sure that We have enough elements on the page if we want to do 20 It's not a problem, but if we want to do 100 or 200 elements on our front page we will have to tackle this and Again, we will introduce new concept which are special keys and with the normal Send send keys method we can simply send text like our username and password But we have to use special keys for stuff like delete enter spacebar and in our case home the home and the end key which will trigger the page to go to the top of the page and The end key will trigger it to be a go to the bottom And let's translate this into code again This will lead to something like this We have to import the keys the special keys from selenium again and from then on we can simply use the keys and Here's a special special element which we have to define before the is the body element because we need an anchor to send our Commands to we need an anchor to send these keys to and we can't just use maybe The Instagram logo or something like that But we have to have really a solid solid element which will stay the same in this case it's the body element because it's of course the element you click in and you operate in when you're using the browser yourself and Then we can simply do a for loop and In this case, we will do it three times We will go to the bottom of the page to the top again to the bottom of the page And we'll sleep two seconds in between each to make sure that the loading actually succeeds and we will get new content So if we take another look at how this code gets executed You will see that it moves to the bottom of the page and it moves up again And it's it's all it does it just executes this three times in a row to make sure we got like 50 elements here to later like so there is only one step left here, which is Finding all the hard elements and clicking them and you should now be able to do this all by yourself because it's We introduced all the concepts here, which are finding elements and clicking elements, of course and If we take another look at the page source here We just have to find these hard icons and find another signature here Make sure that the signature is unique to make sure that we don't get a another element on the page but exactly the one we need we want to and In this case we Will use the class we will use several classes to make really make sure that we will get only these elements and We'll just loop over it and we will do some printing to make sure that We keep track of how many elements we already liked and If we enumerate through it, we will of course add another sleep to make sure we don't don't get banned here We will get something like this where We start up our script from the start and we will see everything we done right now So it starts a new browser. It navigates to Instagram once we're on Instagram. It will log us in and Once we're logged in it will then as we saw before start to move to the bottom of the page to the top again And once it's loaded enough images it can finally start to like some pictures We'll just wait a few seconds here until it's done and you can we'll then see that the page is Really like jumping a little bit. There you go. And you see that the hard icons turn red And this is really just a very simple Automation of the liking for your newsfeed and you can enter you can add your own Automation right now you can do filtering for friends. You can for example make sure you don't like your own pictures You can add commenting and you can add following for the people you actually see on the front page or maybe even Copy the pictures and posting yourself or something like that depends on what you really depends on what you want to do with it but as you see as you've seen right now we heavily rely on the page structure and this leads to some problems here because Instagram Facebook and all these big pages where it would be interesting to do some automation on they change their page layout pretty recently and pretty often and this really leads to breaks in the code and and make sure that the code is not working very long and This is really annoying to fix, but it's pretty quick actually and we'll look into some Ideas on how to make this more maintainable and get make sure that our code is solid more solid The second thing is different languages We saw that we rely on the log in text for searching for elements here and this is really Bad for different countries where we have for example here in Italy Of course would be Italian on in France with the French and so on so we can't we shouldn't rely on the page on the page language but Selenium offers a really nice feature here which we with which we can simply Forced the browser to use a specific language and we can just enforce it to use English in this case and the most annoying problem is AB testing and all of these big companies of course do AB testing for those who don't know AB testing is just Rolling out features to a small group in a specific area like for example, all right We have a new feature we will test it in only in Rimini and if the people in Rimini think this feature is good And like this feature and there are no problems with this feature They will roll it out incrementally to more and more people to make sure that every person on the planet if for these big companies gets a new feature and Testing this is really annoying because you have to make sure you get all the informations from the users. All right Where are they located? What's the what's the language of their system what system are they using and all of these small little details which will or Which can lead to a failure that is really unique to this to that person but Selenium Recommends pay a things an object design pattern for this page It's called page object design pattern and it of course is what it tells It's an object for every page So we would have an object for our login page would have an object for our news for page and so on And this really gets us more maintainable code because we only have One place to make our changes around the UI So in order to make changes right now We would have to go through all the whole code and make all the changes to the X path And this is really really annoying and we don't want to do that It also reduces a duplicated code because it has a Nice layer of abstraction here, which will move all the code to one one big bulb and in this Code and in this place there will be the only logic Third thing is it adds through this abstract abstraction layer It adds adds a nice API layer which we can then use in our actual code base to kind of get some readable code and and if we add some comments and Little hints what we did here. We can really get nice contributors and make sure that the contributors understand our code and can it contribute as well and Since we're all real programmers here We will look at an uml diagram of this page up to this design pattern here and it looks something like this well, no, yeah, just talking it looks more like this and As you can see here, this is an example of the Google search page where every Attribute is an element and in this case for example URL is an attribute the search box is an attribute and so on and Every method wraps an action for example navigation searching and clicking the field lucky button, for example and of course it is actually Test pattern, so we haven't actual test last year for each page object. We need a test object here and Don't know don't matter if you got this right now We will look at an example with Instagram here and we'll use the Instagram login form for this We'll we won't focus on on the app store buttons or the login The sign up a link here. We'll just focus on the username input the password input input and the login button here And if you again translate this into code We will get something like this where we have a class for install login page and we in within the constructor constructor we will define every attribute every element as we said recently In this case it will be the user box the password box in the login button and once we found all these elements We can then use it in the complete in the full class right now so in our case we will implement the login method and It will get past the username and password and here in in the login method We can then instantiate a new action chain and go through all the methods here And to move the element like we did before and One interesting thing about the page object design pattern is That it returns a new page object for the next page We will move to so in our case as you saw when we log in we will move to the insta Into the feed page where our news feed is located So we will return a new instance of the insta feed page object in our case so that not right now we will switch over to Instapy because you don't want to implement this all yourself and Think about how you can implement this and Break your head about what what changes instant from a sense something like that and I'm really Amazed and I'm really excited to talk about this here today because this is actually what led to this talk and I'm really excited how big this got and what the open source community made out of this and it is called Instapy of course and Right now we have around 2k stars on github, which is I think pretty nice for something like this Especially since there are a lot of services out there Which got token down by Instagram itself, but they were in so there were our services out there And this is really completely open source. Everyone can contribute and look at it and Even copy it if they want to I have 32 contributors which are from which four are into the core developer team right now and We got over 420 forks of this project and I Hope there is a lot more to come because I want to keep this alive and make sure that people can actually use it and This many contributors this many interested people leads them to some very interesting stuff like An active community means there is quick help for everyone. So if someone submits an issue there's an if if I can't Help him with this issue. They're like 100 200 people that maybe have have the same problem who can also comment on his stuff and And help me fix all the problems and make sure that the people actually can use this And to give you kind of an impression what Instapy can do Here's a little list of features Of course you can do following liking unfollowing commenting You can like by tag and you can target your users specifically by location for example if you have a Pizzeria or Any kind of shop you can make sure that only people in your location will be notified Of course it works on a server so you can use digital ocean or any kind of virtual server to run it It's has Docker support. You can write it. You can post emojis and One interesting thing is Clarify. I don't know how many of you know Clarify, but it's an image Smart image recognition API and with it you can make sure that you don't like any not-safe-work stuff or Inappropriate stuff or maybe you're you're from some country and you don't want to like New stuff or it depends on your preferences and you can really make sure that the comment pictures you comment on and your people You follow are not some kind of weirdos here and I think the API is pretty interesting too. So this is what it looks like you just have to import insta-pi from insta-pi and In the insta-pi constructor you can then just pass your username and password and after the look in you can do some settings in this case we will just make sure that we don't interact with any account that has more than two and a half thousand followers and We want to do commenting on ten percent as well as following on ten percent We want to use the comments cool awesome and nice And we want to make sure that this this bot don't interact with our real friends So we can make sure that our real friends are not included here And of course we can do some text search where not safe from work and naked are Excluded and what if some of these don't include words are included in the text It will skip the page the picture and not like it and then we can finally do some liking by tags For example in this case we will just like a hundred pictures for nature and cat And let's talk about open source in general and why it's important and I'm Pretty sure a lot of people in here already do open source and this is awesome, but those who don't know those who don't do it You really have to think think about it if you have any project on your on your hard drive that could be open source Why not do it? I mean all of all in here already use open source for their projects But why not give something back to the community if you have something laying around it doesn't matter if you Contribute it to GitHub or GitLab and make it open source for people to use it And I think this is really the most important thing to think about It's also good for new developers to build a portfolio because people will find you on the Internet and if you got great stuff They may might contact you might want to work with you and even companies get interested in you and This is where you get opportunities from and if you if you really look interesting they'll contact you so you didn't have to send out any Any papers or something like that to make sure to apply applicator companies To make it Nice for you and give you a little bit of motivation Here is what open source did for me by now I got 12 job offers and nine internship Internship offers sorry from around the world as well as a ton of new contacts that contacted me through Instapy And I think this really speaks for itself if you got something that's that's interesting and maybe a little bit controversial it's really Just a matter of time until until people think about it and and talk about it and really Give you feedback on it and want to help out and these job offers and contacts are from around the world including Sweden, Sydney, Argentina, Brazil, the UK USA even from Ireland and Singapore or Shanghai and I think this really speaks for itself and of course I got this talk from it, which is Nice for me as well to close this off here are some tips I think are really important for you open source portfolio if you're interested in doing in doing some open source Stuff make sure to include or to keep this information and keep make sure to improve these tips here The first one is at documentation. I've seen so many projects out there who has not Have have no real documentation and this is this is really crucial I know it's annoying for people to write documentation But if people don't understand your project or don't even know what the features of your project are this is It's just insane it how should they use should they look take a look into the peg code. I don't think so Really a few people actually do that The second it's spread the word you can use channels like hack and use reddit or dev post something like this to make sure that people actually see your Pro of your portfolio and your projects to make sure that you get feedback and and people to Get people to talk about your tool And if nobody since nobody if nobody hears about your project, of course, nobody will use it The last one is supported users and this is really time-consuming, but it's really worth it It's a lot of work, but it's worth it and If somebody uses your tool and it he stops using your tool Because if they don't get it to work, they will never come back and you'll you lost a chance to get a new user So what what's next for for Instapy and this project? we want you to Take a look at it. Maybe contribute it use it give us feedback and maybe add some issues or something like that We will we want to add new features like a GUI smarter following and then more human automation and like I said if you have any questions or Recommendations offers anything just make sure to contact me on any of the social medias or Gmail and I'll be happy to chat later or through one of these channels. Thank you for your attention You