<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Mladen Ružičić</title>
    <description>The latest articles on DEV Community by Mladen Ružičić (@ruzicic).</description>
    <link>https://dev.to/ruzicic</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F65440%2F9c7c28a2-2f22-4110-997b-ca3544df72b4.jpeg</url>
      <title>DEV Community: Mladen Ružičić</title>
      <link>https://dev.to/ruzicic</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ruzicic"/>
    <language>en</language>
    <item>
      <title>How I made Instagram scraper on Firebase Console</title>
      <dc:creator>Mladen Ružičić</dc:creator>
      <pubDate>Wed, 20 Feb 2019 12:34:40 +0000</pubDate>
      <link>https://dev.to/ruzicic/how-i-made-instagram-scraper-on-firebase-console-4ddf</link>
      <guid>https://dev.to/ruzicic/how-i-made-instagram-scraper-on-firebase-console-4ddf</guid>
      <description>&lt;p&gt;The last couple of days I read about Instagrams new &lt;a href="https://developers.facebook.com/docs/instagram-api/"&gt;Graph API&lt;/a&gt; and was thinking about some creative ways of using it. To my surprise, they did not support anything related to user registration or at least account username availability. After some research, I saw one “tool” doing exactly that - checking if a provided text is a valid string and available Instagram username. &lt;br&gt;
But now, I don't care about Graph API - I want to figure out how did they make it, without API!&lt;/p&gt;
&lt;h2&gt;
  
  
  Research
&lt;/h2&gt;

&lt;p&gt;Of course, the first thing I tried was to inspect their source code and check network requests. All I could see was that it’s something hosted on Heroku. No help. After that, I went to Instagram official sign up page and inspected their code. &lt;code&gt;instagram.com/accounts/web_create_ajax/&lt;/code&gt;  - I googled it - turns out it’s not publicly available.&lt;/p&gt;

&lt;p&gt;Ok, now I want to create the tool myself. &lt;em&gt;Why?&lt;/em&gt; Because I was the one googling "check Instagram username availability" a few days ago, so I hope I am not the only one who refuses to go to the official site to do that. &lt;em&gt;Expectations?&lt;/em&gt; Coding all day and learning about new technologies. &lt;/p&gt;
&lt;h2&gt;
  
  
  Idea
&lt;/h2&gt;

&lt;p&gt;After investigating the behavior of their form validation, my first idea was a NodeJS script, running puppeteer, populating Instagrams official sign up form and waiting for success or error element to show up on the screen. (I wrote some e2e tests at work last week, so I'm totally into this at the moment).&lt;br&gt;
The plan for puppeteer is:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Navigate to the Instagram signup page&lt;/li&gt;
&lt;li&gt;Click username input and fill in some text&lt;/li&gt;
&lt;li&gt;Click body (to trigger blur event validation check)&lt;/li&gt;
&lt;li&gt;Observe DOM and return whether the field is valid&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  e2e - for different needs
&lt;/h2&gt;

&lt;p&gt;This is what I came up with, and - it was working!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--yRYUpsrY--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/t2lkfnqcyub1rplfa5h0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--yRYUpsrY--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/t2lkfnqcyub1rplfa5h0.png" alt="Puppeteer" title="Puppeteer Crawling Instagram"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Note: Since the puppeteers &lt;a href="https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#pagewaitforselectorselector-options"&gt;waitForSelector&lt;/a&gt; method will throw if an element does not appear in the DOM, I will assume it does not exist, and the username input field is valid.&lt;/p&gt;

&lt;p&gt;Now, when I know the script works, let's deploy it!&lt;/p&gt;
&lt;h2&gt;
  
  
  Adjustments for Firebase Functions
&lt;/h2&gt;

&lt;p&gt;For quite some time, I wanted to make practical use of Firebase Functions. This was a perfect moment to try it.  I had some experience using Firebase before, so I went to &lt;a href="https://console.firebase.google.com"&gt;Firebase Console&lt;/a&gt;, created a new project and initialized it locally.&lt;/p&gt;

&lt;p&gt;There are two types of Firebase Functions:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The ones you call from Firebase app (an app hosted on Firebase, or an app authenticated with Firebase)&lt;/li&gt;
&lt;li&gt;The ones you want anybody to access - through HTTP requests.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;I wanted both. One for my web app, and the other for everyone else.&lt;/p&gt;

&lt;p&gt;First things first. Create a new project on Firebase Console, go to Functions tab and click "Get Started" with functions.&lt;br&gt;
Second thing, install &lt;a href="https://www.npmjs.com/package/firebase-tools"&gt;Firebase CLI&lt;/a&gt; locally:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install -g firebase-tools
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Authenticate to your Firebase account&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;firebase login
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;And initialize a new project, answering the on-screen questions (defaults are just fine)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;firebase init
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;It is important to configure your app to use the Firebase project you created a minute ago. Use &lt;code&gt;firebase use --add&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Function - onRequest
&lt;/h3&gt;

&lt;p&gt;Okay. Now let's make this function available through an HTTP request. We must use &lt;a href="https://firebase.google.com/docs/functions/http-events"&gt;onRequest&lt;/a&gt;. Arguments are the same as for Express.js - request and response, meaning the Request object gives you access to the properties of the HTTP request sent by the client, and the Response object gives you a way to send a response back to the client.&lt;br&gt;
You can easily create a new Express app and export it as a Firebase Function, but that would be overkill for this use case.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--8J_W56DW--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/7w8a5s77o2rihxsvix86.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--8J_W56DW--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/7w8a5s77o2rihxsvix86.png" alt="onRequest" title="Firebase Functions onRequest"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Notice the CORS wrapper - required for accessing this function from outside our Firebase app domain.&lt;/p&gt;

&lt;p&gt;We can test it right now using Postman&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;GET https://us-central1-your-project-name.cloudfunctions.net/check?username=awesome_username_99
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;and confirm it's working - Status 200 OK&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"available"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h3&gt;
  
  
  Function - onCall
&lt;/h3&gt;

&lt;p&gt;Now, the easier part - export a &lt;a href="https://firebase.google.com/docs/functions/callable"&gt;callable function&lt;/a&gt; to use from within Firebase app.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--OijHuGtu--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/o3ouzmnfl8z4ey2y9ymj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--OijHuGtu--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/o3ouzmnfl8z4ey2y9ymj.png" alt="onCall" title="Firebase Functions onCall"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Web App - httpsCallable
&lt;/h3&gt;

&lt;p&gt;What about testing this one? I want a simple webpage. Actually, validating input in vanilla javascript took me a while, but I liked it. If you spend most of your time using angular/vue/react/whatever, you probably forgot what it takes to check and set a validity to form elements, at least I did. I'll skip the boring part (link to the source code will be at the end of text).&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--iuu1_Te5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/bsi9w3rr6g17hwy3cde4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--iuu1_Te5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/bsi9w3rr6g17hwy3cde4.png" alt="httpsCallable" title="Firebase Functions httpsCallable"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Once I was happy how my form looked I deployed it to Firebase Hosting and tested.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;firebase deploy 
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;I took it a step further - bought an SEO-friendly domain name, set up Google Tag Manager and Google Analytics Goals, to actually track how many visitors (if I get any) hit available username.&lt;/p&gt;

&lt;p&gt;P.S. I actually spent more time getting familiar with Google Tag Manager and writing this blog post, than I did coding. xD&lt;/p&gt;

&lt;p&gt;You can see this app live at &lt;a href="https://instagram-username.firebaseapp.com/"&gt;https://instagram-username.firebaseapp.com/&lt;/a&gt; and the source code at &lt;a href="https://github.com/cedevita/instagram-username"&gt;GitHub&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Update February 27.
&lt;/h2&gt;

&lt;p&gt;Instagram asked me to stop using domain containing their name (two days after I  registered instagram-username.com), so I did. Demo still available on firebase subdomain.&lt;/p&gt;

</description>
      <category>node</category>
      <category>firebase</category>
      <category>instagram</category>
      <category>puppeteer</category>
    </item>
  </channel>
</rss>
