日Instagramボットの引用をしましょう!


レッツゲット


🎉 デモ



ファイルを作る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")

🌁 フォントの追加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)
単にキャプション/コンテンツを変更し、ボット.ログインユーザー名/パスワードの詳細は完全に動作するように!

🆘 問題の修正


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!


githubのリンク: