sweemeng / cuaca

A wrapper for the Malaysian Weather Service API
BSD 3-Clause "New" or "Revised" License
8 stars 4 forks source link

The api.locations function cannot retrieve all of the towns as well as districts of all states in Malaysia. #14

Open mingjun1120 opened 2 years ago

mingjun1120 commented 2 years ago

When I tried to pass in "DISTRICT" or "TOWN" as an argument into the api.locations() function, it cannot retrieve all of the districts as well as towns of each state. Below are outputs that I received when I call this function by passing "DISTRICT" and "TOWN" as arguments:

import cuaca
api = cuaca.WeatherAPI("YOUR_API_KEY")
districts = api.locations("DISTRICT")
df_district = pd.DataFrame(districts)

state_dct = {}
for lotrootid in df_district['locationrootid'].unique().tolist():
    state_dct[lotrootid] = df_state['name'][int(lotrootid[-1]) - 1]

print(state_dct)

def swap_columns(df, col1, col2):
    col_list = list(df.columns)
    x, y = col_list.index(col1), col_list.index(col2)
    col_list[y], col_list[x] = col_list[x], col_list[y]
    df = df[col_list]
    return df

df_district['locationrootid'] = df_district['locationrootid'].replace(state_dct)
df_district.rename(columns = {'locationrootid':'state'}, inplace = True)
df_district.drop(labels=['locationcategoryid'], axis=1, inplace=True)
df_district = swap_columns(df_district, 'id', 'state')

df_district_index = df_district.set_index(keys=['state', 'name', 'id', 'latitude', 'longitude'])#.swaplevel(0, 1)
df_district_index

Output: image image

towns = api.locations("TOWN")
df_towns = pd.DataFrame(towns)
df_towns['locationrootid'] = df_towns['locationrootid'].replace(state_dct)
df_towns.rename(columns = {'locationrootid':'state'}, inplace = True)
df_towns.drop(labels=['locationcategoryid'], axis=1, inplace=True)
df_towns = swap_columns(df_towns, 'id', 'state')

df_towns_index = df_towns.set_index(keys=['state', 'name', 'id', 'latitude', 'longitude'])#.swaplevel(0, 1)
df_towns_index

Output: image image

mingjun1120 commented 2 years ago

@sweemeng Do you face this issue when you?

sweemeng commented 2 years ago

I introduced a parameter called pages. I didn't put it in the docs. https://github.com/sweemeng/cuaca/blob/master/cuaca/api.py#L86

locations(self, location_type, page=0). I don't know how many page exist, but i put a big number and it returns empty.

sweemeng commented 2 years ago

In short, try.

page = 0
districts = api.locations("DISTRICT", page)

you will have to use a while loop to generate the page you need. See if the API make sense. It start from page 0, so i default it to 0

sweemeng commented 2 years ago

The default amount is 50, so in page 2, it should have the next 50. It just makes the math easier to deal with

mingjun1120 commented 2 years ago

I see. Thanks for updating me. Since we don't know the maximum amount of DISTRICTS and TOWNS available, we can keep looping to retrieve all the districts and towns available until throw an exception. Then, just wrap it with try except to solve the problem. I will try this way tmr and update you.

On Wed, Aug 10, 2022, 19:52 sweemeng @.***> wrote:

The default amount is 50, so in page 2, it should have the next 50. It just makes the math easier to deal with

— Reply to this email directly, view it on GitHub https://github.com/sweemeng/cuaca/issues/14#issuecomment-1210567039, or unsubscribe https://github.com/notifications/unsubscribe-auth/AM5A4AAFCRDSPKA5KQTAEGLVYOJXLANCNFSM56D3LU5A . You are receiving this because you authored the thread.Message ID: @.***>