Exemple #1
0
def viewinfo(request, location):
    if location == "Rome":
        country = rapi.get_countries_by_capital(location)
        country[0] = country[1]
    elif location == "Tokyo":
        country = rapi.get_countries_by_capital(location)
    else:
        country = rapi.get_countries_by_name(location)
    context = {
        "name": country[0].name,
        "capital": country[0].capital,
        "currency": country[0].currencies[0]['name'],
        "population": country[0].population,
        "borders": country[0].borders,
    }
    borders = country[0].borders
    countries = []
    for code in borders:
        country1 = rapi.get_country_by_country_code(code)
        countries.append(country1)
    if countries == []:
        context['borders'] = 0
    else:
        context['borders'] = countries

    return render(request, "tourit/info.html", context)
Exemple #2
0
def test_get_countries_by_region(countries_map):
    """
    Test that countries can be retrieved by region.
    """
    countries = rapi.get_countries_by_region("africa")
    assert sorted(countries) == sorted(countries_map.values())

    countries = rapi.get_countries_by_region("europe")
    assert countries == []
def test_get_countries_by_region_with_filter(countries_map):
    """
    Test that countries can be retrieved by region and the response is filtered.
    """
    countries = rapi.get_countries_by_region("africa", filters=["name"])
    assert sorted(countries) == sorted(countries_map.values())

    countries = rapi.get_countries_by_region("europe", filters=["name"])
    assert countries == []
Exemple #4
0
def test_get_countries_by_calling_code(countries_map, calling_code,
                                       country_name):
    """
    Test that countries can be retrieved by calling code.
    """
    countries = rapi.get_countries_by_calling_code(calling_code)
    assert countries == [countries_map[country_name]]
def test_get_countries_by_subregion_with_filter(countries_map, subregion,
                                                country_name):
    """
    Test that countries can be retrieved by subregion and the response is filtered.
    """
    countries = rapi.get_countries_by_subregion(subregion, filters=["name"])
    assert countries == [countries_map[country_name]]
Exemple #6
0
def test_get_countries_by_name(countries_map, country_name):
    """
    Test that countries can be retrieved by name.
    """
    sanitized_country_name = country_name.replace("_", " ")
    countries = rapi.get_countries_by_name(sanitized_country_name)
    assert countries == [countries_map[country_name]]
Exemple #7
0
def test_get_country_by_country_code(countries_map, country_code,
                                     country_name):
    """
    Test that a single country can be retrieved by its country code.
    """
    country = rapi.get_country_by_country_code(country_code)
    assert country == countries_map[country_name]
def test_get_countries_by_currency_with_filter(countries_map, currency,
                                               country_name):
    """
    Test that countries can be retrieved by currency and the response is filtered.
    """
    countries = rapi.get_countries_by_currency(currency, filters=["name"])
    assert countries == [countries_map[country_name]]
def test_get_countries_by_country_codes_with_filter(nigeria, egypt, kenya):
    """
    Test that multiple countries can be retrieved by multiple country codes and response filtered.
    """
    countries = rapi.get_countries_by_country_codes(
        ["ng", "eg", "ken"], filters=["name", "currencies"])
    assert sorted(countries) == sorted([nigeria, egypt, kenya])
def test_get_country_by_country_code_with_filter(countries_map, country_code,
                                                 country_name):
    """
    Test that a single country can be retrieved by its country code and the response is filtered.
    """
    country = rapi.get_country_by_country_code(country_code,
                                               filters=["name", "capital"])
    assert country == countries_map[country_name]
def test_get_countries_by_name_with_filter(countries_map, country_name):
    """
    Test that countries can be retrieved by name and the response is filtered.
    """
    sanitized_country_name = country_name.replace("_", " ")
    countries = rapi.get_countries_by_name(sanitized_country_name,
                                           filters=["name", "currencies"])
    assert countries == [countries_map[country_name]]
def test_get_countries_by_calling_code_with_filter(countries_map, calling_code,
                                                   country_name):
    """
    Test that countries can be retrieved by calling code and the response is filtered.
    """
    countries = rapi.get_countries_by_calling_code(calling_code,
                                                   filters=["name"])
    assert countries == [countries_map[country_name]]
def test_get_countries_by_capital_with_filter(countries_map, capital,
                                              country_name):
    """
    Test that countries can be retrieved by capital city and the response is filtered.
    """
    countries = rapi.get_countries_by_capital(capital,
                                              filters=["name", "demonym"])
    assert countries == [countries_map[country_name]]
    def run(self, dispatcher: CollectingDispatcher, tracker: Tracker,
            domain: Dict[Text, Any]) -> List[Dict[Text, Any]]:
        country = tracker.get_slot("GPE")

        country_list = rapi.get_countries_by_name(country)
        capital = country_list[0].capital
        dispatcher.utter_message(
            text="Sure. The capital city is {}".format(capital))

        return [SlotSet("capital", capital)]
Exemple #15
0
def country_flag(country):
    if not os.path.exists('tmp'):
        os.makedirs('tmp')
    country_code = COUNTRY_CODES.get(country.title())
    flag_remote = rapi.get_country_by_country_code(COUNTRY_CODES.get(country.title())).flag
    flag_local_svg = 'tmp/{}.svg'.format(country_code)
    flag_local_png = 'tmp/{}.png'.format(country_code)
    urllib.request.urlretrieve(flag_remote, flag_local_svg)
    renderPM.drawToFile(svg2rlg(flag_local_svg), flag_local_png, fmt='PNG')
    flag_pil = Image.open(flag_local_png)
    return flag_pil
def test_get_countries_by_language_with_filter(south_africa, nigeria, kenya):
    """
    Test that countries can be retrieved by language and the response is filtered.
    """
    countries = rapi.get_countries_by_language("en", filters=["name", "flag"])
    assert countries == [south_africa, nigeria, kenya]
Exemple #17
0
def test_get_countries_by_country_codes(nigeria, egypt, kenya):
    """
    Test that multiple countries can be retrieved by multiple country codes.
    """
    countries = rapi.get_countries_by_country_codes(["ng", "eg", "ken"])
    assert sorted(countries) == sorted([nigeria, egypt, kenya])
def test_get_all_with_filter(countries_map):
    """
    Test that a list of all countries can be retrieved and the response is filtered.
    """
    countries = rapi.get_all(filters=["name", "capital"])
    assert sorted(countries) == sorted(countries_map.values())
Exemple #19
0
def test_get_countries_by_subregion(countries_map, subregion, country_name):
    """
    Test that countries can be retrieved by subregion.
    """
    countries = rapi.get_countries_by_subregion(subregion)
    assert countries == [countries_map[country_name]]
Exemple #20
0
def test_get_all(countries_map):
    """
    Test that a list of all countries can be retrieved.
    """
    countries = rapi.get_all()
    assert sorted(countries) == sorted(countries_map.values())
Exemple #21
0
def test_get_countries_by_language(south_africa, nigeria, kenya):
    """
    Test that countries can be retrieved by language.
    """
    countries = rapi.get_countries_by_language("en")
    assert countries == [south_africa, nigeria, kenya]
Exemple #22
0
def extract_publisher_info(source_domain, list_of_websites=None, threshold=95):
    source_domain_data = None
    required_fields = {
        "domain_name", "creation_date", "expiration_date", "status", "name",
        "org", "city", "state", "zipcode", "country", "suffix", "whois_name",
        "whois_country", "nationality"
    }
    try:
        # WhoisIP
        w = whois.whois(source_domain)
        # To dict
        source_domain_data = dict(w)

        # Not all the required information is available
        if not source_domain_data.keys() >= required_fields:
            # 1) Domain name
            if "domain_name" not in source_domain_data.keys():
                source_domain_data["domain_name"] = None
            else:
                if isinstance(source_domain_data["domain_name"], list):
                    source_domain_data["domain_name"] = source_domain_data[
                        "domain_name"][0].lower()

            # 2) Creation date
            if "creation_date" not in source_domain_data.keys():
                source_domain_data["creation_date"] = gv.org_default_field
            else:
                if isinstance(source_domain_data["creation_date"], list):
                    source_domain_data["creation_date"] = source_domain_data[
                        "creation_date"][0]
                # Add time zone utc
                if isinstance(source_domain_data["creation_date"], datetime):
                    d = pytz.UTC.localize(source_domain_data["creation_date"])
                    source_domain_data["creation_date"] = d.strftime(
                        "%Y-%m-%d %H:%M:%S %Z")

            # 3) Expiration date
            if "expiration_date" not in source_domain_data.keys():
                source_domain_data["expiration_date"] = gv.org_default_field
            else:
                if isinstance(source_domain_data["expiration_date"], list):
                    source_domain_data["expiration_date"] = source_domain_data[
                        "expiration_date"][0]
                # Add time zone utc
                if isinstance(source_domain_data["expiration_date"], datetime):
                    d = pytz.UTC.localize(
                        source_domain_data["expiration_date"])
                    source_domain_data["expiration_date"] = d.strftime(
                        "%Y-%m-%d %H:%M:%S %Z")

            # 4) Updated date
            if "updated_date" not in source_domain_data.keys():
                source_domain_data["updated_date"] = gv.org_default_field
            else:
                if isinstance(source_domain_data["updated_date"], list):
                    source_domain_data["updated_date"] = source_domain_data[
                        "creation_date"][0]
                # Add time zone utc
                if isinstance(source_domain_data["updated_date"], datetime):
                    d = pytz.UTC.localize(source_domain_data["updated_date"])
                    source_domain_data["updated_date"] = d.strftime(
                        "%Y-%m-%d %H:%M:%S %Z")

            # 5) String keys
            standard_keys = [
                "status", "name", "org", "city", "state", "zipcode"
            ]
            for i in standard_keys:
                if i not in source_domain_data.keys():
                    source_domain_data[i] = gv.org_default_field
                elif i in source_domain_data.keys(
                ) and source_domain_data[i] is not None:
                    if isinstance(source_domain_data[i], list):
                        source_domain_data[i] = source_domain_data[i][0]
                    source_domain_data[i] = str(source_domain_data[i])
                else:
                    source_domain_data[i] = gv.org_default_field

            # 6) Extract source domain information
            # ttd = tldextract.extract(source_domain)
            tld_extract_obj: TLDExtract = TLDExtract(
                include_psl_private_domains=True)
            ttd: ExtractResult = tld_extract_obj(
                url=source_domain, include_psl_private_domains=True)
            if source_domain_data["domain_name"] is None:
                # tldExtractor
                source_domain_data["domain_name"] = ttd.registered_domain

            # Add a new key for whois name
            source_domain_data["whois_name"] = source_domain_data["name"]

            if list_of_websites is not None:
                source_domain_data["name"] = check_name_publisher(
                    publisher_name=ttd.domain.lower(),
                    organizations_list=list_of_websites,
                    threshold=threshold)
            else:
                source_domain_data["name"] = ttd.domain.upper()
            source_domain_data["suffix"] = ttd.suffix

            # 7) Country and nationality
            country_name: str = top_level_country_domains.get(ttd.suffix, "")
            country_list: list = rapi.get_countries_by_name(country_name)

            if country_list:
                source_domain_data["country"] = country_list[0].name
                source_domain_data["nationality"] = country_list[0].demonym
            else:
                source_domain_data["country"] = gv.org_default_field
                source_domain_data["nationality"] = gv.org_default_field

        # Only keep the required keys
        source_domain_data = dict((key, value)
                                  for key, value in source_domain_data.items()
                                  if key in list(required_fields))
    except Exception as e:
        gv.logger.error(e)
    return source_domain_data
Exemple #23
0
def test_get_countries_by_currency(countries_map, currency, country_name):
    """
    Test that countries can be retrieved by currency.
    """
    countries = rapi.get_countries_by_currency(currency)
    assert countries == [countries_map[country_name]]
Exemple #24
0
from restcountries import RestCountryApiV2 as rc
countries_list = rc.get_all()

for c in countries_list:
    print(c.name, "-", c.capital, "-", end="")

    for curr in c.currencies:
        code, name, symbol = curr.items()
        print(code, name, symbol, end="")
    print()
Exemple #25
0
def test_get_countries_by_capital(countries_map, capital, country_name):
    """
    Test that countries can be retrieved by capital city.
    """
    countries = rapi.get_countries_by_capital(capital)
    assert countries == [countries_map[country_name]]
Exemple #26
0
from restcountries import RestCountryApiV2 as rc

# for c in rc.get_countries_by_name("india"):
#     print(type(c))
#     print(dir(c))

for c in rc.get_all():
    print(c.name, ' - ', c.capital)