-
Notifications
You must be signed in to change notification settings - Fork 5
/
follower.py
100 lines (80 loc) · 3.28 KB
/
follower.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
import threading
import requests
from time import sleep, time
import os
import sys
import logging
from contextlib import contextmanager
from typing import Iterator, Tuple, List
from config import UserConfig, Config
from request import get_current_followers, send_follow_request
logger = logging.getLogger()
@contextmanager
def timed(description: str) -> Iterator[None]:
start = time()
yield
elapsed = time() - start
logger.debug(f"{description} took {elapsed:.2f}s")
def follower_checker(config: Config):
while True:
count = get_current_followers(
user_id=config.target_user_id,
auth_token=config.default_auth_token,
device_id=config.device_id,
cfduid=config.cfduid,
)
logger.info(f"Currently have {count} followers")
sleep(config.follower_check_interval)
def infinite_follower(config: Config, user_config: UserConfig):
follow_count = 0
while True:
logger.debug("Sending a follow request...")
with timed("Follow request"):
try:
status, response = send_follow_request(
user_id=user_config.user_id,
auth_token=user_config.auth_token,
device_id=config.device_id,
cfduid=config.cfduid,
target_user_id=config.target_user_id,
)
except requests.exceptions.RequestException as e:
logger.warn(
f"Stopping the follower for the user id {user_config.user_id} "
f"because we got an exception.\n"
f"Exception: {e}\n"
f"Since the start of the script, this user id has successfully sent {follow_count} follow requests"
)
break
if status == 429:
logger.warn(
f"Stopping the follower for the user id {user_config.user_id} "
f"because we reached the rate limit for that user.\n"
f"Status code: {status} - Response: {response}\n"
f"Since the start of the script, this user id has successfully sent {follow_count} follow requests"
)
break
if status != 200:
# We sometimes get 502s, not stopping the script for those
logger.warn(
f"An unexpected status code for the user id {user_config.user_id} "
f"Status code: {status} - Response: {response}\n"
f"Since the start of the script, this user id has successfully sent {follow_count} follow requests"
)
if status == 200:
follow_count += 1
logger.debug(f"Follow request sent. Status: {status} Response: {response}")
sleep(config.follow_interval)
def start_follow_checker(config: Config):
t = threading.Thread(target=follower_checker, daemon=True, args=(config,))
t.start()
def start_infinite_followers(config: Config):
for i, user_config in enumerate(config.users):
if user_config.skip:
continue
t = threading.Thread(
name=f"Follower-{i}",
target=infinite_follower,
args=(config, user_config),
)
t.start()