Skip to content

Commit

Permalink
Merge pull request #18 from birdhouses/implement-task-cycle
Browse files Browse the repository at this point in the history
Add proxies.txt to .gitignore and update next_proxy() function to rea…
  • Loading branch information
birdhouses authored Apr 24, 2023
2 parents 928395e + 8c4b2bd commit d2ccf08
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 9 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -158,3 +158,4 @@ cython_debug/
#.idea/
settings.json
followed_users.txt
proxies.txt
36 changes: 27 additions & 9 deletions instabot/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,29 @@ def freeze(message: str, hours: int = 0, days: int = 0) -> None:
freeze_time = hours * 3600 + days * 86400
time.sleep(freeze_time)

def read_proxies():
try:
with open('proxies.txt', 'r') as f:
raw_proxies = [line.strip() for line in f.readlines()]
return raw_proxies
except FileNotFoundError:
print("Warning: proxies.txt not found, returning an empty list.")
return []

def next_proxy() -> str:
config = load_config('config.json')
url = config['proxy_url']
response = requests.get(url)
response.raise_for_status()
raw_proxies = read_proxies()

if not raw_proxies:
print("No proxies available, returning an empty string.")
return ""

raw_proxy = random.choice(raw_proxies)

raw_proxies = response.text.strip().split('\n')
proxies = [f"http://{proxy.split(':')[2]}:{proxy.split(':')[3]}@{proxy.split(':')[0]}:{proxy.split(':')[1]}" for proxy in raw_proxies]
ip, port, username, password = raw_proxy.split(':')
formatted_proxy = f"http://{username}:{password}@{ip}:{port}"

return formatted_proxy

return random.choice(proxies)

def load_or_login_and_save_session(client, username, password, session_file_path):
if os.path.exists(session_file_path):
Expand Down Expand Up @@ -85,25 +98,30 @@ def handle_exception(client: Client, e: Exception) -> Union[bool, None]:
if isinstance(e, BadPassword):
raise e
elif isinstance(e, LoginRequired):
freeze(e, 1)
remove_session_and_login(client, username=username, password=password, session_file_path=session_file_path)
return True
elif isinstance(e, ChallengeRequired):
raise e
elif isinstance(e, FeedbackRequired):
client.set_proxy(next_proxy())
freeze(e, hours=1)
remove_session_and_login(client, username=username, password=password, session_file_path=session_file_path)
return True
elif isinstance(e, PleaseWaitFewMinutes):
client.set_proxy(next_proxy())
freeze(e, 1)
return True
elif isinstance(e, RetryError):
client.set_proxy(next_proxy())
remove_session_and_login(client, username=username, password=password, session_file_path=session_file_path)
freeze(e, hours=1)
return True
raise e

client = Client()
proxy = next_proxy()
logger.info(proxy)
client.set_proxy(next_proxy())

client.handle_exception = handle_exception
load_or_login_and_save_session(client, username, password, session_file_path)

Expand Down

0 comments on commit d2ccf08

Please sign in to comment.