Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
328 views
in Technique[技术] by (71.8m points)

TypeError: 'Job' object is not callable (Schedule package in Python)

I am coding some bot in python and thinking everything works I stumbled across a very strange error:

Traceback (most recent call last):
  File "/home/kali/PycharmProjects/instabotv4_TEMPLATE/main.py", line 104, in <module>
    schedule.every().hour().do(post)
TypeError: 'Job' object is not callable

I've never encountered this error while using schedule. When searching other questions
regarding "XXX object is not callable" It always made sense because the objects people tried to call were integers and lists. but when I ran print(type(post)) I got the following output:

<class 'function'>

I checked to see if a recent update was made to the package but last update was in 2019. Here is the source code:

from instagrapi import Client
from PIL import Image
from pathlib import Path
from time import sleep
import requests
import praw
import json
import schedule
import random
import os
import io

record = "post_ids.txt"
secret = "info.json"

# turn any picture into a square


def make_square(im, min_size=256, fill_color=(0, 0, 0, 0)):
    x, y = im.size
    size = max(min_size, x, y)
    new_im = Image.new('RGB', (size, size), fill_color)
    new_im.paste(im, (int((size - x) / 2), int((size - y) / 2)))
    return new_im


def square_and_save(name, image_link):
    image = requests.get(image_link)
    image_bytes = io.BytesIO(image.content)
    image_pil = Image.open(image_bytes)
    image_pil = make_square(image_pil)
    image_pil.save(name)

# reading secret information regarding instagram and reddit client


with open(secret) as json_file:
    info = json.load(json_file)
reddit_info = info["RedditInfo"]
instagram_info = info["InstagramInfo"]

reddit = praw.Reddit(
    client_id=reddit_info["client_id"],
    client_secret=reddit_info["client_secret"],
    user_agent=reddit_info["user_agent"],
    username=reddit_info["username"],
    password=reddit_info["password"]
)

subreddit = reddit.subreddit(reddit_info["subreddit"])
instagram_client = Client()
instagram_client.login(instagram_info["username"], instagram_info["password"])


def post():
    feed = subreddit.hot(limit=10)
    for submission in feed:
        if submission.is_video:
            continue
        if submission.pinned:
            continue
        if submission.over_18:
            continue
        submission_info = vars(submission)
        try:
            post_id = str(submission_info['id'])
            lines = []

            with open(record, 'r+') as history:
                for line in history:
                    lines.append(line.rstrip('
'))
                if post_id not in lines:
                    history.write(post_id)
                    history.write('
')

            if post_id in lines:
                continue

            title = str(submission_info['title'])
            author = str(submission_info['author'])
            image_link = str(submission_info['url'])
            permalink = str(submission_info['permalink'])
            name = post_id + '.jpg'

            square_and_save(name=name, image_link=image_link)

            try:
                tags = open("tags/" + random.choice(os.listdir("tags"))).read()
            except:
                tags = ""
            caption = '"{title}"
 posted by: {author} (on reddit)
 https://reddit.com{permalink} 
*
*
*
 {tags}'
                .format(title=title, author=author, permalink=permalink, tags=tags)
            instagram_client.photo_upload(path=Path(name), caption=caption)

            os.remove(Path(name))

            break
        except KeyError as e:
            print(e.args[0] + " key missing")


print(type(post))

schedule.every().hour().do(post)
while True:
    schedule.run_pending()
    sleep(5)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Don't call hour, do

schedule.every().hour.do(post)

Based on: https://github.com/dbader/schedule/issues/45#issuecomment-118030275.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...