ohld / igbot

🐙 Free scripts, bots and Python API wrapper. Get free followers with our auto like, auto follow and other scripts!
https://hikerapi.com/p/N2P6iqiM
Apache License 2.0
4.72k stars 1.47k forks source link

Randomize "repost best photos from users" #626

Closed brain-wash closed 6 years ago

brain-wash commented 6 years ago

Please follow the guide below


Before submitting an issue, make sure you have:

Purpose of your issue?

Describe your issue

I have a list of Instagram-Accounts and would like to repost their most popular pictures in a random order. Currently the bot picks one account from the list (the one with the most followers?) and only reposts pictures of this account.

What I am looking for is the bot picking a random account from the list, repost the most popular picture (which I haven't reposted before) and next time again pick a different/random account from this list etc. Did anybody try something similar or could implement this? Unfortunately my python knowledge is very limited.

kintaro1981 commented 6 years ago

If you read the example it already check if media is already posted.

To randomize you need use shuffle.

add this to repost_best_photos_from_users

after line 12 import random

after line 90 users = random.choice(users)

now the variable users contains only one random picked account from the file

brain-wash commented 6 years ago

@kintaro1981 Thank you for the response! I added those lines, unfortunately I get following error when I try to start the script:

TypeError: object of type 'NoneType' has no len()

kintaro1981 commented 6 years ago

same problem :D It's because it is expecting a list

try replacing all the code from row 90 to the end with this:

    users = random.choice(utils.file(args.file).list)
    user = []
    user.append(users)

repost_best_photos(bot, user, args.amount)

p.s. it works with username_database.txt

brain-wash commented 6 years ago

@kintaro1981 Hmm, it still is not working. Now there is a NameError: name 'user' is not defined

kintaro1981 commented 6 years ago

are you running it with -file username_database.txt?

brain-wash commented 6 years ago

At line 20 there is USERNAME_DATABASE = 'username_database.txt' and I added all of the account names in the username_database.txt file

kintaro1981 commented 6 years ago

I'm running like this:

python3 ./instabot/examples/repost_best_photos_from_random_user.py -file username_database.txt and username_database.txt is located in ./

I duplicated repost_best_photos_from_users.py creating from it repost_best_photos_from_random_user.py to maintain the original one

my repost_best_photos_from_random_user.py is like this:

"""
    instabot example

    Workflow:
    Repost best photos from users to your account
    By default bot checks username_database.txt
    The file should contain one username per line!
"""

import argparse
import os
import sys
import random

from tqdm import tqdm

sys.path.append(os.path.join(sys.path[0], '../'))
from instabot import Bot, utils

USERNAME_DATABASE = 'username_database.txt'
POSTED_MEDIAS = 'posted_medias.txt'

def repost_best_photos(bot, users, amount=1):
    medias = get_not_used_medias_from_users(bot, users)
    medias = sort_best_medias(bot, medias, amount)
    for media in tqdm(medias, desc='Reposting photos'):
        repost_photo(bot, media)

def sort_best_medias(bot, media_ids, amount=1):
    best_medias = [bot.get_media_info(media)[0] for media in tqdm(media_ids, desc='Getting media info')]
    best_medias = sorted(best_medias, key=lambda x: (x['like_count'], x['comment_count']), reverse=True)
    return [best_media['pk'] for best_media in best_medias[:amount]]

def get_not_used_medias_from_users(bot, users=None, users_path=USERNAME_DATABASE):
    if not users:
        users = utils.file(users_path).list
    users = map(str, users)
    total_medias = []
    for user in users:
        medias = bot.get_user_medias(user, filtration=False)
        medias = [media for media in medias if not exists_in_posted_medias(media)]
        total_medias.extend(medias)
    return total_medias

def exists_in_posted_medias(new_media_id, path=POSTED_MEDIAS):
    medias = utils.file(path).list
    return str(new_media_id) in medias

def update_posted_medias(new_media_id, path=POSTED_MEDIAS):
    medias = utils.file(path)
    medias.append(str(new_media_id))
    return True

def repost_photo(bot, new_media_id, path=POSTED_MEDIAS):
    if exists_in_posted_medias(new_media_id, path):
        bot.logger.warning("Media {0} was uploaded earlier".format(new_media_id))
        return False
    photo_path = bot.download_photo(new_media_id, save_description=True)
    if not photo_path or not isinstance(photo_path, str):
        # photo_path could be True, False, or a file path.
        return False
    with open(photo_path[:-3] + 'txt', 'r') as f:
        text = ''.join(f.readlines())
    if bot.upload_photo(photo_path, text):
        update_posted_medias(new_media_id, path)
        bot.logger.info('Media_id {0} is saved in {1}'.format(new_media_id, path))

parser = argparse.ArgumentParser(add_help=True)
parser.add_argument('-u', type=str, help="username")
parser.add_argument('-p', type=str, help="password")
parser.add_argument('-proxy', type=str, help="proxy")
parser.add_argument('-file', type=str, help="users filename")
parser.add_argument('-amount', type=int, help="amount", default=1)
parser.add_argument('users', type=str, nargs='*', help='users')
args = parser.parse_args()

bot = Bot()
bot.login()

users = None
if args.users:
    users = args.users
elif args.file:
    users = random.choice(utils.file(args.file).list)
    user = []
    user.append(users)
    print (user)

repost_best_photos(bot, user, args.amount)
brain-wash commented 6 years ago

Thanks! Now it seems to be working

brain-wash commented 6 years ago

@kintaro1981 I have one more question right now - I hope I do not annoy you :D When there are emojis in the caption, they get copied into a text file which then turns them into some weird characters. Is there a way to either remove the emojis before adding them to the text file, or even copy the emojis?

kintaro1981 commented 6 years ago

@brain-wash I'm on macOs and if I copy and past it maintains emoji (I'm using BBEdit/TextWrangler). I think that you need a different text editor with emojis support.

stale[bot] commented 6 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.