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

View File

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