use tldextract to extract hostname

This commit is contained in:
Son 2021-10-18 11:45:48 +02:00
parent af221998f3
commit 462164ff16
2 changed files with 16 additions and 16 deletions

View File

@ -1,3 +1,4 @@
import tldextract
from flask import jsonify, request, g
from sqlalchemy import desc
@ -62,12 +63,10 @@ def options_v4():
if hostname:
# keep only the domain name of hostname, ignore TLD and subdomain
# for ex www.groupon.com -> groupon
domain_name = hostname
if "." in hostname:
parts = hostname.split(".")
domain_name = parts[-2]
domain_name = convert_to_id(domain_name)
ret["prefix_suggestion"] = domain_name
ext = tldextract.extract(hostname)
prefix_suggestion = ext.domain
prefix_suggestion = convert_to_id(prefix_suggestion)
ret["prefix_suggestion"] = prefix_suggestion
suffixes = get_available_suffixes(user)
@ -133,12 +132,10 @@ def options_v5():
if hostname:
# keep only the domain name of hostname, ignore TLD and subdomain
# for ex www.groupon.com -> groupon
domain_name = hostname
if "." in hostname:
parts = hostname.split(".")
domain_name = parts[-2]
domain_name = convert_to_id(domain_name)
ret["prefix_suggestion"] = domain_name
ext = tldextract.extract(hostname)
prefix_suggestion = ext.domain
prefix_suggestion = convert_to_id(prefix_suggestion)
ret["prefix_suggestion"] = prefix_suggestion
suffixes = get_available_suffixes(user)

View File

@ -1,5 +1,3 @@
import json
from flask import url_for
from app.db import Session
@ -132,10 +130,15 @@ def test_different_scenarios_v5(flask_client):
"/api/v5/alias/options?hostname=www.test.com",
headers={"Authentication": api_key.code},
)
print(json.dumps(r.json, indent=2))
assert r.json["prefix_suggestion"] == "test"
# <<< with hostname with 2 parts TLD, for example wwww.numberoneshoes.co.nz >>>
r = flask_client.get(
"/api/v5/alias/options?hostname=wwww.numberoneshoes.co.nz",
headers={"Authentication": api_key.code},
)
assert r.json["prefix_suggestion"] == "numberoneshoes"
# <<< with recommendation >>>
alias = Alias.create_new(user, prefix="test")
Session.commit()