<?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: Hrithwik Bharadwaj</title>
    <description>The latest articles on DEV Community by Hrithwik Bharadwaj (@hrithwik).</description>
    <link>https://dev.to/hrithwik</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%2F633866%2F1ab35a4a-d4c6-457b-9a69-ae0f930631c3.jpeg</url>
      <title>DEV Community: Hrithwik Bharadwaj</title>
      <link>https://dev.to/hrithwik</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/hrithwik"/>
    <language>en</language>
    <item>
      <title>Reddit To Instagram - A Meme BOT</title>
      <dc:creator>Hrithwik Bharadwaj</dc:creator>
      <pubDate>Wed, 14 Jul 2021 07:42:59 +0000</pubDate>
      <link>https://dev.to/hrithwik/reddit-to-instagram-a-meme-bot-1m66</link>
      <guid>https://dev.to/hrithwik/reddit-to-instagram-a-meme-bot-1m66</guid>
      <description>&lt;h1&gt;
  
  
  The Story behind
&lt;/h1&gt;

&lt;p&gt;Most of my time is spent on Instagram and I use it mostly for the memes.&lt;/p&gt;

&lt;p&gt;So, some of my friends told me that most of the memes are uploaded to Reddit and later spread to Instagram, and Reddit was the best place to go for funny content but I wasn't a big fan of the Reddit UI and didn't really get used to it.&lt;/p&gt;

&lt;p&gt;One day I randomly thought, what if I could start a meme page that gets content from Reddit but without downloading and uploading the images manually and still be the first account to upload those memes on Instagram straight from Reddit?&lt;/p&gt;

&lt;p&gt;And, I Built a BOT which uploads memes from some subreddits like &lt;strong&gt;r/dankmemes&lt;/strong&gt; and &lt;strong&gt;r/memes&lt;/strong&gt; and uploads it on my Instagram account - &lt;a href="https://instagram.com/thiru.bot"&gt;@thiru.bot&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  How did I do it?
&lt;/h2&gt;

&lt;p&gt;At this stage, I was a beginner and knew few things about python, had some experience building flask apps, had some experience in web scrapping and had huge hunger to build this just to flex my skills with my friends.&lt;/p&gt;

&lt;p&gt;Before I started building it I had to visualize what I needed?&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Reddit API &amp;amp; Basics of web scrapping to get the images and save them.&lt;/li&gt;
&lt;li&gt;Any Instagram API which lets me upload photos to my account.&lt;/li&gt;
&lt;li&gt;A server to host this later to make it work 24/7&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;Friends, When you are a beginner you just google things, mix code from different repositories, add your own code and make it work.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;and that's what I did!&lt;br&gt;
There were no Instagram Reddit bot but I found a Reddit to twitter project which gave me a clarity of thought in building this.&lt;/p&gt;
&lt;h2&gt;
  
  
  The Working Instagram API
&lt;/h2&gt;

&lt;p&gt;The Official Instagram API doesn't allow developers to upload photos or videos to post. Most of the other third party APIs also dint work but at last found this pip package.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;pip&lt;/span&gt; &lt;span class="n"&gt;install&lt;/span&gt; &lt;span class="n"&gt;instabot&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Later Tested if this worked by giving my Instagram credentials and uploaded a random photo from my computer.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;instabot&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;Bot&lt;/span&gt;
&lt;span class="n"&gt;bot&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;Bot&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="n"&gt;bot&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;login&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;username&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"thiru.jpg"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;password&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"followMeOnInstagram"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;bot&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;upload_photo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'hey.png'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;caption&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Test Upload "&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and it worked :)&lt;/p&gt;

&lt;p&gt;Next step - Scrapping the latest images from subreddits&lt;/p&gt;

&lt;h2&gt;
  
  
  Getting Images from Reddit
&lt;/h2&gt;

&lt;p&gt;To get the latest submissions from our subreddits we will be using PRAW API from subreddit. it's pretty simple not to worry.&lt;/p&gt;

&lt;h3&gt;
  
  
  Setting up Praw API
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;reddit&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;praw&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Reddit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;client_id&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;''&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="n"&gt;client_secret&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;''&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="n"&gt;username&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;''&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="n"&gt;password&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;''&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="n"&gt;user_agent&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;''&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Creating an Reddit App
&lt;/h3&gt;

&lt;p&gt;To Fill the above details you need to create an app on Reddit, Just Open &lt;a href="https://ssl.reddit.com/prefs/apps/"&gt;https://ssl.reddit.com/prefs/apps/ &lt;/a&gt; and click on create app.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--sGLgylaZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1596813314528/05I9oEKoA.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--sGLgylaZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1596813314528/05I9oEKoA.png" alt="praw.PNG"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I am here creating an app with the name test app. Client_Id parameter (the one which is below personal use script in the image above), client_secret is the secret key available. For username and password give your Reddit username and password. User Agent can be blank or chrome.&lt;/p&gt;

&lt;h3&gt;
  
  
  Setting up subreddits to get memes
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;red&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;'dankmemes'&lt;/span&gt;
&lt;span class="n"&gt;subreddit&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;reddit&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;subreddit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;red&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;newMemes&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;subreddit&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;hot&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;limit&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I choose the subreddit 'dankmemes' and ** NewMemess ** Praw Class has all the hottest submissions from the subreddit.&lt;br&gt;
You can get any type of submissions&lt;br&gt;
(refer to official documentation &lt;a href="https://praw.readthedocs.io/en/latest/code_overview/models/subreddit.html"&gt;here&lt;/a&gt; )&lt;/p&gt;
&lt;h2&gt;
  
  
  Validating and Picking only Photos
&lt;/h2&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;submission&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;new_memes&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;submission&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;is_self&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="bp"&gt;True&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Post was text, skipping to next post."&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;continue&lt;/span&gt;
    &lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;pass&lt;/span&gt;
&lt;span class="n"&gt;url&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;submission&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;url&lt;/span&gt;
&lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sleep&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;waitTime&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;You want just photos to upload. &lt;br&gt;
So, You need to validate them and choose only the submissions which have photos.&lt;/p&gt;
&lt;h2&gt;
  
  
  Downloading the images
&lt;/h2&gt;

&lt;p&gt;Before Storing the images from Reddit we check Two Things&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;if the submission is an Video/text or an image using &lt;strong&gt;isImageLink(url) Function&lt;/strong&gt; and it returns the extension name of the image(Eg: jpg,png)&lt;/li&gt;
&lt;li&gt;If the image was posted before using &lt;strong&gt;already_uploaded(id)&lt;/strong&gt; function so that we don't post the same memes again.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You can check out those functions and full code on my GitHub Repo &lt;a href="https://github.com/hrithwikbharadwaj/redditToInstaBot"&gt;here&lt;/a&gt; also follow me on GitHub.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;isImageLink&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;url&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="ow"&gt;and&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="n"&gt;already_uploaded&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;id&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;img&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;requests&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;submission&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;url&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;filename&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;counter&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="s"&gt;'.'&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="n"&gt;IsImageLink&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;submission&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;url&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;filename&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'images'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;filename&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;imagefile&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nb"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;filename&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;'wb'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;imagefile&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;write&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;img&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;content&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;imagefile&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;close&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="n"&gt;post_ids&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;submission&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;# A list of all submission ids
&lt;/span&gt;        &lt;span class="n"&gt;log_insta&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;post_ids&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;photoAlbum&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;append&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;&lt;span class="s"&gt;'File'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="n"&gt;filename&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;'Title'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="n"&gt;submission&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;title&lt;/span&gt;&lt;span class="p"&gt;})&lt;/span&gt;
        &lt;span class="c1"&gt;#dictionary of all photos
&lt;/span&gt;        &lt;span class="n"&gt;counter&lt;/span&gt;&lt;span class="o"&gt;+=&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;
    &lt;span class="k"&gt;except&lt;/span&gt; &lt;span class="nb"&gt;Exception&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After checking, You can download the image and save it with the count+ the extension (Eg: 2.png or 3.jpg). &lt;/p&gt;

&lt;p&gt;Later you save a list with all the post id.&lt;/p&gt;

&lt;p&gt;Remember you had to check if the post was already uploaded. How will it know if it is posted already?&lt;br&gt;
Yes, you will be logging the post_id of the image using the function ** log_insta(post_ids)**&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;log_insta&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;post_ids&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;post_id&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;post_ids&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="nb"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;POSTED_CACHE&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;'a'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;out_file&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="n"&gt;out_file&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;write&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;post_id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;'&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Downloading the images from Imgur
&lt;/h3&gt;

&lt;p&gt;Some Redditors upload photos to Imgur, and to download those images, You will using &lt;strong&gt;&lt;em&gt;beautifulSoup&lt;/em&gt;&lt;/strong&gt; and scrape data from those submissions. After you download, the saving part is same as above so I won't be posting the whole code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;elif str(submission.url).lower().startswith('https://imgur.com') or str(submission.url).lower().startswith('http://imgur.com') and counter&amp;lt;max_images:

    try:
        html_page = urllib.request.urlopen(submission.url)
        soup = BeautifulSoup(html_page, 'lxml')
        images = []
        for img in soup.findAll('img'):
        images.append('https:'+img.get('src'))

        img=requests.get(images[0])
        filename=str(counter)+'.'+images[0][-3:]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Downloaded the images, What next?
&lt;/h2&gt;

&lt;p&gt;Now that we have all the 3 images in the photo album Dictionary with the File and Title we can upload it to instagram using our *** unofficial Instagram API *** and give credits to the authors,subreddit and also add captionTags.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;authors = ''.join(str(e + ', ') for e in authors)
captionTags='#binod #memes #dank'
for photo in photoAlbum:
    img=photo['File']
    captionText=photo['Title']
bot.upload_photo(img,caption=(captionText + '\n' +'Reddit Authors:'+authors+'\n' captionTags))
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Later we delete the image we downloaded just not to waste disk space.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;for filename in glob(filePath + '/*'):
os.remove(filename)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;** Now Put all this code together into a single Python File, Run it and Hola, You have now uploaded a meme from Reddit to Instagram from your Python Code ** In my case, I named the file  &lt;a href="https://github.com/hrithwikbharadwaj/redditToInstaBot/blob/master/bot.py"&gt;bot.py&lt;/a&gt;, thought of telling you this as this might be useful later in the deployment part.&lt;/p&gt;

&lt;p&gt;But the &lt;a href="https://github.com/hrithwikbharadwaj/redditToInstaBot/blob/master/bot.py"&gt;bot.py&lt;/a&gt; is run on the local machine and we can't just run this 24/7 on our local machine. Any Fix?&lt;/p&gt;

&lt;h2&gt;
  
  
  Deployment
&lt;/h2&gt;

&lt;p&gt;To Run The Bot in a server for 24/7 you need to deploy this to cloud.&lt;br&gt;
I used Heroku to deploy this Instagram meme bot. Follow this article to set up a Heroku account and know the basics of Heroku. I will soon be writing an article on the same.&lt;a href="https://medium.com/the-andela-way/deploying-your-flask-application-to-heroku-c99050bce8f9"&gt;How to Upload Flask apps to Heroku&lt;/a&gt; .&lt;/p&gt;

&lt;p&gt;You need 3 files to be created for the deployment of your bot into &lt;strong&gt;Heroku&lt;/strong&gt; .&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;ProcFile&lt;/li&gt;
&lt;li&gt;Runtime.txt&lt;/li&gt;
&lt;li&gt;Requirements.txt&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;** Runtime.txt **&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;python-3.7.3
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In *** Runtime.txt *** you specify what language your code is in.&lt;/p&gt;

&lt;p&gt;** The ProcFile **&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;web&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;python3&lt;/span&gt; &lt;span class="nx"&gt;bot&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;py&lt;/span&gt;
&lt;span class="nx"&gt;worker&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;python3&lt;/span&gt; &lt;span class="nx"&gt;bot&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;py&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here you will be specifying the python file to be run by the dynos and note that I created two of these just to make sure one of them always stays on and doesn't sleep. You later need to scale them.&lt;/p&gt;

&lt;p&gt;** Requirements.txt **&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;imageio==2.4.1
praw
requests
urllib3
keyboard
pillow
beautifulsoup4
instabot
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Heroku in simple words creates a virtual machine so you need to mention what are the packages you want to install for your &lt;strong&gt;bot.py&lt;/strong&gt; to work.&lt;/p&gt;

&lt;p&gt;After Uploading these files into a single folder. It's time to scale your dynos&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;heroku ps:scale web=1 worker=1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  A working meme BOT
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--4dR9aBIJ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1597081036441/GATenoKV9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--4dR9aBIJ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1597081036441/GATenoKV9.png" alt="thiru.bot.PNG"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;But yeah , I need to work on the followers count lol.&lt;/p&gt;

&lt;h2&gt;
  
  
  Problems &amp;amp; Suggestions
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Instagram Detecting Bots&lt;/li&gt;
&lt;li&gt;Upload Frequency&lt;/li&gt;
&lt;li&gt;Heroku Running out of Credits&lt;/li&gt;
&lt;li&gt;Dynamic Tags&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Instagram Detecting Bots
&lt;/h3&gt;

&lt;p&gt;Instagram DOES NOT like Bots. &lt;/p&gt;

&lt;p&gt;Initially, when I started this, my bot was detected and banned for 1 day and was not allowed to use any Instagram features like liking, etc for 1 week.&lt;/p&gt;

&lt;h4&gt;
  
  
  Solution which worked for me
&lt;/h4&gt;

&lt;p&gt;It took me weeks to crack this but the reason why my bot was getting detected is that my posts were only from one subreddit and the authors were mostly same and hashtags as well so every post had somethings similar and with same upload frequency and Instagram started detecting.&lt;/p&gt;

&lt;p&gt;With no options left, I had to stop giving credits to authors in the caption and created a list of subreddits and used ** random ** function in Python to select one subreddit and stoped using tags which fixed the issue.Upload frequency also matters and I will be speaking about it in the next paragraphs.&lt;/p&gt;

&lt;h3&gt;
  
  
  Upload Frequency
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;How many Posts should you upload at a time?&lt;/p&gt;

&lt;p&gt;How many hours once should you upload?&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The safe bet is ** 3 posts /hour **&lt;/p&gt;

&lt;h3&gt;
  
  
  Heroku Running out of credits
&lt;/h3&gt;

&lt;p&gt;Heroku gives 550 hours of free dynos when you create an account. I had used all the 550 dynos and all my projects on Heroku had shut down, luckily I gave my credit card information and was able to get an extra 450 hours of dynos, But that won't be sufficient if you want to run this bot forever as 30 days = 1800 hours.&lt;br&gt;
I usually was turning off the dynos when I was going to sleep and turning it on when required so worked. This is the solution that worked out for me and just to remind you, this is hosted for completely free of cost so nothing to complain.&lt;/p&gt;

&lt;h3&gt;
  
  
  Dynamic Tags
&lt;/h3&gt;

&lt;p&gt;So, removing tags helped with not getting banned but I wanted better reach as my account had just 3 followers from weeks so created multiple lists of ** trending hashtags ** and wrote a code to choose the hashtags according to the subreddit and its working great.&lt;/p&gt;

&lt;h3&gt;
  
  
  What Did we learn from this?
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;You Don't need to know everything to start building things. Just the Hunger is enough.&lt;/li&gt;
&lt;li&gt;The results don't come just in the first iteration. Experiment Fix experiment.&lt;/li&gt;
&lt;li&gt;You can't get a girlfriend by creating a Meme bot :(&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Github Repository
&lt;/h3&gt;

&lt;p&gt;I still haven't added a ReadMe file(working on it). But this article is sufficient to understand the working of the same.&lt;/p&gt;

&lt;p&gt;** Here is the complete Code - &lt;a href="https://github.com/hrithwikbharadwaj/redditToInstaBot"&gt;GitHub Repo&lt;/a&gt; **&lt;/p&gt;

</description>
      <category>instagramapi</category>
      <category>reddit</category>
      <category>bot</category>
      <category>python</category>
    </item>
    <item>
      <title>Github Copilot - The Good and the BAD</title>
      <dc:creator>Hrithwik Bharadwaj</dc:creator>
      <pubDate>Wed, 14 Jul 2021 07:39:57 +0000</pubDate>
      <link>https://dev.to/hrithwik/github-copilot-the-good-and-the-bad-4d31</link>
      <guid>https://dev.to/hrithwik/github-copilot-the-good-and-the-bad-4d31</guid>
      <description>&lt;p&gt;I have been trying to generate code with the help of GPT3 from few months now and recently got access to a better product called &lt;strong&gt;Github copilot&lt;/strong&gt;. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://copilot.github.com/" rel="noopener noreferrer"&gt;&lt;strong&gt;Github Copilot&lt;/strong&gt;&lt;/a&gt;  is an VS Code extension which can autocomplete your code and also synthesize code by seeing your comments and function name. This is built using the same model and trained on billions of public code.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1625455957844%2F-90gokQHv.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1625455957844%2F-90gokQHv.jpeg" alt="gpt3copilot.JPG"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When I told my friends about Github Copilot  the first question I got is&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Can it do DSA Problems and help me crack into FAANG xD?&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This was funny but I wanted to try it out and the results were mind blowing. AI could solve most of the DSA problems and I have shared some results  &lt;a href="https://richurl.tech/dsacopilot" rel="noopener noreferrer"&gt;here &lt;/a&gt; .&lt;/p&gt;

&lt;p&gt;Later I tried using github copilot in some real development work like writing email validation , making api calls to github and even building a simple email sending client.&lt;/p&gt;

&lt;p&gt;Now I have some clear idea on the limitations and things that Copilot is good at this movement .&lt;/p&gt;

&lt;p&gt;Before moving on to the article if you prefer videos over an article, Here is my video on  &lt;a href="https://www.youtube.com/watch?v=9Pw-Roo_duE" rel="noopener noreferrer"&gt;Solving DSA Problems using Github Copilot&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.youtube.com/watch?v=9Pw-Roo_duE" rel="noopener noreferrer"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1625455007755%2FXNtYm-8jT.jpeg" alt="copilot.jpg"&gt;&lt;/a&gt; &lt;/p&gt;

&lt;h2&gt;
  
  
  The Good✨
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Forgot the Syntax ? No worries. AI got your back
&lt;/h3&gt;

&lt;p&gt;When building a project I had forgot status code for what response to send and writing a comment about it gave me a result. Also I had written python after long time and at one point I had forgot the syntax for slicing and this helped me there aswell.&lt;/p&gt;

&lt;h3&gt;
  
  
  Can help you with Email Validation and API Calls
&lt;/h3&gt;

&lt;p&gt;As I shared before it was really easy to &lt;strong&gt;generate simple functions&lt;/strong&gt;  for calling some  &lt;a href="https://richurl.tech/8bRT4sxX6" rel="noopener noreferrer"&gt;API's and write some regex code&lt;/a&gt;  without learning it or depending on &lt;strong&gt;documentation&lt;/strong&gt;. ( Not recommended if you are a beginner) . &lt;/p&gt;

&lt;h3&gt;
  
  
  Bioler Plate code
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1625456225356%2F0uYrXfTAj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1625456225356%2F0uYrXfTAj.png" alt="vlcsnap-2021-07-05-09h06m42s448.png"&gt;&lt;/a&gt;&lt;br&gt;
It can also help you get like a boiler plate code for express and helped me write some mongo schemes aswell .&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Overall I just love how seamlessly  Github Copilot can show you suggestions on vs code. Beautifully done&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  Limitations 🙃
&lt;/h2&gt;

&lt;p&gt;Github Copilot for an first generation/ Beta product is way too good. But there are a lot of biased articles online so I just wanted to show the other side aswell.&lt;/p&gt;

&lt;h3&gt;
  
  
  Doesn't Follow the Best Practices
&lt;/h3&gt;

&lt;p&gt;The code synthesized doesn't always follow the best practices or is not optimized to use it for backend development. Like in Javascript, it generates &lt;strong&gt;var&lt;/strong&gt; and &lt;strong&gt;==&lt;/strong&gt; than &lt;strong&gt;const *&lt;em&gt;and *&lt;/em&gt;===&lt;/strong&gt; which can lead to different bugs and shadowing.&lt;/p&gt;

&lt;p&gt;Checkout my article on  &lt;a href="https://blog.hrithwik.me/10-best-javascript-practices-recommended-by-top-developers" rel="noopener noreferrer"&gt;best practices for JS developers&lt;/a&gt; &lt;/p&gt;

&lt;h3&gt;
  
  
  Version Conflicts
&lt;/h3&gt;

&lt;p&gt;Some Code Generated might not work for the version your codebase is on and can lead to conflicts. Also sometimes AI can give out a lot of unnecessary code. Developers with experience can definitely handle this out but what if a newbie directly starts with the help of AI, he will spend more time on stack overflow than writing actual code. ( oh wait that's how most of the developers are lmao)&lt;/p&gt;

&lt;h3&gt;
  
  
  Weird Copyright messages
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1625456179507%2Fn5Oyz_WBL.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1625456179507%2Fn5Oyz_WBL.png" alt="Creating an actual project using Github Copilot.png"&gt;&lt;/a&gt;&lt;br&gt;
When I tried to generate code for converting linkedlist into a tree which is like a famous DS problem I started AI started showing a Copyrighted code and it was from a person called Stephen. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Hey Stephen, If you are reading this, please follow me on hashnode .&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  Frequently asked Questions
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;What's Good ? &lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;It's great if you want to create functions like email validation or user schema or API request since it has enough data but again some code might not really be efficient.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;Can it help you crack DSA Problems &lt;strong&gt;(if it is legal)&lt;/strong&gt; ? &lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;The Answer is not  binary. it can help you give building blocks according to your needs and you are the one who should build the building.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;Will it Replace Humans ?&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;Absolutely NOT. As the name suggests ,  it's  a copilot making your life easier. You are the pilot who can understand human narratives and stories to build product from code.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Let's Connect
&lt;/h3&gt;

&lt;p&gt;Thanks for reading my article. I want to make new friends in the dev world. Let's connect on  &lt;a href="//twitter.com/hrithwik_"&gt;Twitter&lt;/a&gt; and &lt;a href="https://www.linkedin.com/in/hrithwik-bharadwaj" rel="noopener noreferrer"&gt;LinkedIn &lt;/a&gt; .&lt;/p&gt;

&lt;p&gt;Also a tiny Observation&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;After using this to write my project code for more than 2 hours I got so used to copilot that without using github copilot suggestion I literally didn't know what to write next in my code . I am a little scared. IT's a TRAP to make developers even lazy xD . Can't wait to see the pricing.&lt;/p&gt;
&lt;/blockquote&gt;

</description>
    </item>
  </channel>
</rss>
