Skip to content

Commit

Permalink
feat: added new wcommented command
Browse files Browse the repository at this point in the history
  • Loading branch information
Datalux committed Aug 7, 2020
1 parent b60a9c5 commit 8113d3f
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 1 deletion.
4 changes: 4 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ def cmdlist():
print("Get list of users tagged by target")
pc.printout("target\t\t")
print("Set new target")
pc.printout("wcommented\t\t")
print("Get a list of user who commented target's photos")


printlogo()
Expand Down Expand Up @@ -112,6 +114,8 @@ def cmdlist():
api.get_people_tagged_by_user()
elif cmd == "target":
api.change_target()
elif cmd == "wcommented":
api.get_people_who_commented()
elif cmd == "FILE=y":
api.set_write_file(True)
elif cmd == "FILE=n":
Expand Down
74 changes: 73 additions & 1 deletion src/Osintgram.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,20 @@ def __get_feed__(self):

return data

def __get_comments__(self, media_id):
comments = []

result = self.api.media_comments(str(media_id))
comments.extend(result.get('comments', []))

next_max_id = result.get('next_max_id')
while next_max_id:
results = self.api.media_comments(str(media_id), max_id=next_max_id)
comments.extend(results.get('comments', []))
next_max_id = results.get('next_max_id')

return comments

def __printTargetBanner__(self):
pc.printout("\nLogged as ", pc.GREEN)
pc.printout(self.api.username, pc.CYAN)
Expand Down Expand Up @@ -417,7 +431,6 @@ def get_hashtags(self):
else:
pc.printout("Sorry! No results found :-(\n", pc.RED)


def get_user_info(self):
try:
content = urllib.request.urlopen("https://www.instagram.com/" + str(self.target) + "/?__a=1")
Expand Down Expand Up @@ -543,6 +556,65 @@ def get_media_type(self):
else:
pc.printout("Sorry! No results found :-(\n", pc.RED)

def get_people_who_commented(self):
if self.is_private:
pc.printout("Impossible to execute command: user has private profile\n", pc.RED)
return

pc.printout("Searching for users who commented...\n")

data = self.__get_feed__()
users = []

for post in data:
comments = self.__get_comments__(post['id'])
for comment in comments:
if not any(u['id'] == comment['user']['pk'] for u in users):
user = {
'id': comment['user']['pk'],
'username': comment['user']['username'],
'full_name': comment['user']['full_name'],
'counter': 1
}
users.append(user)
else:
for user in users:
if user['id'] == comment['user']['pk']:
user['counter'] += 1
break

if len(users) > 0:
ssort = sorted(users, key=lambda value: value['counter'], reverse=True)

json_data = {}

t = PrettyTable()

t.field_names = ['Comments', 'ID', 'Username', 'Full Name']
t.align["Comments"] = "l"
t.align["ID"] = "l"
t.align["Username"] = "l"
t.align["Full Name"] = "l"

for u in ssort:
t.add_row([str(u['counter']), u['id'], u['username'], u['full_name']])

print(t)

if self.writeFile:
file_name = "output/" + self.target + "_users_who_commented.txt"
file = open(file_name, "w")
file.write(str(t))
file.close()

if self.jsonDump:
json_data['users_who_commented'] = ssort
json_file_name = "output/" + self.target + "_users_who_commented.json"
with open(json_file_name, 'w') as f:
json.dump(json_data, f)
else:
pc.printout("Sorry! No results found :-(\n", pc.RED)

def get_photo_description(self):
if self.is_private:
pc.printout("Impossible to execute command: user has private profile\n", pc.RED)
Expand Down

0 comments on commit 8113d3f

Please sign in to comment.