Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: wcommented command #27

Merged
merged 2 commits into from
Aug 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
130 changes: 103 additions & 27 deletions 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 @@ -379,40 +393,43 @@ def get_hashtags(self):
hashtags.append(s.encode('UTF-8'))
counter += 1

hashtag_counter = {}

for i in hashtags:
if i in hashtag_counter:
hashtag_counter[i] += 1
else:
hashtag_counter[i] = 1
if len(hashtags) > 0:
hashtag_counter = {}

ssort = sorted(hashtag_counter.items(), key=lambda value: value[1], reverse=True)
for i in hashtags:
if i in hashtag_counter:
hashtag_counter[i] += 1
else:
hashtag_counter[i] = 1

file = None
json_data = {}
hashtags_list = []
ssort = sorted(hashtag_counter.items(), key=lambda value: value[1], reverse=True)

if self.writeFile:
file_name = "output/" + self.target + "_hashtags.txt"
file = open(file_name, "w")
file = None
json_data = {}
hashtags_list = []

for k, v in ssort:
hashtag = str(k.decode('utf-8'))
print(str(v) + ". " + hashtag)
if self.writeFile:
file.write(str(v) + ". " + hashtag + "\n")
if self.jsonDump:
hashtags_list.append(hashtag)
file_name = "output/" + self.target + "_hashtags.txt"
file = open(file_name, "w")

if file is not None:
file.close()
for k, v in ssort:
hashtag = str(k.decode('utf-8'))
print(str(v) + ". " + hashtag)
if self.writeFile:
file.write(str(v) + ". " + hashtag + "\n")
if self.jsonDump:
hashtags_list.append(hashtag)

if self.jsonDump:
json_data['hashtags'] = hashtags_list
json_file_name = "output/" + self.target + "_hashtags.json"
with open(json_file_name, 'w') as f:
json.dump(json_data, f)
if file is not None:
file.close()

if self.jsonDump:
json_data['hashtags'] = hashtags_list
json_file_name = "output/" + self.target + "_hashtags.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_user_info(self):
try:
Expand Down Expand Up @@ -539,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