■ ■ ■ ■ ■ ■
src/commands/whocommented/run.py
| 1 | + | import yaml |
| 2 | + | import readline |
| 3 | + | import signal |
| 4 | + | from prettytable import PrettyTable |
| 5 | + | from src import printcolors as pc |
| 6 | + | from src import utils as utils |
| 7 | + | import json |
| 8 | + | import sys |
| 9 | + | import time |
| 10 | + | |
| 11 | + | from src.CommandFather import CommandFather |
| 12 | + | |
| 13 | + | |
| 14 | + | class Command(CommandFather): |
| 15 | + | |
| 16 | + | def __init__(self, status, api): |
| 17 | + | super().__init__(status) |
| 18 | + | self.status = status |
| 19 | + | self.osintgram = api |
| 20 | + | |
| 21 | + | def __get_comments__(self, media_id): |
| 22 | + | comments = [] |
| 23 | + | |
| 24 | + | result = self.osintgram.get_api().media_comments(str(media_id)) |
| 25 | + | comments.extend(result.get('comments', [])) |
| 26 | + | |
| 27 | + | next_max_id = result.get('next_max_id') |
| 28 | + | while next_max_id: |
| 29 | + | results = self.osintgram.get_api().media_comments(str(media_id), max_id=next_max_id) |
| 30 | + | comments.extend(results.get('comments', [])) |
| 31 | + | next_max_id = results.get('next_max_id') |
| 32 | + | |
| 33 | + | return comments |
| 34 | + | |
| 35 | + | def run(self): |
| 36 | + | |
| 37 | + | if self.osintgram.check_private_profile(): |
| 38 | + | return |
| 39 | + | |
| 40 | + | pc.printout("Searching for users who commented...\n") |
| 41 | + | |
| 42 | + | posts = [] |
| 43 | + | rank_token = self.osintgram.generate_uuid() |
| 44 | + | data = self.osintgram.get_api().user_feed(str(self.osintgram.target_id), rank_token=rank_token) |
| 45 | + | posts.extend(data.get('items', [])) |
| 46 | + | |
| 47 | + | next_max_id = data.get('next_max_id') |
| 48 | + | while next_max_id: |
| 49 | + | if(int(super().get_option('output_limit')) > 0 and len(posts) > int(super().get_option('output_limit'))): |
| 50 | + | break |
| 51 | + | |
| 52 | + | if(int(super().get_option('delay')) > 0): |
| 53 | + | time.sleep(int(super().get_option('delay'))) |
| 54 | + | |
| 55 | + | sys.stdout.write("\rCatched %i posts" % len(posts)) |
| 56 | + | sys.stdout.flush() |
| 57 | + | results = self.osintgram.get_api().user_feed(str(self.osintgram.target_id), max_id=next_max_id, rank_token=rank_token) |
| 58 | + | posts.extend(results.get('items', [])) |
| 59 | + | next_max_id = results.get('next_max_id') |
| 60 | + | sys.stdout.write("\rCatched %i posts\n" % len(posts)) |
| 61 | + | sys.stdout.flush() |
| 62 | + | |
| 63 | + | users = [] |
| 64 | + | |
| 65 | + | for post in posts: |
| 66 | + | if(int(super().get_option('output_limit')) > 0 and len(users) > int(super().get_option('output_limit'))): |
| 67 | + | break |
| 68 | + | |
| 69 | + | comments = self.__get_comments__(post['id']) |
| 70 | + | for comment in comments: |
| 71 | + | if not any(u['id'] == comment['user']['pk'] for u in users): |
| 72 | + | user = { |
| 73 | + | 'id': comment['user']['pk'], |
| 74 | + | 'username': comment['user']['username'], |
| 75 | + | 'full_name': comment['user']['full_name'], |
| 76 | + | 'counter': 1 |
| 77 | + | } |
| 78 | + | users.append(user) |
| 79 | + | else: |
| 80 | + | for user in users: |
| 81 | + | if user['id'] == comment['user']['pk']: |
| 82 | + | user['counter'] += 1 |
| 83 | + | break |
| 84 | + | |
| 85 | + | if len(users) > 0: |
| 86 | + | ssort = sorted(users, key=lambda value: value['counter'], reverse=True) |
| 87 | + | self.status.print_output(ssort, ['Comments', 'ID', 'Username', 'Full Name'], ['counter', 'id', 'username', 'full_name']) |
| 88 | + | else: |
| 89 | + | pc.printout("Sorry! No results found :-(\n", pc.RED) |
| 90 | + | |
| 91 | + | |