joaquinlpereyra / twitterImgBot

Ever wanted to randomly tweet pictures from a folder? No? Well, maybe you do now.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

allow_repeat_after and tweet_post_number not working

opened this issue · comments

I've been having allow_repeat_after to 144, running every 30 min so should be not reposting the same image after 3 days but it still does.

Also I've had the tweet_post_number to False and tweet_this_text to nothing but when it tweets it would still do "No. " + str(post_number) + " " + raw_text

Will look into this. Noticed allow_repeat_after not working on my own bot today. I'm planning on moving the stupid log file which never caused anything else but problems to a proper table on a database, that should simplify things.

I'm currently with a lot of work and working on another project too (joaquinlpereyra/ludema), so I don't promise any quick fix.

I honestly wouldn't care how long the fix would take. I currently have mine off till i get atleast more than 500 pics and the chance for it to repost is smaller then.

Also something else, I've heard you can put the bot on heroku and let it run from there but i've been trying to get it on there for the past 3 days without success. What do you use to host your bot?

@HuskerDoggo Where have you heard that? I've never tried anything fancier than cron on my desktop computer (that's how mine has always run), but I'll be glad to hear you configured it successfully on Heroku. I don't see why it wouldn't work. What issues are you facing?

@joaquinlpereyra i read that here https://www.reddit.com/r/learnprogramming/comments/27af2e/hosting_a_twitter_bot/. (it doesnt say anything about image bot but its worth the try)
At first my problem with it that it wouldn't detect the buildpack but i somehow fixed that. now my problems are something to do with a procfile and and requirements.txt.
I have 1 more thing i could try, then i'm probably gonna contact other people for help. if that doesnt help im gonna look into getting a vps
chrome_2016-11-01_20-04-57

@joaquinlpereyra i gave up on putting it on heroku cause it seems like it doesn't work. it also says it installs python 2.7.12 and it doesnt work on that.

luckily i am able to use microsoft azure for free but i have not yet figured out how it works.

@HuskerDoggo The bot will only work with Python3. I'm sorry I can't provide any more info about this, as I said, I've always just put it on my cron and that's it :)

I came up with a fix for the image repeating bug. its nothing professional or anything but at least it works.

I created a new file in /logs called "mediaLog" that only contains image paths. It is just so i can easier check if media was already tweeted.
In /logs/logger.py I added a little function for the log_line in the mediaLog that is just basically like the original log_line function:

def log_line2(img_path):
    """Returns a string fit for the log from a img_path.
    """
    log_line2 = img_path  + '\n'
    return log_line2

The mediaLog fileis also added to the config.py & settings file just like the original log file:

media_log = app_config['media_log']

media_log = logs/mediaLog

And as last i modified the main script to check if it was already tweeted in itself rather than in status.py
(I dont have the if banned since I made this to my situation and i dont use the banned functions)

def handle_tweet_posting(text, reply_id, test=False):
    """Sends a tweet to twitter, making sure it is not repeated and logs
    it to our log file. If no non repeated or non banned images found, return
    False. If operation was succesful, return True.

    text = text to be tweeted,
    reply_id = the id of the tweet we'll be replying to,
    test = if bot was executed with test flag or not.
    """

    log = config.log_file
    tolerance = config.tolerance
    banned_list = config.banned_file

    #log that only contains path of media
    mediaLog = config.media_log


    #get image and amount_media_available
    media, amount_media_available = get_random_image_from_folder(config.source_folder)


    #check if image was tweeted. returns True if image was already tweeted
    check_if_image_was_tweeted(media)


    #if check_if_image_was_tweeted(media) returned true: grab new media
    #and check again till returned false
    while check_if_image_was_tweeted(media):
    #get new media
        media, amount_media_available = get_random_image_from_folder(config.source_folder)
        check_if_image_was_tweeted(media)
        if tolerance >= amount_media_available:
        return False

    #!important! that this is behind the while loop or else it will still repeat the image and post
    #a diffirent image path to the log
    t = status.Tweet(media, text, reply_id)

    if not test:
        tweet_id = t.post_to_twitter(api)
        log_line = logger.log_line(post_number, tweet_id, media, reply_id)

        #post media to log if it wasn't a test
        log_line2 = logger.log_line2(media)
        logger.add_line_to_log(log_line2, mediaLog)

    else:
        # if it was a test, don't post it and mark the log line as such
        log_line = logger.log_line(post_number, 'TEST_ID', "TEST_PATH", reply_id)

    logger.add_line_to_log(log_line, log)
    print(media)
    return True


def check_if_image_was_tweeted(media):
    """Returns True if the tweet was already tweeted withing the last
    tolerance tweets and False if log_file isn't a file or if it wasn't
    tweeted.
    """
    mediaLog = config.media_log
    tolerance = config.tolerance
    log_line2 = logger.log_line2(media) #no idea why this line is here but too lazy to delete and test

    readlineAmount = -1*(tolerance)
    if not os.path.isfile(mediaLog):
        # if the file doesn't exist just don't bother
        return False

    try:
        already_tweeted = open(mediaLog, 'r').readlines()[readlineAmount:]
    except IndexError:
        print("Index Error")
        already_tweeted = open(mediaLog, 'r').readlines()
    for line in already_tweeted:
        if line.split()[0] == media:
            return True

    return False

For some reason there is also a line "tolerance = 0" in your main script that needs to be removed
twitterbot.zip