Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes for new API #303

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions openpyn/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def get_data_from_api(
country_code: str, area: str, p2p: bool, dedicated: bool, double_vpn: bool,
tor_over_vpn: bool, anti_ddos: bool, netflix: bool, location: float) -> List:
country_code = country_code.lower()
url = "https://api.nordvpn.com/server"
url = "https://api.nordvpn.com/v1/servers?limit=0"
json_response = get_json_cached(url)

type_filtered_servers = []
Expand All @@ -92,32 +92,32 @@ def get_data_from_api(

def list_all_countries() -> None:
countries_mapping = {}
url = "https://api.nordvpn.com/server"
url = "https://api.nordvpn.com/v1/servers?limit=0"
json_response = get_json_cached(url)
for res in json_response:
if res["domain"][:2] not in countries_mapping:
countries_mapping.update({res["domain"][:2]: res["country"]})
if res["hostname"][:2] not in countries_mapping:
countries_mapping.update({res["hostname"][:2]: res["country"]})
for key, val in countries_mapping.items():
print("Full Name : " + val + "\t\tCountry Code : " + key)


def get_country_code(full_name: str) -> str:
full_name = full_name.lower()
url = "https://api.nordvpn.com/server"
url = "https://api.nordvpn.com/v1/servers?limit=0"
json_response = get_json_cached(url)
for res in json_response:
if res["country"].lower() == full_name:
code = res["domain"][:2]
code = res["hostname"][:2]
return code
raise RuntimeError("Country Name Not Correct")


def get_country_name(iso_code: str) -> str:
iso_code = iso_code.lower()
url = "https://api.nordvpn.com/server"
url = "https://api.nordvpn.com/v1/servers?limit=0"
json_response = get_json_cached(url)
for res in json_response:
if res["domain"][:2] == iso_code:
if res["hostname"][:2] == iso_code:
name = res["country"]
return name
raise RuntimeError("Country Code Not Correct")
61 changes: 32 additions & 29 deletions openpyn/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ def filter_by_area(area: str, type_country_filtered: List) -> List:
def filter_by_country(country_code: str, type_filtered_servers: List) -> List:
remaining_servers = []
for aServer in type_filtered_servers:
if aServer["domain"][:2] == country_code:
if aServer["hostname"][:2].lower() == country_code:
remaining_servers.append(aServer)
# logger.debug(aServer["domain"])
# logger.debug(aServer["hostname"])
return remaining_servers


Expand Down Expand Up @@ -113,9 +113,9 @@ def filter_by_netflix(json_response, country_code: str) -> List:
server_count += 1
for server in netflix_srv:
for number in range(server[0], server[1] + 1):
if server[2] + str(number) + "." in eachServer["domain"]:
if server[2] + str(number) + "." in eachServer["hostname"]:
remaining_servers.append(eachServer)
# logger.debug(eachServer["domain"])
# logger.debug(eachServer["hostname"])
# logger.debug("Total available servers = ", serverCount)
return remaining_servers

Expand All @@ -130,25 +130,26 @@ def filter_by_type(json_response, p2p: bool, dedicated: bool, double_vpn: bool,

for eachServer in json_response:
server_count += 1
for ServerType in eachServer["categories"]:
if p2p and ServerType["name"] == "P2P":
remaining_servers.append(eachServer)
break
if dedicated and ServerType["name"] == "Dedicated IP":
remaining_servers.append(eachServer)
break
if double_vpn and ServerType["name"] == "Double VPN":
remaining_servers.append(eachServer)
break
if tor_over_vpn and ServerType["name"] == "Onion Over VPN":
remaining_servers.append(eachServer)
break
if anti_ddos and ServerType["name"] == "Obfuscated Servers":
remaining_servers.append(eachServer)
break
if standard_vpn and ServerType["name"] == "Standard VPN servers":
remaining_servers.append(eachServer)
break
if "groups" in eachServer:
for ServerType in eachServer["groups"]:
if p2p and ServerType["title"] == "P2P":
remaining_servers.append(eachServer)
break
if dedicated and ServerType["title"] == "Dedicated IP":
remaining_servers.append(eachServer)
break
if double_vpn and ServerType["title"] == "Double VPN":
remaining_servers.append(eachServer)
break
if tor_over_vpn and ServerType["title"] == "Onion Over VPN":
remaining_servers.append(eachServer)
break
if anti_ddos and ServerType["title"] == "Obfuscated Servers":
remaining_servers.append(eachServer)
break
if standard_vpn and ServerType["title"] == "Standard VPN servers":
remaining_servers.append(eachServer)
break
# logger.debug("Total available servers = ", serverCount)
return remaining_servers

Expand All @@ -157,12 +158,14 @@ def filter_by_protocol(json_res_list: List, tcp: bool) -> List:
remaining_servers = []

for res in json_res_list:
# when connecting using TCP only append if it supports OpenVPN-TCP
if tcp is True and res["features"]["openvpn_tcp"] is True:
remaining_servers.append([res["domain"][:res["domain"].find(".")], res["load"]])
# when connecting using UDP only append if it supports OpenVPN-UDP
elif tcp is False and res["features"]["openvpn_udp"] is True:
remaining_servers.append([res["domain"][:res["domain"].find(".")], res["load"]])
if "technologies" in res:
for tech in res["technologies"]:
# when connecting using TCP only append if it supports OpenVPN-TCP
if tcp and tech["identifier"] == "openvpn_tcp":
remaining_servers.append([res["hostname"][:res["hostname"].find(".")], res["load"]])
# when connecting using UDP only append if it supports OpenVPN-UDP
elif not tcp and tech["identifier"] == "openvpn_udp":
remaining_servers.append([res["hostname"][:res["hostname"].find(".")], res["load"]])
return remaining_servers


Expand Down
2 changes: 1 addition & 1 deletion openpyn/openpyn.py
Original file line number Diff line number Diff line change
Expand Up @@ -1101,7 +1101,7 @@ def display_servers(list_servers: str, port: str, area: str, p2p: bool, dedicate
# add server names to "servers_on_web" set
for res in json_res_list:
print("Server =", res["domain"][:res["domain"].find(".")], ", Load =", res["load"],
", Country =", res["country"], ", Features", res["categories"], "\n")
", Country =", res["country"], ", Features", res["groups"], "\n")
servers_on_web.add(res["domain"][:res["domain"].find(".")])

if not area:
Expand Down