Created
September 2, 2022 18:51
-
-
Save iklobato/8265abfbfac8928fe3a29ee75171d2d4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
from random import choice | |
import argparse | |
from tqdm import tqdm | |
import base64 | |
import re | |
import requests | |
FILE_NAME = 'proxies_downloaded.txt' | |
class ProxyManager(object): | |
parser = argparse.ArgumentParser(description='Proxy Manager') | |
parser.add_argument( | |
'--quantity', | |
'-q', | |
type=int, | |
default=0, | |
required=False, | |
) | |
parser.add_argument( | |
'--download', | |
'-d', | |
action='store_true', | |
required=False, | |
dest='download', | |
) | |
def __init__(self, **kw): | |
self.proxy_list = self.download() | |
def get_ip_and_port(self, line): | |
line = line.replace('\n', '') | |
if line.startswith('http://') or line.startswith('https://'): | |
return line.split(':')[0], line.split(':')[1] | |
if ':' in line: | |
return line.split(':')[0], line.split(':')[1] | |
return line, '80' | |
def is_ip(self, ip): | |
regex_ip = r'^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$' | |
return True if re.match(regex_ip, ip) else False | |
def get_ip_info(self, ip): | |
info_url = 'https://api.ipify.org' | |
content = requests.get(info_url) | |
content = content.json() | |
return { | |
'ip': content['ip'], | |
'hostname': content['hostname'], | |
'city': content['city'], | |
'region': content['region'], | |
'country': content['country'], | |
'loc': content['loc'], | |
'org': content['org'], | |
'postal': content['postal'], | |
} | |
def download(self, *args, **options): | |
if os.path.exists(FILE_NAME): | |
return open(FILE_NAME, 'r').readlines() | |
print('Downloading proxies...') | |
download_url = base64.b64decode( | |
'your proxy provider' | |
).decode('utf-8').replace('\n', '') | |
content = requests.get(download_url) | |
content = content.json() | |
file_content = [] | |
for i in content['proxy-providers']: | |
if i['type'] == 1: # http type | |
response = requests.get(i['url']) | |
if response.status_code not in [200, 301, 302]: | |
continue | |
response = response.text.split('\n') | |
for line in tqdm(response): | |
ip, port = self.get_ip_and_port(line) | |
if self.is_ip(ip): | |
file_content.append(f'{ip}:{port}') | |
with open(FILE_NAME, 'w') as f: | |
f.write('\n'.join(file_content)) | |
print(f'{len(file_content)} proxies downloaded at {os.getcwd()}/{FILE_NAME}') | |
return file_content | |
def get(self, *args, **options): | |
proxies = self.proxy_list | |
for i in range(self.parser.parse_args().quantity): | |
proxy_choice = choice(proxies) | |
ip, port = self.get_ip_and_port(proxy_choice) | |
print(f'{ip}:{port}') | |
def proxy(self, *args, **options): | |
proxies = self.proxy_list | |
proxy_choice = choice(proxies) | |
ip, port = self.get_ip_and_port(proxy_choice) | |
return { | |
'ip': ip, | |
'port': port, | |
'proxy': { | |
'http': f'http://{ip}:{port}', | |
'https': f'https://{ip}:{port}' | |
} | |
} | |
def ip_info(self, ip): | |
url = f'http://ip-api.com/json/{ip}' | |
response = requests.get(url) | |
return response.json() | |
if __name__ == '__main__': | |
pm = ProxyManager() | |
chosen_proxy = pm.get() | |
Author
iklobato
commented
Sep 2, 2022
•
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment