This repository has been archived by the owner on Sep 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 82
/
geogramint.py
408 lines (351 loc) · 13.9 KB
/
geogramint.py
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
# v1.4
import os
import sys
import codecs
import trio
import shutil
import json
import pandas as pd
if len(sys.argv) < 2:
from kivy.uix.image import AsyncImage
from api import TelegramAPIRequests
from threading import Thread
from kivy.uix.boxlayout import BoxLayout
from kivymd.uix.button import MDFlatButton, MDRaisedButton, MDIconButton
from kivymd.uix.dialog import MDDialog
from kivymd.uix.textfield import MDTextField
from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.clock import Clock
from kivy.config import Config
from kivymd.app import MDApp
from kivy.core.window import Window
from kivy.logger import Logger
from api.TelegramAPIRequests import geolocate_AllEntities_Nearby
from mapfiles.markercenter import MarkerHelper
from utils import settings
from utils import resultDisplay
from plyer import filechooser
from utils import ressources
lat = None
lon = None
loop = None
users = None
groups = None
enabled = False
searchStarted = False
api_id = None
api_hash = None
phone_number = None
error = False
export_report = None
export_osintracker = None
extended_report = None
timestamp = None
real_lat, real_lon = None, None
Loading = AsyncImage(
pos_hint={'bottom': 1, 'right': 1},
size_hint={0.1, None},
source='appfiles/orange_loading.gif',
anim_delay=0.1,
anim_loop=0
)
success_anim = AsyncImage(
pos_hint={'bottom': 1, 'right': 1},
size_hint={0.1, None},
source='appfiles/ok_anim.gif',
anim_delay=0.1,
anim_loop=1
)
error_anim = AsyncImage(
pos_hint={'bottom': 1, 'right': 1},
size_hint={0.1, None},
source='appfiles/error_anim.gif',
anim_delay=0.1,
anim_loop=1
)
else:
from CLI import geogramint_cli
def telegramAPICall():
global lat, lon, users, groups, enabled, searchStarted, error, timestamp, real_lat, real_lon
try:
real_lat, real_lon = lat, lon
users, groups, timestamp = geolocate_AllEntities_Nearby(api_id, api_hash, phone_number, float(lat), float(lon))
except Exception as e:
error = True
Logger.info(f"Geogramint Search: Nothing Found")
searchStarted = False
return
enabled = True
json_string_user = json.dumps([ob.__dict__() for ob in users], ensure_ascii=False)
with codecs.open('cache_telegram/users.json', 'w', 'utf-8') as f:
f.write(json_string_user)
json_string_group = json.dumps([ob.__dict__() for ob in groups], ensure_ascii=False)
with codecs.open('cache_telegram/groups.json', 'w', 'utf-8') as f:
f.write(json_string_group)
df = pd.read_json('cache_telegram/users.json')
df.to_csv('cache_telegram/users.csv', index=None)
df = pd.read_json('cache_telegram/groups.json')
df.to_csv('cache_telegram/groups.csv', index=None)
def startSearch(dt):
global lat, lon, users, searchStarted, Loading
Logger.info(f"Geogramint Search: Search Started at : {lat}, {lon}")
if users is None and searchStarted == False \
and api_id is not None and api_hash is not None and phone_number is not None:
searchStarted = True
Logger.info(f"Geogramint Search: Searching ...")
App.get_running_app().root.ids.mapzone.add_widget(Loading)
try:
t = Thread(target=telegramAPICall)
t.start()
except:
Logger.info(f"Geogramint Search: Nothing Found")
App.get_running_app().root.ids.mapzone.add_widget(error_anim)
Clock.schedule_once(remove_erroranim, error_anim.anim_delay * 50)
searchStarted = False
def telegramCodeDialog():
"""
verification code pop-up
"""
content = BoxLayout(
orientation="vertical",
spacing="10dp",
size_hint_y=None,
height="100dp",
)
ok = MDFlatButton(
text="Ok",
theme_text_color="Custom",
)
code_input = MDTextField(
hint_text="Code:",
helper_text_mode="on_error",
helper_text="Wrong format"
)
password_input = MDTextField(
hint_text="Password Two-Step Verification (optional):",
helper_text_mode="on_error",
helper_text="Wrong format"
)
content.add_widget(code_input)
content.add_widget(password_input)
dialog = MDDialog(
title="Telegram's verification code",
type="custom",
content_cls=content,
auto_dismiss=False,
buttons=[
ok,
],
)
def submit(dt):
if not code_input.text.isnumeric():
code_input.error = True
return
if password_input.text != "":
TelegramAPIRequests.password = password_input.text
TelegramAPIRequests.code = code_input.text
dialog.dismiss()
def debug(dt):
print(TelegramAPIRequests.code)
ok.bind(on_press=submit)
dialog.bind(on_dismiss=debug)
return dialog
def remove_successanim(dt):
App.get_running_app().root.ids.mapzone.remove_widget(success_anim)
def remove_erroranim(dt):
App.get_running_app().root.ids.mapzone.remove_widget(error_anim)
def export_pdf_report(dt):
global timestamp, users, groups, extended_report, real_lat, real_lon
current_dir = os.getcwd()
_path = filechooser.save_file(title="Export a Geogramint Report :",
filters=[("Geogramint Report", "*.pdf")])
os.chdir(current_dir)
if len(_path) == 0 or _path[0] == "":
return
path = _path[0]
if os.path.isdir(path):
if path[-1] != '/':
path += 'Report_' + str(lat) + ',' + str(lon) + '.pdf'
else:
path += '/Report_' + str(lat) + ',' + str(lon) + '.pdf'
try:
t = Thread(target=ressources.generate_pdf_report, args=(users, groups, real_lat, real_lon, timestamp, path,
extended_report))
t.start()
except:
Logger.info("Geogramint report: an error has occured during report creation")
elif not os.path.isfile(path):
if path[len(path) - 4:] != '.pdf':
path += '.pdf'
try:
t = Thread(target=ressources.generate_pdf_report, args=(users, groups, real_lat, real_lon, timestamp, path,
extended_report))
t.start()
except:
Logger.info("Geogramint report: an error has occured during report creation")
else:
return
def export_osintracker_investigation(dt):
global timestamp, users, groups, extended_report, real_lat, real_lon
current_dir = os.getcwd()
_path = filechooser.save_file(title="Export Geogramint search data to Osintracker Investigation :",
filters=[("Osintracker Export", "*.json")])
os.chdir(current_dir)
if len(_path) == 0 or _path[0] == "":
return
path = _path[0]
if os.path.isdir(path):
if path[-1] != '/':
path += 'Geogramint_' + str(lat) + ',' + str(lon) + '.json'
else:
path += '/Geogramint_' + str(lat) + ',' + str(lon) + '.json'
try:
t = Thread(target=ressources.generate_osintracker_investigation, args=(users, groups, real_lat, real_lon, path, extended_report))
t.start()
except:
Logger.info("Osintracker Export: an error has occured during investigation creation")
elif not os.path.isfile(path):
if path[len(path) - 12:] != '.json':
path += '.json'
try:
t = Thread(target=ressources.generate_osintracker_investigation, args=(users, groups, real_lat, real_lon, path, extended_report))
t.start()
except:
Logger.info("Osintracker Export: an error has occured during investigation creation")
else:
return
def background_loop(dt):
"""
Main background loop of this app
"""
global lat, lon, users, enabled, Loading, error, export_report, export_osintracker
lat = App.get_running_app().root.ids.mapview.ids.mark.lat
lon = App.get_running_app().root.ids.mapview.ids.mark.lon
if users is not None and enabled:
App.get_running_app().root.ids.mapzone.remove_widget(Loading)
App.get_running_app().root.ids.mapzone.add_widget(success_anim)
export_report = MDRaisedButton(
id="export",
text="[b]Export PDF[/b]",
md_bg_color=(1, 0.52, 0, 0.9),
pos_hint={'center_y': 0.05, 'center_x': 0.15}
)
export_osintracker = MDIconButton(
id="osintracker",
text=" ",
pos_hint={'center_y': 0.05, 'center_x': 0.3},
icon='appfiles/osintracker.png',
icon_size="35sp"
)
export_report.bind(on_press=export_pdf_report)
export_osintracker.bind(on_press=export_osintracker_investigation)
App.get_running_app().root.ids.mapzone.add_widget(export_report)
App.get_running_app().root.ids.mapzone.add_widget(export_osintracker)
Clock.schedule_once(remove_successanim, success_anim.anim_delay * 100)
for user in users:
name = ""
if user.firstname is not None:
name += user.firstname + " "
if user.lastname is not None:
name += user.lastname
resultDisplay.UserList().add_elm(user.id, name, user.username, user.distance)
groups.sort(key=lambda x: x.distance, reverse=False)
for group in groups:
resultDisplay.GroupList().add_elm(group.id, group.name, group.distance)
resultDisplay.GroupList().add_empty()
resultDisplay.GroupList().add_empty()
resultDisplay.UserList().add_empty()
resultDisplay.UserList().add_empty()
enabled = False
if TelegramAPIRequests.code_dialog is True and TelegramAPIRequests.connected is False:
TelegramAPIRequests.code = None
telegramCodeDialog().open()
TelegramAPIRequests.code_dialog = False
if not searchStarted and Loading is not None:
App.get_running_app().root.ids.mapzone.remove_widget(Loading)
if error:
App.get_running_app().root.ids.mapzone.add_widget(error_anim)
Clock.schedule_once(remove_erroranim, error_anim.anim_delay * 50)
error = False
def search_location(dt):
str = App.get_running_app().root.ids.searchInput.text
if str.count(' ') < 1 and str.count(',') < 1:
App.get_running_app().root.ids.searchInput.error = True
return
try:
lat, long = str.split(',')
if lat.count('.') > 1 or long.count('.') > 1:
App.get_running_app().root.ids.searchInput.error = True
return
numlat, numlong = float(lat), float(long)
App.get_running_app().root.ids.mapview.center_on(numlat, numlong)
App.get_running_app().root.ids.searchInput.error = False
except:
try:
lat, long = str.split(' ')
if lat.count('.') > 1 or long.count('.') > 1:
App.get_running_app().root.ids.searchInput.error = True
return
numlat, numlong = float(lat), float(long)
App.get_running_app().root.ids.mapview.center_on(numlat, numlong)
App.get_running_app().root.ids.searchInput.error = False
except:
App.get_running_app().root.ids.searchInput.error = True
return
def reload_settings(dt):
global api_id, api_hash, phone_number, extended_report
api_id, api_hash, phone_number, extended_report = settings.loadConfig()
def settings_menu(dt):
global api_id, api_hash, phone_number
dialog = settings.settings_dialog(str(api_id), api_hash, phone_number, extended_report)
dialog.bind(on_dismiss=reload_settings)
dialog.open()
def reset(dt):
global users, groups, searchStarted, export_report, export_osintracker
shutil.rmtree("cache_telegram", ignore_errors=True)
resultDisplay.UserList().clear_all()
resultDisplay.GroupList().clear_all()
users = None
groups = None
searchStarted = False
if export_report:
App.get_running_app().root.ids.mapzone.remove_widget(export_report)
App.get_running_app().root.ids.mapzone.remove_widget(export_osintracker)
if len(sys.argv) < 2:
class Geogramint(MDApp):
def build(self):
global api_id, api_hash, phone_number, extended_report
Window.size = (1100, 600)
api_id, api_hash, phone_number, extended_report = settings.loadConfig()
def on_start(self):
global loop, export_report
self.window = GridLayout()
self.icon = "appfiles/Geogramint.png"
Config.set('input', 'mouse', 'mouse,disable_multitouch')
# display marker in the middle of the map
MarkerHelper().run()
# Background loop
loop = Clock.schedule_interval(background_loop, 1 / 30.)
# Buttons Bindings
App.get_running_app().root.ids.Start.bind(on_press=startSearch)
App.get_running_app().root.ids.Reset.bind(on_press=reset)
App.get_running_app().root.ids.searchInput.bind(on_text_validate=search_location)
App.get_running_app().root.ids.settings.bind(on_press=settings_menu)
def on_stop(self):
shutil.rmtree("cache", ignore_errors=True) # deleting cache created by Mapview
Clock.unschedule(loop)
if __name__ == "__main__":
shutil.rmtree("cache_telegram", ignore_errors=True)
shutil.rmtree("cache", ignore_errors=True)
if len(sys.argv) < 2:
try:
trio.run(Geogramint().run())
except TypeError:
shutil.rmtree("cache", ignore_errors=True)
else:
geogramint_cli.CLI()
shutil.rmtree("cache", ignore_errors=True)
if os.path.exists("geckodriver.log"):
os.remove("geckodriver.log")
print()