<gh_stars>0
import os
import requests
import random
from time import sleep

# Config
VPN_COUNTRIES = ['United States']
VPN_CATEGORIES = ['Standard VPN servers']
VPN_LOAD_LIMIT = 60
VPN_LOWEST_LOAD = False

def vpn_reconnect():
    vpn_disconnect()
    sleep(0.2)
    vpn_connect()

def vpn_connect():
    target_server = getNewVPN(VPN_COUNTRIES, VPN_CATEGORIES, VPN_LOAD_LIMIT, VPN_LOWEST_LOAD)
    if target_server == 'Error':
        print('Could not get new server from VPN API. Please try again later.')
        return
    os.popen('nordvpn connect ' + target_server).read()
    os.wait()
    status = os.popen('nordvpn status').read().split("Status: ",1)[1].lower()
    if 'reconnecting' in status or 'disconnected' in status:
        os.popen('nordvpn connect --group The_Americas United_States').read()
        os.wait()
    
def vpn_disconnect():
    os.popen('nordvpn disconnect').read()
    os.wait()

def getNewVPN(target_countries, target_categories, max_load=100, lowest_load=True):
    matched_servers = []

    response = requests.get('https://nordvpn.com/api/server') 
    for srv in response.json():
        if srv['country'] in target_countries and int(srv['load']) < max_load:
            for category in srv['categories']:
                if category['name'] in target_categories:
                    matched_servers.append(srv)     # Add server to match list if all parameters match

    if matched_servers == []:
        print('No servers found matching parameters.')
        return 'Error'

    matched_servers.sort(key=lambda x: x['load'])   # Sort by lowest load %

    server = ''
    if lowest_load:
        server = matched_servers[0]
    else:
        server = random.choice(matched_servers)     # Pick random matched server if lowest_load == False

    return server['domain'][:server['domain'].find('.')]

def get_VPN_status():
    status = os.popen('nordvpn status').read().split("Status: ",1)[1]
    if(status.lower().find('disconnected')) != -1:
        return "VPN Status: Disconnected ❌"
    elif(status.lower().find('connected')) != -1:
        return "VPN Status: Connected ✔️"
    return "VPN Status: Reconnecting..."
import os
import requests
import random
from time import sleep

# Config
VPN_COUNTRIES = ['United States']
VPN_CATEGORIES = ['Standard VPN servers']
VPN_LOAD_LIMIT = 60
VPN_LOWEST_LOAD = False

def vpn_reconnect():
    vpn_disconnect()
    sleep(0.2)
    vpn_connect()

def vpn_connect():
    target_server = getNewVPN(VPN_COUNTRIES, VPN_CATEGORIES, VPN_LOAD_LIMIT, VPN_LOWEST_LOAD)
    if target_server == 'Error':
        print('Could not get new server from VPN API. Please try again later.')
        return
    os.popen('nordvpn connect ' + target_server).read()
    os.wait()
    status = os.popen('nordvpn status').read().split("Status: ",1)[1].lower()
    if 'reconnecting' in status or 'disconnected' in status:
        os.popen('nordvpn connect --group The_Americas United_States').read()
        os.wait()
    
def vpn_disconnect():
    os.popen('nordvpn disconnect').read()
    os.wait()

def getNewVPN(target_countries, target_categories, max_load=100, lowest_load=True):
    matched_servers = []

    response = requests.get('https://nordvpn.com/api/server') 
    for srv in response.json():
        if srv['country'] in target_countries and int(srv['load']) < max_load:
            for category in srv['categories']:
                if category['name'] in target_categories:
                    matched_servers.append(srv)     # Add server to match list if all parameters match

    if matched_servers == []:
        print('No servers found matching parameters.')
        return 'Error'

    matched_servers.sort(key=lambda x: x['load'])   # Sort by lowest load %

    server = ''
    if lowest_load:
        server = matched_servers[0]
    else:
        server = random.choice(matched_servers)     # Pick random matched server if lowest_load == False

    return server['domain'][:server['domain'].find('.')]

def get_VPN_status():
    status = os.popen('nordvpn status').read().split("Status: ",1)[1]
    if(status.lower().find('disconnected')) != -1:
        return "VPN Status: Disconnected ❌"
    elif(status.lower().find('connected')) != -1:
        return "VPN Status: Connected ✔️"
    return "VPN Status: Reconnecting..."
# Config # Add server to match list if all parameters match # Sort by lowest load % # Pick random matched server if lowest_load == False