Lets make an quote-of-the-day instagram bot!
source link: https://dev.to/ryandaghost/lets-make-an-quote-of-the-day-instagram-bot-3lbk
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Lets get started
π Demo
https://www.instagram.com/dailydouseofquotes/
Make a files: main.py
Starting the code
π§ͺ Importing the libaries
import urllib.request
import os
from PIL import Image, ImageDraw, ImageFont, ImageOps
import requests
from instabot import Bot
π· Getting an image and saving it
def dl_image():
image_url = "https://source.unsplash.com/user/eberhardgross/1080x1080/"
urllib.request.urlretrieve(image_url, "image.jpg")
print("Installing image")
The following code will get an 1080x1080 image by the author eberhardgross from unsplash, Why him ? I haven't seen anything 18+ from this user and have seen amazing images only!
π Getting the daily quote
def dailyquote():
response = requests.get('https://quotes.rest/qod.json?language=en')
response_output = response.json()
return response_output
This code will get a json daily quote of the day response from quotes.rest, as you can see we will output certain results that we from FROM the dailyquote function later!
π Putting the quote and image in one!!
π±βπ Fixing the line order
We will be doing this so we can get a good word-per-line system, so we see the whole text!
def wrap_by_word(s, n):
a = s.split()
ret = ''
for i in range(0, len(a), n):
ret += ' '.join(a[i:i+n]) + '\n'
return ret
π¨ Lets do it all!
def text_overlay_ig(quote, author):
quote = wrap_by_word(quote, 6)
image_file = os.path.join(os.path.dirname(
os.path.realpath(__file__)), 'image.jpg')
im = Image.open(image_file)
Fixing the quote line order and then opening our image file
Time to put on everything!
draw = ImageDraw.Draw(im)
font = ImageFont.truetype('font.ttf', size=60)
(x, y) = (125, 300)
shadowColor = (0, 0, 0)
thickness = 4
draw.text((x - thickness, y - thickness), quote,
font=font, fill=shadowColor, thick=thickness)
draw.text((x + thickness, y - thickness), quote,
font=font, fill=shadowColor, thick=thickness)
draw.text((x - thickness, y + thickness), quote,
font=font, fill=shadowColor, thick=thickness)
draw.text((x + thickness, y + thickness), quote,
font=font, fill=shadowColor, thick=thickness)
draw.text((x, y), quote, spacing=4, fill=(
255, 255, 255), font=font)
(x, y) = (125, 700)
author = "β " + author
draw.text((x - thickness, y - thickness), author,
font=font, fill=shadowColor, thick=thickness)
draw.text((x + thickness, y - thickness), author,
font=font, fill=shadowColor, thick=thickness)
draw.text((x - thickness, y + thickness), author,
font=font, fill=shadowColor, thick=thickness)
draw.text((x + thickness, y + thickness), author,
font=font, fill=shadowColor, thick=thickness)
draw.text((x, y), author, spacing=4, fill=(
255, 255, 255), font=font)
im.save("image.jpg")
π Adding a font.tff file!
i'd recommend using https://github.com/9ebd7134/Python-quote-of-the-day-instagram-bot/blob/master/font.ttf just click on download and put it in your folder ! The font is easy to read so its perfect
And now lets put on the quote on the image
(x, y) = (125, 300)
shadowColor = (0, 0, 0)
thickness = 4
draw.text((x - thickness, y - thickness), quote,
font=font, fill=shadowColor, thick=thickness)
draw.text((x + thickness, y - thickness), quote,
font=font, fill=shadowColor, thick=thickness)
draw.text((x - thickness, y + thickness), quote,
font=font, fill=shadowColor, thick=thickness)
draw.text((x + thickness, y + thickness), quote,
font=font, fill=shadowColor, thick=thickness)
draw.text((x, y), quote, spacing=4, fill=(
255, 255, 255), font=font)
The x, y are the position of the text we want to put, change this but be careful this won't change based on the text length! We could do that but lets stick with this for now
Everything else is us just putting the text alongside the text shadow on the image.
Time to put the author
(x, y) = (125, 700)
author = "β " + author
draw.text((x - thickness, y - thickness), author,
font=font, fill=shadowColor, thick=thickness)
draw.text((x + thickness, y - thickness), author,
font=font, fill=shadowColor, thick=thickness)
draw.text((x - thickness, y + thickness), author,
font=font, fill=shadowColor, thick=thickness)
draw.text((x + thickness, y + thickness), author,
font=font, fill=shadowColor, thick=thickness)
draw.text((x, y), author, spacing=4, fill=(
255, 255, 255), font=font)
Same thing but we are changing the x, y positions AND also adding β to the author username
ππ Final steps
quote = dailyquote()['contents']['quotes'][0]['quote']
author = dailyquote()['contents']['quotes'][0]['author']
tags = dailyquote()['contents']['quotes'][0]['tags']
dl_image()
text_overlay_ig(quote, author)
ptag = ''
for tag in tags:
ptag += "#" + tag + ' '
content = "This bot was made by ryan s.(@9ebd7134 on github), Hope you enjoyed this quote, This quote was by"+author+"\nLinks:\nπ·Image by: https://unsplash.com/user/eberhardgross\nTags:\n#οΈβ£ " + ptag;
bot = Bot()
bot.login(username="****", password="password1234")
bot.upload_photo("image.jpg", caption=content)
Simply change the caption/content and then bot.login username/password details for this to fully work!
π Fixing up some issues
I'll be updating this and updating the github page for it, i will make an issue and a pull request talking all about the changes! If you want to fix my code i'd love to see it!
Link for the github:
https://github.com/9ebd7134/Python-quote-of-the-day-instagram-bot
Recommend
-
162
Page not found Β· GitHub Β· GitHub Skip to content...
-
4
im quote of the day Nov 29, 2005 β’ {"login"=>"fuzzyblog", "email"=>"[email protected]", "display_name"=>"fuzzyblog", "first_name"=>"Scott", "last_name"=>"Johnson"} iwas dmumb Not o...
-
5
Introduction Hello there everyone, I gotta say I really like the Mandalorian TV series. It brings me back to my childhood and I like it so much in fact, that I decided to build a small little project. The project will...
-
8
Whoβs Running the Vincere Bot Network on Instagram?Thousands of fake Instagram accounts are powering scams targeting influencers. The scams are run by different people, but are the bots?...
-
16
InstaPy Tooling that automates your social media interactions to βfarmβ Likes, Comments, and Followers on Instagram Implemented in Python using the Selenium module.Twitter of Inst...
-
4
Quote of the Day is a simple protocol that is used to deliver daily quotes. Although its usage is almost nonexistent these days, there are still a few public servers. The protocol is defined by
-
4
Articles: Donβt quote. Make it yours and say it yourself. 2019-09-29 Which sounds better to you? βIn his best-selling book on behavioral science, Thinking, Fa...
-
3
Instagram now lets all users tag products in their posts The feature is rolling out to users in the US today By...
-
1
Instagram lets you create and donate to fundraisers from Reels
-
1
build your own instagram bot from scratch (or) set one up in like 10s for free a brief overview of this blog post: ...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK