Repository navigation
Expand file tree
/
Copy pathmain.py
More file actions
1411 lines (1265 loc) · 64.9 KB
/
Copy pathmain.py
File metadata and controls
1411 lines (1265 loc) · 64.9 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
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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import os
import time
import random
import sys
import json
import re
import datetime
import subprocess
from pathlib import Path
import threading
# Importy Appium i Selenium
from appium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException, StaleElementReferenceException, NoSuchDriverException
from appium.options.android import UiAutomator2Options
from colorama import init, Fore, Style
init(autoreset=True)
# GLOBALNE
global_last_index = 0
global_followers_start_wait = None
last_action_time = time.time() # Kiedy ostatnio bot coś faktycznie zrobił
stop_bot = False # Flaga przerwania sesji (poza dozwolonym zakresem)
downtime_start = None # Czas rozpoczęcia przerwy
bot_mode = None # "followers" lub "waiting"
last_wait_mode = None # "feed" albo "reels" – jeśli bot czeka między sesjami
last_wait_remaining = 0 # Pozostały czas oczekiwania, jeśli był tryb "waiting"
# Dla zachowania stanu sesji – te zmienne nie są resetowane przy restarcie
session_start_time = None
current_target = None
current_hour_followed = 0
# Flaga informująca, że program wykonuje "wymuszony sleep" – wówczas monitor nie liczy czasu oczekiwania
forced_sleep = False
appium_error_time = None
# Definicja własnego wyjątku
class RateLimitException(Exception):
pass
# ==================== KONFIGURACJA STRINGOWA ====================
BASE_DIR = os.path.join(os.getcwd(), "osobne_konta_insta")
# Pliki użytkownika będą tworzone tylko wewnątrz folderu użytkownika – nie tworzymy zbędnych folderów
CONFIG_FILE = "config.json"
# ------------------- SELEKTORY DLA NATYWNEJ APLIKACJI -------------------
MOBILE_LOGIN_USERNAME_SELECTOR = "//*[@resource-id='com.instagram.android:id/login_username']"
MOBILE_LOGIN_PASSWORD_SELECTOR = "//*[@resource-id='com.instagram.android:id/login_password']"
MOBILE_LOGIN_BUTTON_SELECTOR = "//*[@resource-id='com.instagram.android:id/button_login']"
MOBILE_NOT_NOW_BUTTON_SELECTOR = "//*[@text='Not Now' or @text='Nie teraz']"
INSTAGRAM_DEEP_LINK_PROFILE = "instagram://user?username={}"
MOBILE_FOLLOWERS_BUTTON_SELECTOR = "//*[@text='followers' or @text='Obserwujący']"
MOBILE_FOLLOWERS_LIST_XPATH = "//*[@resource-id='com.instagram.android:id/unified_follow_list_view_pager']"
MOBILE_FOLLOWER_ROW_XPATH = "//*[@resource-id='com.instagram.android:id/follow_list_container']"
MOBILE_FOLLOW_BUTTON_ROW_XPATH = "//*[@resource-id='com.instagram.android:id/follow_list_row_large_follow_button']"
MOBILE_PROFILE_FOLLOW_BUTTON_SELECTOR = "//*[@resource-id='com.instagram.android:id/profile_header_follow_button']"
MOBILE_BACK_BUTTON_SELECTOR = "//*[@resource-id='com.instagram.android:id/action_bar_button_back']"
MOBILE_PROFILE_POST_BUTTON_SELECTOR = "//*[@resource-id='com.instagram.android:id/image_button']"
MOBILE_PROFILE_LIKE_BUTTON_SELECTOR = "//*[@resource-id='com.instagram.android:id/row_feed_button_like']"
MOBILE_REELS_TAB_SELECTOR = "//*[@resource-id='com.instagram.android:id/clips_tab' and contains(@class, 'FrameLayout')]"
MOBILE_REELS_LIKE_BUTTON_SELECTOR = "//*[@resource-id='com.instagram.android:id/like_button' and contains(@class, 'android.widget.ImageView')]"
SEARCH_TAB_SELECTOR = "//*[@resource-id='com.instagram.android:id/search_tab' and contains(@class, 'FrameLayout')]"
SEARCH_FIELD_SELECTOR = "//*[@resource-id='com.instagram.android:id/action_bar_search_edit_text' and contains(@class, 'EditText')]"
FIRST_RESULT_SELECTOR = "//*[@resource-id='com.instagram.android:id/row_search_user_info_container' and contains(@class, 'LinearLayout')]"
SPAN_USERNAME_XPATH = ".//android.widget.TextView"
SUGGESTED_TEXT = "Suggested for you"
# ==================== KONFIGURACJA LICZBOWA I CZASOWA ====================
def load_config():
default_config = {
"STAY_LOGGED_IN": {"value": True, "comment": "Czy pozostac zalogowanym"},
"USERNAME": {"value": "", "comment": "Nazwa użytkownika na Instagram"},
"PASSWORD": {"value": "", "comment": "Hasło do konta"},
"HEADLESS": {"value": False, "comment": "Tryb headless (nie dotyczy mobilnej)"},
"MAX_TO_FOLLOW": {"value": 1000, "comment": "Maksymalna liczba kont do followowania"},
"BATCH_SIZE_MIN": {"value": 1, "comment": "Minimalny rozmiar partii"},
"BATCH_SIZE_MAX": {"value": 4, "comment": "Maksymalny rozmiar partii"},
"HOURLY_TARGET_RANGE": {"value": [18, 23], "comment": "Docelowa liczba followów na godzinę"},
"BOT_START_TIME": {"value": 10, "comment": "Godzina rozpoczęcia pracy bota"},
"BOT_END_TIME": {"value": 21, "comment": "Godzina zakończenia pracy bota"},
"BOT_ACTIVE_TIME_OFFSET_RANGE": {"value": [-60, 60], "comment": "Offset w minutach"},
"EMULATOR_NAME": {"value": "Pixel_1", "comment": "Nazwa AVD/emulatora"},
"EMULATOR_UDID": {"value": "emulator-5554", "comment": "UDID emulatora"},
"DNS": {"value": "8.8.8.8,4.4.4.4", "comment": "lista DNS dla emulatora, przecinkiem"}
}
if not os.path.exists(CONFIG_FILE):
with open(CONFIG_FILE, "w", encoding="utf-8") as f:
json.dump(default_config, f, indent=4)
print(f"[INFO] Utworzono {CONFIG_FILE} z wartościami domyślnymi.")
return default_config
else:
with open(CONFIG_FILE, "r", encoding="utf-8") as f:
return json.load(f)
config = load_config()
STAY_LOGGED_IN = config["STAY_LOGGED_IN"]["value"]
USERNAME = config["USERNAME"]["value"]
PASSWORD = config["PASSWORD"]["value"]
MAX_TO_FOLLOW = config["MAX_TO_FOLLOW"]["value"]
BATCH_SIZE_MIN = config["BATCH_SIZE_MIN"]["value"]
BATCH_SIZE_MAX = config["BATCH_SIZE_MAX"]["value"]
HOURLY_TARGET_RANGE = tuple(config["HOURLY_TARGET_RANGE"]["value"])
BOT_START_TIME = config["BOT_START_TIME"]["value"]
BOT_END_TIME = config["BOT_END_TIME"]["value"]
BOT_ACTIVE_TIME_OFFSET_RANGE = tuple(config["BOT_ACTIVE_TIME_OFFSET_RANGE"]["value"])
START_OFFSET = random.randint(*BOT_ACTIVE_TIME_OFFSET_RANGE)
END_OFFSET = random.randint(*BOT_ACTIVE_TIME_OFFSET_RANGE)
EMULATOR_NAME = config["EMULATOR_NAME"]["value"]
EMULATOR_UDID = config["EMULATOR_UDID"]["value"]
DNS = config["DNS"]["value"]
# Zakresy oczekiwań
LOGIN_LOAD_WAIT_RANGE = (5, 8)
AFTER_LOGIN_WAIT_RANGE = (5, 9)
FOLLOWERS_CLICK_WAIT_RANGE = (5, 8)
WAIT_AFTER_SCROLL_RANGE = (2, 4)
SCROLL_PAUSE_RANGE = (0.3, 1.4)
FOLLOW_ACTION_WAIT_RANGE = (8, 10)
LOAD_PROFILE_WAIT_RANGE = (5, 8)
OPEN_POST_WAIT_RANGE = (4.0, 6.0)
LIKE_POST_WAIT_RANGE = (2, 3.5)
AFTER_CLOSE_POST_WAIT_RANGE = (1.0, 2.0)
MAIN_PAGE_AFTER_OPEN_WAIT_RANGE = (3, 5)
MAIN_PAGE_SCROLL_DELAY_BASE = 30
MAIN_PAGE_SCROLL_DELAY_OFFSET_RANGE = (-9, 9)
MAIN_PAGE_BETWEEN_SCROLLS_WAIT_RANGE = (0.35, 0.7)
SESSION_OFFSET_RANGE_SHORT = (-200, 500)
SESSION_OFFSET_RANGE_LONG = (420, 800)
DYNAMIC_WAIT_OFFSET_RANGE = (-220, 220)
# Kolory ANSI
GREEN = Fore.GREEN
RED = Fore.RED
YELLOW = Fore.YELLOW
WHITE = Fore.WHITE
BLUE = Fore.BLUE
RESET = Style.RESET_ALL
avg_hourly_target = (HOURLY_TARGET_RANGE[0] + HOURLY_TARGET_RANGE[1]) / 2.0
avg_batch_size = (BATCH_SIZE_MIN + BATCH_SIZE_MAX) / 2.0
avg_followers_click = (FOLLOWERS_CLICK_WAIT_RANGE[0] + FOLLOWERS_CLICK_WAIT_RANGE[1]) / 2.0
avg_wait_after_scroll = (WAIT_AFTER_SCROLL_RANGE[0] + WAIT_AFTER_SCROLL_RANGE[1]) / 2.0
avg_scroll_pause = (SCROLL_PAUSE_RANGE[0] + SCROLL_PAUSE_RANGE[1]) / 2.0
avg_follow_action = (FOLLOW_ACTION_WAIT_RANGE[0] + FOLLOW_ACTION_WAIT_RANGE[1]) / 2.0
avg_scrolling_time = avg_scroll_pause + avg_wait_after_scroll
avg_load_profile_wait = (LOAD_PROFILE_WAIT_RANGE[0] + LOAD_PROFILE_WAIT_RANGE[1]) / 2.0
avg_open_post_wait = (OPEN_POST_WAIT_RANGE[0] + OPEN_POST_WAIT_RANGE[0]) / 2.0
avg_like_post_wait = (LIKE_POST_WAIT_RANGE[0] + LIKE_POST_WAIT_RANGE[1]) / 2.0
avg_after_close_post_wait = (AFTER_CLOSE_POST_WAIT_RANGE[0] + AFTER_CLOSE_POST_WAIT_RANGE[1]) / 2.0
avg_batch_time = (2 * avg_follow_action * avg_batch_size) + (avg_wait_after_scroll * 2) + (0.4 * (
avg_load_profile_wait + (
1.5 * 0.5 * (avg_open_post_wait + avg_like_post_wait + avg_after_close_post_wait))))
base_dynamic_wait = (3600 / (avg_hourly_target / avg_batch_size)) - avg_batch_time
print(f"[DEBUG] Base dynamic wait: {base_dynamic_wait:.2f} sekund.")
# ==================== WCZYTYWANIE POLSKICH IMION ====================
names_file = os.path.join(os.getcwd(), "names", "names_to_look_for.txt")
try:
with open(names_file, "r", encoding="utf-8") as f:
polish_firstnames = [line.strip().lower() for line in f if line.strip()]
print(f"{GREEN}[INFO] Wczytano {len(polish_firstnames)} imion z {names_file}.{RESET}")
except Exception as e:
print(f"{RED}[BŁĄD] Nie udało się wczytać imion: {e}{RESET}")
polish_firstnames = []
polish_female_names_file = os.path.join(os.getcwd(), "names", "names_to_avoid.txt")
try:
with open(polish_female_names_file, "r", encoding="utf-8") as f:
polish_female_firstnames = [line.strip().lower() for line in f if line.strip()]
print(f"{GREEN}[INFO] Wczytano {len(polish_female_firstnames)} imion z {polish_female_names_file}.{RESET}")
except Exception as e:
print(f"{RED}[BŁĄD] Nie udało się wczytać imion: {e}{RESET}")
polish_female_firstnames = []
# ==================== FUNKCJE POMOCNICZE DO FORMATOWANIA I OCZEKIWANIA ====================
def format_duration(seconds):
seconds = int(round(seconds))
if seconds < 60:
return f"{seconds}s"
elif seconds < 3600:
return f"{seconds // 60}m {seconds % 60}s"
else:
hours = seconds // 3600
minutes = (seconds % 3600) // 60
sec = seconds % 60
return f"{hours}h {minutes}m {sec}s"
def dynamic_sleep(duration, message):
global forced_sleep, appium_error_time
forced_sleep = True
start_time_system = datetime.datetime.now()
print(f"[DEBUG] Start time: {start_time_system.strftime('%Y-%m-%d %H:%M:%S')}")
end_time = time.time() + duration
while True:
if stop_bot:
break
remaining = end_time - time.time()
if remaining < 0:
remaining = 0
sys.stdout.write("\r" + message + f" (pozostało: {format_duration(remaining)})")
sys.stdout.flush()
if remaining <= 0:
break
time.sleep(1 if remaining >= 1 else remaining)
sys.stdout.write("\n")
forced_sleep = False
def sleep_random(range_tuple, description=""):
wait_time = random.uniform(*range_tuple)
if wait_time > 10:
dynamic_sleep(wait_time, f"[INFO] Czekam {wait_time:.2f} sekund {description}.")
else:
current_time = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
print(f"[DEBUG] Current system time: {current_time}")
print(f"[INFO] Czekam {wait_time:.2f} sekund {description}")
time.sleep(wait_time)
# ==================== FUNKCJE POMOCNICZE DO SWIPE'ÓW I TAPÓW ====================
def tap_element(element):
loc = element.location
size = element.size
center_x = int(loc['x'] + size['width'] / 2)
center_y = int(loc['y'] + size['height'] / 2)
actions = ActionChains(driver)
actions.w3c_actions.pointer_action.move_to_location(center_x, center_y)
actions.w3c_actions.pointer_action.pointer_down()
actions.w3c_actions.pointer_action.pointer_up()
actions.perform()
def swipe_up():
size = driver.get_window_size()
screen_height = size['height']
screen_width = size['width']
swipe_distance = int(screen_height * 0.32)
start_x = screen_width // 2
start_y = int(screen_height * 0.8)
end_y = start_y - swipe_distance
actions = ActionChains(driver)
actions.w3c_actions.pointer_action.move_to_location(start_x, start_y)
actions.w3c_actions.pointer_action.pointer_down()
actions.w3c_actions.pointer_action.move_to_location(start_x, end_y)
actions.w3c_actions.pointer_action.pointer_up()
actions.perform()
def small_swipe():
size = driver.get_window_size()
screen_height = size['height']
screen_width = size['width']
standard_distance = int(screen_height * 0.25)
small_distance = int(standard_distance / 3)
start_x = screen_width // 2
start_y = int(screen_height * 0.8)
end_y = start_y - small_distance
actions = ActionChains(driver)
actions.w3c_actions.pointer_action.move_to_location(start_x, start_y)
actions.w3c_actions.pointer_action.pointer_down()
actions.w3c_actions.pointer_action.move_to_location(start_x, end_y)
actions.w3c_actions.pointer_action.pointer_up()
actions.perform()
def get_screen_size_adb():
"""
Pobiera rozmiar ekranu przez ADB
"""
try:
result = subprocess.run(['adb', 'shell', 'wm', 'size'],
capture_output=True, text=True, check=True)
# Output: Physical size: 1080x2340
size_line = result.stdout.strip()
if 'Physical size:' in size_line:
dimensions = size_line.split(':')[1].strip()
width, height = map(int, dimensions.split('x'))
return width, height
return 1080, 2340 # fallback
except:
return 1080, 2340 # fallback
def swipe_up_reels():
try:
width, height = get_screen_size_adb()
# Środek ekranu jako punkt startowy
start_x = width // 2
start_y = int(height * 0.8) # 80% wysokości ekranu (dół)
# Punkt końcowy (góra)
end_x = width // 2
end_y = int(height * 0.2) # 20% wysokości ekranu (góra)
# Czas trwania swipe w ms
duration = 300
# Komenda ADB swipe
cmd = ['adb', 'shell', 'input', 'swipe',
str(start_x), str(start_y), str(end_x), str(end_y), str(duration)]
print(f"ADB swipe: {start_x},{start_y} -> {end_x},{end_y}")
result = subprocess.run(cmd, capture_output=True, text=True)
if result.returncode == 0:
print("✓ ADB swipe wykonany pomyślnie")
time.sleep(0.5)
return True
else:
print(f"✗ ADB swipe błąd: {result.stderr}")
return False
except Exception as e:
print(f"ADB swipe nie zadziałał: {e}")
return False
def swipe_up_home_page():
try:
width, height = get_screen_size_adb()
# Środek ekranu jako punkt startowy
start_x = width // 2
start_y = int(height * 0.8) # 80% wysokości ekranu (dół)
# Punkt końcowy (góra)
end_x = width // 2
end_y = int(height * 0.3) # 20% wysokości ekranu (góra)
# Czas trwania swipe w ms
duration = 300
# Komenda ADB swipe
cmd = ['adb', 'shell', 'input', 'swipe',
str(start_x), str(start_y), str(end_x), str(end_y), str(duration)]
print(f"ADB swipe: {start_x},{start_y} -> {end_x},{end_y}")
result = subprocess.run(cmd, capture_output=True, text=True)
if result.returncode == 0:
print("✓ ADB swipe wykonany pomyślnie")
time.sleep(0.5)
return True
else:
print(f"✗ ADB swipe błąd: {result.stderr}")
return False
except Exception as e:
print(f"ADB swipe nie zadziałał: {e}")
return False
# ==================== FUNKCJE DOTYCZĄCE INICJALIZACJI I NAWIGACJI ====================
def get_username_from_profile():
try:
profile_button = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.XPATH, "//*[@resource-id='com.instagram.android:id/tab_avatar']"))
)
profile_button.click()
time.sleep(random.uniform(0.35, 0.5))
profile_button.click()
print(f"{BLUE}[INFO] Kliknięto przycisk profilu.{RESET}")
username_elem = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.XPATH, "//*[@resource-id='com.instagram.android:id/action_bar_large_title_auto_size']"))
)
username_text = username_elem.text.strip()
print(f"{BLUE}[INFO] Wczytano username z profilu: {username_text}{RESET}")
return username_text
except Exception as e:
print(f"{RED}[ERROR] Nie udało się pobrać username z profilu: {e}{RESET}")
return None
def open_profile(username):
try:
search_tab = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.XPATH, SEARCH_TAB_SELECTOR))
)
search_tab.click()
time.sleep(random.uniform(0.35, 0.5))
search_tab.click()
print(f"{BLUE}[INFO] Kliknięto przycisk wyszukiwania.{RESET}")
time.sleep(random.uniform(10, 12))
search_field = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.XPATH, SEARCH_FIELD_SELECTOR))
)
search_field.click()
search_field = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.XPATH, SEARCH_FIELD_SELECTOR))
)
search_field.send_keys(username)
time.sleep(random.uniform(0.05, 0.1))
print(f"{BLUE}[INFO] Wpisano username: {username}.{RESET}")
time.sleep(random.uniform(4, 6))
first_result = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.XPATH, FIRST_RESULT_SELECTOR))
)
first_result.click()
print(f"{BLUE}[INFO] Kliknięto pierwszy wynik wyszukiwania dla {username}.{RESET}")
sleep_random(LOAD_PROFILE_WAIT_RANGE, f"(po otwarciu profilu {username} przez wyszukiwanie)")
except Exception as e:
print(f"{RED}[ERROR] Nie udało się otworzyć profilu {username} przez wyszukiwanie: {e}{RESET}")
def open_followers_popup(target):
open_profile(target)
try:
followers_button = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.XPATH, MOBILE_FOLLOWERS_BUTTON_SELECTOR))
)
followers_button.click()
sleep_random(FOLLOWERS_CLICK_WAIT_RANGE, "(po kliknięciu followers)")
followers_list = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.XPATH, MOBILE_FOLLOWERS_LIST_XPATH))
)
return followers_list
except Exception as e:
# spróbuj ponownie po krótkim odczekaniu
print(f"[WARN] open_followers_popup: {e}, ponawiam...")
driver.activate_app("com.instagram.android")
sleep_random((2,4), "(ponowne uruchomienie Instagrama przed followers)")
return open_followers_popup(target)
def init_account_directories(username):
account_dir = os.path.join(BASE_DIR, username)
Path(account_dir).mkdir(parents=True, exist_ok=True)
global TARGET_USERS_FILE
TARGET_USERS_FILE = os.path.join(account_dir, "target_accounts_for_followers.txt")
if not os.path.exists(TARGET_USERS_FILE):
open(TARGET_USERS_FILE, "w", encoding="utf-8").close()
global ALREADY_FOLLOWED_FILE, followed_file_set, already_followed
ALREADY_FOLLOWED_FILE = os.path.join(account_dir, "already_followed.txt")
if not os.path.exists(ALREADY_FOLLOWED_FILE):
open(ALREADY_FOLLOWED_FILE, "w", encoding="utf-8").close()
with open(ALREADY_FOLLOWED_FILE, "r", encoding="utf-8") as f:
followed_file_set = set(line.strip() for line in f if line.strip())
already_followed = set()
account_file = os.path.join(account_dir, "total_followed.json")
if not os.path.exists(account_file):
with open(account_file, "w", encoding="utf-8") as f:
json.dump({"total_followed": 0}, f)
print(f"{BLUE}[INFO] Utworzono plik {account_file} z total_followed = 0.{RESET}")
def get_already_followed_filename(username):
account_dir = os.path.join(BASE_DIR, username)
Path(account_dir).mkdir(parents=True, exist_ok=True)
return os.path.join(account_dir, "already_followed.txt")
def get_account_filename(username):
account_dir = os.path.join(BASE_DIR, username)
return os.path.join(account_dir, "total_followed.json")
def load_account_variables(username):
filename = get_account_filename(username)
if os.path.exists(filename):
try:
with open(filename, "r", encoding="utf-8") as f:
data = json.load(f)
total_followed = data.get("total_followed", 0)
print(f"[INFO] Wczytano total_followed = {total_followed} z pliku {filename}.")
return total_followed
except Exception as e:
print(f"{RED}[BŁĄD] Problem przy wczytywaniu pliku {filename}: {e}{RESET}")
return 0
def update_account_variables(username, total_followed):
filename = get_account_filename(username)
data = {"total_followed": total_followed}
try:
with open(filename, "w", encoding="utf-8") as f:
json.dump(data, f)
print(f"[INFO] Zaktualizowano total_followed = {total_followed} w pliku {filename}.")
except Exception as e:
print(f"{RED}[BŁĄD] Nie udało się zapisać pliku {filename}: {e}{RESET}")
def convert_polish_to_english(s):
mapping = {'ą': 'a', 'ę': 'e', 'ó': 'o', 'ś': 's', 'ż': 'z', 'ź': 'z', 'ć': 'c', 'ń': 'n'}
return "".join(mapping.get(char.lower(), char) for char in s)
def clean_nick(nick):
return re.sub(r'[^A-Za-ząćęłńóśźżĄĆĘŁŃÓŚŹŻ]', '', nick).lower()
def has_polish_firstname_in_nick(nick):
cleaned = clean_nick(nick)
english_nick = convert_polish_to_english(cleaned)
return any(name in english_nick for name in polish_firstnames)
def get_polish_firstname(nick):
cleaned = clean_nick(nick)
english_nick = convert_polish_to_english(cleaned)
for name in polish_firstnames:
if name in english_nick:
return name
return None
def get_polish_female_firstname(nick):
cleaned = clean_nick(nick)
english_nick = convert_polish_to_english(cleaned)
for name in polish_female_firstnames:
if name in english_nick:
return name
return None
def update_last_action_time():
global last_action_time
last_action_time = time.time()
# ==================== FUNKCJE OBSŁUGUJĄCE APLIKACJĘ INSTAGRAM ====================
def login_instagram():
try:
driver.activate_app("com.instagram.android")
if STAY_LOGGED_IN:
print("[INFO] Używam zapisanej sesji – pomijam procedurę logowania.")
init_account_directories(USERNAME)
return
print("[INFO] Rozpoczynam procedurę logowania...")
WebDriverWait(driver, 20).until(
EC.presence_of_element_located((By.XPATH, MOBILE_LOGIN_USERNAME_SELECTOR))
)
driver.find_element(By.XPATH, MOBILE_LOGIN_USERNAME_SELECTOR).send_keys(USERNAME)
driver.find_element(By.XPATH, MOBILE_LOGIN_PASSWORD_SELECTOR).send_keys(PASSWORD)
login_button = driver.find_element(By.XPATH, MOBILE_LOGIN_BUTTON_SELECTOR)
login_button.click()
sleep_random(LOGIN_LOAD_WAIT_RANGE, "(po kliknięciu przycisku logowania)")
try:
not_now = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.XPATH, MOBILE_NOT_NOW_BUTTON_SELECTOR))
)
not_now.click()
print("[INFO] Kliknięto 'Not Now'.")
except Exception as e:
print(f"{YELLOW}[INFO] Popup 'Not Now' nie został znaleziony: {e}{RESET}")
sleep_random(AFTER_LOGIN_WAIT_RANGE, "(po zalogowaniu)")
print("[INFO] Zalogowano pomyślnie.")
init_account_directories(USERNAME)
except Exception as e:
print(f"{RED}[BŁĄD] Problem podczas logowania: {e}{RESET}")
driver.save_screenshot("login_error.png")
sys.exit(1)
def get_next_candidate(scroll_container, skip_initial_refresh=False):
candidates, finished = get_followers_from_open_popup(
scroll_container,
needed_count=1,
skip_initial_refresh=skip_initial_refresh
)
if candidates:
return candidates[0], finished
return None, finished
def ensure_active_session():
global driver
try:
_ = driver.current_activity
except Exception as e:
print(f"{YELLOW}[INFO] Driver session nieaktywna: {e}. Re-inicjalizuję drivera...{RESET}")
driver = initialize_driver(EMULATOR_NAME, EMULATOR_UDID)
def refresh_scroll_container():
global driver, global_last_index, appium_error_time
try:
if not hasattr(driver, "session_id") or not driver.session_id:
print(f"{YELLOW}[INFO] Sesja została zakończona – ponowna inicjalizacja drivera...{RESET}")
driver = initialize_driver(EMULATOR_NAME, EMULATOR_UDID)
_ = driver.current_activity
new_container = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.XPATH, MOBILE_FOLLOWERS_LIST_XPATH))
)
global_last_index = 0
sleep_random((1.5, 2.2), "na odświeżenie kontenera")
print(f"{BLUE}[INFO] Odświeżono kontener followersów.{RESET}")
appium_error_time = None
return new_container
except (NoSuchDriverException, Exception) as e:
print(f"{RED}[ERROR] Nie udało się odświeżyć kontenera followersów: {e}{RESET}")
try:
driver = initialize_driver(EMULATOR_NAME, EMULATOR_UDID)
new_container = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.XPATH, MOBILE_FOLLOWERS_LIST_XPATH))
)
global_last_index = 0
appium_error_time = None
return new_container
except Exception as ex:
print(f"{RED}[ERROR] Re-inicjalizacja drivera nie powiodła się: {ex}{RESET}")
sys.exit(1)
def force_refresh_container():
try:
main_tab = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.XPATH, "//*[@resource-id='com.instagram.android:id/wrapper' and contains(@class, 'android.view.ViewGroup')]"))
)
main_tab.click()
time.sleep(random.uniform(1, 2))
search_tab = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.XPATH, SEARCH_TAB_SELECTOR))
)
search_tab.click()
time.sleep(random.uniform(1, 2))
new_container = refresh_scroll_container()
print(f"{BLUE}[INFO] Wymuszone odświeżenie kontenera wykonane.{RESET}")
return new_container
except Exception as e:
print(f"{RED}[ERROR] Force refresh container failed: {e}{RESET}")
return refresh_scroll_container()
def get_followers_from_open_popup(scroll_container, needed_count, skip_initial_refresh=False):
global global_last_index, current_target, driver, global_followers_start_wait
xpath_selector = ("//*[@resource-id='com.instagram.android:id/follow_list_container' or "
"@resource-id='com.instagram.android:id/see_more_button' or "
"@resource-id='com.instagram.android:id/row_header_textview']")
if global_followers_start_wait is None:
global_followers_start_wait = time.time()
start_wait_local = global_followers_start_wait
was_refreshed = False
try:
rows = scroll_container.find_elements(By.XPATH, xpath_selector)
except StaleElementReferenceException:
print(f"{YELLOW}[INFO] Kontener followersów jest nieaktualny – odświeżam go...{RESET}")
scroll_container = refresh_scroll_container()
was_refreshed = True
rows = scroll_container.find_elements(By.XPATH, xpath_selector)
if not was_refreshed and not skip_initial_refresh:
scroll_container = refresh_scroll_container()
rows = scroll_container.find_elements(By.XPATH, xpath_selector)
if (time.time() - global_followers_start_wait) > 15:
print(f"{YELLOW}[INFO] Oczekiwanie na nowe elementy trwa ponad 15 sekund. Wymuszam odświeżenie kontenera...{RESET}")
scroll_container = force_refresh_container()
global_followers_start_wait = time.time()
rows = scroll_container.find_elements(By.XPATH, xpath_selector)
if len(rows) <= global_last_index and (time.time() - global_followers_start_wait) > 30:
print(f"{YELLOW}[INFO] Odczekano ponad 30 sekund na nowy element. Wykonuję czyszczenie logcat i restart ADB/Appium...{RESET}")
clear_logcat_buffer(EMULATOR_UDID)
driver, _ = restart_adb_appium(EMULATOR_NAME, EMULATOR_UDID)
global_last_index = 0
global_followers_start_wait = time.time()
rows = scroll_container.find_elements(By.XPATH, xpath_selector)
if len(rows) <= global_last_index and (time.time() - global_followers_start_wait) > 10:
print(f"{YELLOW}[INFO] Przez 10 sekund nie było nowych elementów. Wykonuję mały swipe...{RESET}")
small_swipe()
time.sleep(3)
scroll_container = refresh_scroll_container()
rows = scroll_container.find_elements(By.XPATH, xpath_selector)
if len(rows) <= global_last_index:
print(f"{YELLOW}[INFO] Po małym swipie nadal brak nowych kont. Oznaczam target jako DONE.{RESET}")
mark_target_done(current_target)
global_followers_start_wait = None
return ([], True)
global_followers_start_wait = time.time()
batch_users = []
for i in range(global_last_index, len(rows)):
row = rows[i]
if not row.is_displayed():
continue
try:
resource_id = row.get_attribute("resource-id")
if resource_id == "com.instagram.android:id/see_more_button":
print(f"{YELLOW}[INFO] Wiersz zawiera przycisk 'See more'. Klikam go...{RESET}")
row.click()
sleep_random((2, 4), "(czekam po kliknięciu See more)")
scroll_container = refresh_scroll_container()
rows = scroll_container.find_elements(By.XPATH, xpath_selector)
global_followers_start_wait = time.time()
global_last_index = 0
return get_followers_from_open_popup(scroll_container, needed_count, skip_initial_refresh=True)
elif resource_id == "com.instagram.android:id/row_header_textview":
text = row.text.strip()
if text == "Suggested for you":
print(f"{YELLOW}[INFO] Wiersz zawiera etykietę 'Suggested for you'. Oznaczam target jako DONE.{RESET}")
mark_target_done(current_target)
return (batch_users, True)
continue
else:
primary_elements = row.find_elements(By.XPATH, ".//*[@resource-id='com.instagram.android:id/follow_list_username']")
secondary_elements = row.find_elements(By.XPATH, ".//*[@resource-id='com.instagram.android:id/follow_list_subtitle']")
if not primary_elements:
continue
primary_nick = primary_elements[0].text.strip()
secondary_nick = secondary_elements[0].text.strip() if secondary_elements else ""
button = row.find_element(By.XPATH, MOBILE_FOLLOW_BUTTON_ROW_XPATH)
btn_text = button.text.strip().lower()
if any(x in btn_text for x in ["following", "requested"]):
print(f"{RED}[INFO] Pomijam {primary_nick}, status: \"{btn_text}\"{RESET}")
continue
if primary_nick in followed_file_set:
print(f"{RED}[INFO] Pomijam {primary_nick}, już wcześniej zafollowowane (plik){RESET}")
continue
if any(x in btn_text for x in ["follow", "obserwuj"]):
male_name = get_polish_firstname(primary_nick)
source = "primary"
if male_name is None:
male_name = get_polish_firstname(secondary_nick)
source = "secondary" if male_name else None
if male_name is not None:
female_name = None
if source == "primary":
female_name = get_polish_female_firstname(primary_nick)
elif source == "secondary":
female_name = get_polish_female_firstname(secondary_nick)
if female_name is not None:
print(f"{YELLOW}[INFO] W {source} nicku {primary_nick} znaleziono żeńskie imię '{female_name}'. Pomijam.{RESET}")
else:
batch_users.append((primary_nick, row))
print(f"{GREEN}[INFO] Dodano {primary_nick} do batcha. Znaleziono męskie imię '{male_name}' w {source} nicku.{RESET}")
else:
print(f"{YELLOW}[INFO] Nie znaleziono męskiego imienia w nicku {primary_nick}.{RESET}")
except Exception as e:
print(f"{YELLOW}[INFO] Błąd przy przetwarzaniu wiersza: {e}{RESET}")
continue
global_last_index = len(rows)
return (batch_users, False)
def like_posts_in_profile():
try:
sleep_random(LOAD_PROFILE_WAIT_RANGE, "(po otwarciu profilu przed likeowaniem)")
posts = driver.find_elements(By.XPATH, MOBILE_PROFILE_POST_BUTTON_SELECTOR)
num_posts = len(posts)
print(f"{BLUE}[INFO] Znaleziono {num_posts} postów na profilu.{RESET}")
if num_posts == 0:
print(f"{YELLOW}[INFO] Brak postów do polubienia.{RESET}")
return
if num_posts > 1:
num_to_like = random.randint(1, min(2, num_posts))
else:
num_to_like = 1
print(f"{BLUE}[INFO] Wybrano {num_to_like} post(y) do like'owania.{RESET}")
available_indices = list(range(num_posts))
chosen_indices = random.sample(available_indices, num_to_like)
for idx in chosen_indices:
try:
posts = driver.find_elements(By.XPATH, MOBILE_PROFILE_POST_BUTTON_SELECTOR)
if idx >= len(posts):
print(f"{YELLOW}[INFO] Indeks {idx} jest poza zakresem dostępnych postów po odświeżeniu.{RESET}")
continue
post = posts[idx]
post.click()
print(f"{BLUE}[INFO] Kliknięto post o indeksie {idx}.{RESET}")
sleep_random(OPEN_POST_WAIT_RANGE, "(po otwarciu posta)")
like_btn = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.XPATH, MOBILE_PROFILE_LIKE_BUTTON_SELECTOR))
)
tap_element(like_btn)
print(f"{BLUE}[INFO] Polubiono post o indeksie {idx}.{RESET}")
sleep_random(LIKE_POST_WAIT_RANGE, "(po likeowaniu)")
back_btn = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.XPATH, MOBILE_BACK_BUTTON_SELECTOR))
)
back_btn.click()
print(f"{BLUE}[INFO] Wrócono z posta o indeksie {idx}.{RESET}")
sleep_random((2, 3), "(po powrocie z posta i odświeżeniu elementów)")
posts = driver.find_elements(By.XPATH, MOBILE_PROFILE_POST_BUTTON_SELECTOR)
print(f"{BLUE}[INFO] Odświeżono listę postów po powrocie z posta.{RESET}")
except Exception as e:
print(f"{YELLOW}[INFO] Błąd przy próbie like'owania posta o indeksie {idx}: {e}{RESET}")
try:
back_btn = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.XPATH, MOBILE_BACK_BUTTON_SELECTOR))
)
back_btn.click()
sleep_random((2, 3), "(po powrocie z błędu)")
except Exception:
pass
except Exception as e:
print(f"{RED}[BŁĄD] Problem podczas like'owania postów: {e}{RESET}")
def follow_user(candidate):
allowed = is_current_time_allowed()
if not allowed:
wait = seconds_until_next_start()
dynamic_sleep(wait, "[INFO] Czekam do godzinnego okna")
# Pierwsze losowanie: Direct vs New Activity
random_value = random.random()
use_new_activity = (random_value < 0.4) # 60% Direct, 40% New Activity
username, row = candidate
print(f"[DEBUG] Losowanie dla {username}: random_value={random_value:.3f}, use_new_activity={use_new_activity}")
if use_new_activity: # Tryb "New Activity"
try:
row.click()
sleep_random(LOAD_PROFILE_WAIT_RANGE, "(po wejściu w profil)")
# Drugie losowanie: od razu follow czy polubić posty
follow_immediately = (random.random() < 0.5) # 50% na od razu follow, 50% na polubienie postów
print(f"[DEBUG] New Activity dla {username}: follow_immediately={follow_immediately}")
if not follow_immediately: # Najpierw polub posty
like_posts_in_profile()
# Follow z profilu
profile_follow = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.XPATH, MOBILE_PROFILE_FOLLOW_BUTTON_SELECTOR))
)
btn_text = profile_follow.text.strip().lower()
if any(x in btn_text for x in ["following", "requested"]):
print(f"{RED}[INFO] (New Activity) Pomijam {username}, status: \"{btn_text}\"{RESET}")
WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.XPATH, MOBILE_BACK_BUTTON_SELECTOR))
).click()
return False
if any(x in btn_text for x in ["follow", "obserwuj"]):
profile_follow.click()
print(f"{BLUE}[INFO] (New Activity) Kliknięto Follow dla {username}.{RESET}")
sleep_random(FOLLOW_ACTION_WAIT_RANGE, "(po follow)")
try:
new_text = profile_follow.text.strip().lower()
except Exception:
new_text = ""
if new_text in ["follow", "obserwuj"]:
print(f"{RED}[BŁĄD] (New Activity) Follow nie powiódł się – prawdopodobnie rate limit{RESET}")
WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.XPATH, MOBILE_BACK_BUTTON_SELECTOR))
).click()
sys.exit(1)
else:
print(f"{BLUE}[INFO] (New Activity) Zaobserwowano {username}.{RESET}")
already_followed.add(username)
with open(get_already_followed_filename(USERNAME), "a", encoding="utf-8") as f:
f.write(username + "\n")
followed_file_set.add(username)
WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.XPATH, MOBILE_BACK_BUTTON_SELECTOR))
).click()
return True
print(f"[INFO] (New Activity) Nieobsługiwany przycisk '{btn_text}' dla {username}")
WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.XPATH, MOBILE_BACK_BUTTON_SELECTOR))
).click()
return False
except TimeoutException:
print(f"{YELLOW}[INFO] (New Activity) Brak przycisku Follow dla {username}{RESET}")
try:
WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.XPATH, MOBILE_BACK_BUTTON_SELECTOR))
).click()
except Exception:
pass
return False
except Exception as e:
print(f"{RED}[BŁĄD] (New Activity) Problem przy follow dla {username}: {e}{RESET}")
try:
WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.XPATH, MOBILE_BACK_BUTTON_SELECTOR))
).click()
except Exception:
pass
return False
else: # Tryb "Direct"
try:
follow_button = row.find_element(By.XPATH, MOBILE_FOLLOW_BUTTON_ROW_XPATH)
btn_text = follow_button.text.strip().lower()
if any(x in btn_text for x in ["following", "requested"]):
print(f"{RED}[INFO] (Direct) Pomijam {username}, status: \"{btn_text}\"{RESET}")
return False
if any(x in btn_text for x in ["follow", "obserwuj"]):
follow_button.click()
print(f"{BLUE}[INFO] (Direct) Kliknięto Follow dla {username}.{RESET}")
sleep_random(FOLLOW_ACTION_WAIT_RANGE, "(po follow)")
try:
new_text = follow_button.text.strip().lower()
except Exception:
new_text = ""
if new_text in ["follow", "obserwuj"]:
print(f"{RED}[BŁĄD] (Direct) Follow nie powiódł się – prawdopodobnie rate limit{RESET}")
sys.exit(1)
else:
print(f"{BLUE}[INFO] (Direct) Zaobserwowano {username}.{RESET}")
already_followed.add(username)
with open(get_already_followed_filename(USERNAME), "a", encoding="utf-8") as f:
f.write(username + "\n")
followed_file_set.add(username)
return True
print(f"{RED}[INFO] (Direct) Nieobsługiwany przycisk '{btn_text}' dla {username}{RESET}")
sys.exit(1)
except TimeoutException:
print(f"{YELLOW}[INFO] (Direct) Brak przycisku Follow dla {username}{RESET}")
return False
except Exception as e:
print(f"{RED}[BŁĄD] (Direct) Problem przy follow dla {username}: {e}{RESET}")
return False
def mark_target_done(target):
try:
with open(TARGET_USERS_FILE, "r", encoding="utf-8") as f:
lines = f.readlines()
with open(TARGET_USERS_FILE, "w", encoding="utf-8") as f:
for line in lines:
parts = line.strip().split()
if parts and parts[0] == target and "DONE" not in parts:
f.write(f"{target} DONE\n")
else:
f.write(line)
print(f"{YELLOW}[INFO] Oznaczono target '{target}' jako DONE.{RESET}")
except Exception as e:
print(f"{RED}[BŁĄD] Nie udało się oznaczyć targetu {target} jako DONE: {e}{RESET}")
def is_current_time_allowed():
now = datetime.datetime.now()
# podstawowe daty graniczne z offsetami
base_start = now.replace(hour=BOT_START_TIME, minute=0, second=0, microsecond=0) \
+ datetime.timedelta(minutes=START_OFFSET)
base_end = now.replace(hour=BOT_END_TIME, minute=0, second=0, microsecond=0) \
+ datetime.timedelta(minutes=END_OFFSET)
if base_start <= base_end:
# proste okno w ciągu dnia
start_dt, end_dt = base_start, base_end
else:
# okno przechodzi przez północ
if now >= base_start:
# jesteśmy między base_start (dziś) a północą → end = jutro
start_dt = base_start
end_dt = base_end + datetime.timedelta(days=1)
else:
# jesteśmy przed base_end (dziś) → start = wczoraj
start_dt = base_start - datetime.timedelta(days=1)
end_dt = base_end
return start_dt <= now < end_dt
def seconds_until_next_start():
now = datetime.datetime.now()
# kiedy przypada najbliższy start z offsetem
base_start = now.replace(hour=BOT_START_TIME, minute=0, second=0, microsecond=0) \
+ datetime.timedelta(minutes=START_OFFSET)
if is_current_time_allowed():
return 0.0
# jeśli teraz < base_start → czekamy do tej samej daty
if now < base_start:
next_start = base_start
else:
# inaczej do jutra o tej samej godzinie+offset
next_start = base_start + datetime.timedelta(days=1)
return (next_start - now).total_seconds()
def handle_main_feed_page(wait_time):
# upewnij się, że driver działa
ensure_active_session()
try:
main_feed_btn = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((
By.XPATH,
"//*[@resource-id='com.instagram.android:id/wrapper' and contains(@class, 'android.view.ViewGroup')]"
))
)
main_feed_btn.click()
time.sleep(1)
main_feed_btn.click()
except StaleElementReferenceException:
print("[INFO] main_feed_btn stale – ponawiam przełączanie na feed")
return handle_main_feed_page(wait_time)
except Exception as e:
print(f"[ERROR] Błąd przełączania na główny feed: {e}")
return
sleep_random(MAIN_PAGE_AFTER_OPEN_WAIT_RANGE, "(po otwarciu feedu)")
start_time = time.time()
while True:
if stop_bot:
break
if not is_current_time_allowed():
global bot_mode, last_wait_mode, last_wait_remaining
bot_mode = "waiting"
last_wait_mode = "feed"
elapsed = time.time() - start_time
last_wait_remaining = max(0, wait_time - elapsed)
return
elapsed = time.time() - start_time
remaining = wait_time - elapsed
if remaining <= 0:
break
swipe_up_home_page()
update_last_action_time()
print(f"[INFO] Swipe w feedzie. Pozostało: {format_duration(remaining)}.")
sleep_random((25, 35), "przed następnym scrollowaniem")
print("[INFO] Zakończono feed – wracam do wyszukiwania.")
def handle_reels_page(wait_time):
ensure_active_session()
try:
reels_tab = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.XPATH, MOBILE_REELS_TAB_SELECTOR))
)