Skip to content

Fetching data through State-Province #620

@tarunbisht-24

Description

@tarunbisht-24

There's a problem when you send the request based on state province like if someone wants to get all the universities based on the state.

URL:- http://universities.hipolabs.com/search?name=united+states&state-province=Alaska

Also, if someone wants universities based on country, then state-wise to narrow down the search.

URL:- http://universities.hipolabs.com/search?name=india&country=India&state-province=Assam

Solution (to change the filter.py file based on state-province):-

import json
import sys


def filter_by_country_and_state(data, country=None, state_province=None):
    def filter(entry):
        country_match = country is None or entry["country"].lower() == country.lower()
        state_match = state_province is None or (entry["state-province"] and entry["state-province"].lower() == state_province.lower())
        return country_match and state_match

    return [entry for entry in data if filter(entry)]


def main():
    args = sys.argv[1:]
    
    if len(args) == 0:
        print("Usage: filter.py <country/state-province> [state-province]")
        return

    # Load the source data
    with open("./world_universities_and_domains.json", "r", encoding="utf-8") as src_file:
        data = json.load(src_file)

    # Check if the first argument is a country or a state/province
    country_or_state = args[0]
    state_province = args[1] if len(args) > 1 else None

    # If the first argument matches any country in the data, treat it as a country
    countries = set(entry["country"].lower() for entry in data)
    if country_or_state.lower() in countries:
        country = country_or_state
    else:
        country = None
        state_province = country_or_state

    # Filter the data
    filtered_data = filter_by_country_and_state(data, country, state_province)

    # Write the filtered result
    with open("./filtered_world_universities_and_domains.json", "w", encoding="utf-8") as dest_file:
        json.dump(filtered_data, dest_file, ensure_ascii=False, indent=4)


if __name__ == "__main__":
    main()

Also why the request uses HTTP Protocol instead of HTTPS?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions