-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathcheck_thread.py
More file actions
143 lines (114 loc) · 5.89 KB
/
Copy pathcheck_thread.py
File metadata and controls
143 lines (114 loc) · 5.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""Check thread with a specific user directly from Instagram"""
import sys
import os
# Fix Windows console encoding issues
if sys.platform == 'win32':
sys.stdout.reconfigure(encoding='utf-8', errors='replace')
from instagram_client import InstagramClient
def check_thread(username):
client = InstagramClient()
print(f"Logging in to Instagram...")
if not client.login():
print("Login failed!")
return
print(f"Fetching threads...")
threads = client.get_direct_threads(amount=500)
found = False
for thread in threads:
if not thread.users:
continue
thread_user = thread.users[0]
if thread_user.username.lower() == username.lower():
found = True
print(f"\n{'='*60}")
print(f"THREAD WITH: @{thread_user.username}")
print(f"{'='*60}")
if not thread.messages:
print("No messages in this thread")
break
print(f"\nLast {min(10, len(thread.messages))} messages (newest first):\n")
print(f"Your user_id: {client.client.user_id}")
print(f"Their user_id: {thread_user.pk}\n")
# Check if your last message was seen using thread-level last_seen_at
your_last_message = None
for msg in thread.messages:
if str(msg.user_id) == str(client.client.user_id):
your_last_message = msg
break
if your_last_message:
seen_by_them = False
# Check thread.last_seen_at for read receipts
if hasattr(thread, 'last_seen_at') and thread.last_seen_at:
their_user_id = str(thread_user.pk)
your_msg_timestamp = int(your_last_message.timestamp.timestamp() * 1000000)
# Check if they have a last_seen_at entry
if their_user_id in thread.last_seen_at:
their_last_seen = thread.last_seen_at[their_user_id]
# Handle both dict and object formats for timestamp
if isinstance(their_last_seen, dict) and 'timestamp' in their_last_seen:
their_seen_timestamp = int(their_last_seen['timestamp'])
elif hasattr(their_last_seen, 'timestamp'):
their_seen_timestamp = int(their_last_seen.timestamp.timestamp() * 1000000)
else:
their_seen_timestamp = 0
# If their last seen timestamp is >= your message timestamp, they've seen it
if their_seen_timestamp >= your_msg_timestamp:
seen_by_them = True
if seen_by_them:
print(f"[SEEN] YOUR LAST MESSAGE WAS SEEN\n")
else:
print(f"[NOT SEEN] YOUR LAST MESSAGE WAS NOT SEEN YET\n")
for idx, msg in enumerate(thread.messages[:10]):
# Check if message is from you by comparing user_id
is_from_you = str(msg.user_id) == str(client.client.user_id)
sender = "YOU" if is_from_you else f"@{thread_user.username}"
timestamp = msg.timestamp.strftime('%Y-%m-%d %H:%M:%S') if msg.timestamp else 'Unknown time'
text = msg.text if hasattr(msg, 'text') and msg.text else '[Media/Reaction]'
# Show read receipt for your messages using thread.last_seen_at
read_status = ""
if is_from_you and hasattr(thread, 'last_seen_at') and thread.last_seen_at:
their_user_id = str(thread_user.pk)
msg_timestamp = int(msg.timestamp.timestamp() * 1000000)
if their_user_id in thread.last_seen_at:
their_last_seen = thread.last_seen_at[their_user_id]
# Handle both dict and object formats for timestamp
if isinstance(their_last_seen, dict) and 'timestamp' in their_last_seen:
their_seen_timestamp = int(their_last_seen['timestamp'])
elif hasattr(their_last_seen, 'timestamp'):
their_seen_timestamp = int(their_last_seen.timestamp.timestamp() * 1000000)
else:
their_seen_timestamp = 0
if their_seen_timestamp >= msg_timestamp:
read_status = " [SEEN]"
print(f"{idx+1}. [{timestamp}] {sender}{read_status}:")
print(f" {text}")
print()
# Find last reply from them
their_last_message = None
for msg in thread.messages:
if str(msg.user_id) != str(client.client.user_id):
their_last_message = msg
break
if their_last_message:
print(f"\n{'='*60}")
print(f"THEIR LAST REPLY:")
print(f"{'='*60}")
timestamp = their_last_message.timestamp.strftime('%Y-%m-%d %H:%M:%S') if their_last_message.timestamp else 'Unknown'
text = their_last_message.text if hasattr(their_last_message, 'text') and their_last_message.text else '[Media/Reaction]'
print(f"Time: {timestamp}")
print(f"Message: {text}")
else:
print("\nThey haven't replied yet (all messages are from you)")
print(f"\n{'='*60}\n")
break
if not found:
print(f"\nNo thread found with @{username}")
print("Make sure you have an active DM conversation with this user.")
if __name__ == '__main__':
if len(sys.argv) < 2:
print("Usage: python check_thread.py username")
sys.exit(1)
username = sys.argv[1].replace('@', '')
check_thread(username)