A simple way of randomizing tags from a given list.
session.py
from instapy import InstaPy
from random import randint
from random import seed
# Pass a list of tags and the amount of randomized tags you want to get back
# only unique tags are being passed back
def randomize_tags(tags, amount):
max = len(tags)-1
if amount > 1:
randomInts = []
for x in range(amount):
r = randint(0, max)
while r in randomInts:
r = randint(0, max)
randomInts.append(r)
randomTags = []
for x in randomInts:
randomTags.append(tags[x])
print("Random Tags: ", randomTags)
return randomTags
elif amount is 1:
randomTag = tags[randint(0, max)]
print("Random Tag: ", randomTag)
return randomTag
# List of all possible tags
tags = [
'#1',
'#2',
'#3',
'#4',
'#5'
]
# Enhance randomness
seed()
session = InstaPy(username='test', password='test', nogui=True)
session.login()
# Simply call & pass the return value of 'randomize_tags()' to InstaPys functions
session.like_by_tags(randomize_tags(tags, amount=3), amount=2)
session.end()
@citizenQ Interesting that you implemented this yourself.
You could also simply do:
from random import sample
# List of all possible tags
tags = [
'#1',
'#2',
'#3',
'#4',
'#5'
]
sample(tags, 3)
lol aight :'D, than here the elegant way with static tags addition if anyone else is interested in.
session.py
from instapy import InstaPy
from random import sample
randomTags = [
'#1',
'#2',
'#3'
]
staticTags = [
'#!!',
'#!!!'
]
tags = sample(randomTags, 2)
for x in staticTags:
tags.append(x)
print("Tags: ", tags)
session = InstaPy(username='test', password='test', nogui=True)
session.login()
session.like_by_tags(tags, amount=1)
session.end()
I've also added some tag/likes/follow randomization because I thought it would seem more natural to instagram and would decrease the chances of being caught. But I'm not a coder so my solution is not very elegant. It works, though.
import copy
from instapy import InstaPy
import random
amountLike = random.randint(50,60)
amountTags = random.randint(4,7)
amountLocations = random.randint(4,6)
percentFollow = random.randint(25,45)
tagsFile = open("data/tags.txt", "r", encoding="utf8")
tags = tagsFile.read().split(',')
tagsShuffled = copy.copy(tags)
random.shuffle(tagsShuffled)
tagsShuffled = tagsShuffled[:amountTags]
locationsFile = open("data/locations.txt", "r", encoding="utf8")
locations = locationsFile.read().split(',')
locationsShuffled = copy.copy(locations)
random.shuffle(locations)
locationsShuffled = locationsShuffled [:amountLocations]
So then i can use it like
InstaPy(username='', password='')\
.login()\
.set_do_follow(enabled=True, percentage=percentFollow, times=1)\
.like_by_tags(tagsShuffled, amount=amountLike, media='Photo')\
.like_by_locations([locationsShuffled], amount=amountLike)\
The random.sample
looks easier :) glad to learn about it.
@citizenQ @jedbanger why dont you open pull request to share youre solution with out all and contribute with these project?
to me it sounds great and useful to have everyone will like submit it please
you did a awesome job
I think before pulling we should do something to avoid redundancy.
Tags, Comments and Locations can be randomized, so lets write one function which can handle all of them. So it rather becomes a "randomize_content()". And the optional ability to random pick from a huge amount of Tags, Comments, Locations which are located in a .txt file while static content should always be represented as a python list, because the static content should have only a few entrys, in my opinion.
So I thought about something like this.
session.py - Located in /InstaPy :
from instapy import InstaPy
from random import sample
randomTags = [
'#1',
'#2',
'#3'
]
staticTags = [
'#!!',
'#!!!'
]
def randomize_content(content=None, staticContent=None, amount=2, contentFile=False):
if contentFile:
f = open("tags.txt", "r", encoding="utf8")
contentList = f.read().split('\n')
print(contentList)
randomContent = sample(contentList, amount)
print("Random content: ", randomContent)
if staticContent is not None:
print("Adding static content ..")
for x in staticContent:
randomContent.append(x)
print("Final content: ", randomContent)
return randomContent
elif content is not None:
print(content)
randomContent = sample(content, amount)
print("Random content: ", randomContent)
if staticContent is not None:
print("Adding static content ..")
for x in staticContent:
randomContent.append(x)
print("Final content: ", randomContent)
return randomContent
# the normal way
randomizedTags = randomize_content(content=randomTags, staticContent=staticTags, amount=3)
# with 'tags.txt' File
randomizedTags = randomize_content(staticContent=staticTags, amount=3, contentFile=True)
# or without static content
randomizedTags = randomize_content(content=randomTags, amount=3)
session = InstaPy(username='', password='', nogui=True)
session.login()
session.like_by_tags(randomizedTags, amount=1)
session.end()
tags.txt - Located in /InstaPy :
Most helpful comment
I've also added some tag/likes/follow randomization because I thought it would seem more natural to instagram and would decrease the chances of being caught. But I'm not a coder so my solution is not very elegant. It works, though.
So then i can use it like
The
random.sample
looks easier :) glad to learn about it.