오늘의 명언 인스타그램 봇을 만들어보자!

시작하자



🎉 데모





파일 만들기:main.py

코드 시작


🧪 라이브러리 가져오기




import urllib.request
import os
from PIL import Image, ImageDraw, ImageFont, ImageOps
import requests
from instabot import Bot

📷 이미지 가져오기 및 저장



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!


🙌 일일 견적 받기



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!


😎 명언과 이미지를 한번에 담기!!


🐱‍🚀 라인 순서 수정



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


🎨 다 하자!




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



모든 것을 입을 시간입니다!

    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")




🌁 font.tff 파일 추가!

다운로드를 클릭하고 폴더에 넣어 사용하는 것이 좋습니다! 폰트가 가독성이 좋아서 완벽합니다

그리고 이제 이미지에 인용구를 붙일 수 있습니다.

    (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는 우리가 넣고자 하는 텍스트의 위치입니다. 이것을 변경하되 텍스트 길이에 따라 변경되지 않도록 주의하십시오! 우리는 그렇게 할 수 있지만 지금은 이것을 고수합시다

다른 모든 것은 이미지의 텍스트 그림자 옆에 텍스트를 배치하는 것입니다.

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)

동일하지만 x, y 위치를 변경하고 작성자 사용자 이름에 —를 추가합니다.

😎🙌 마지막 단계




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)

캡션/콘텐츠를 변경한 다음 bot.login 사용자 이름/암호 세부 정보를 변경하기만 하면 완전히 작동합니다!

🆘 일부 문제 수정



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!



깃허브 링크:

좋은 웹페이지 즐겨찾기