KartikTalwar / Duolingo

Unofficial Duolingo API Written in Python
MIT License
822 stars 129 forks source link

implement method to get data by user ID #92

Closed igorskh closed 4 years ago

igorskh commented 4 years ago

This pull request implements getting user data from https://www.duolingo.com/2017-06-30/users/{self.user_data.id}.

This URL is currently used by Duolingo web app, also it is used in this library in the get_daily_xp_progress. However, it seems reasonable to have it implemented in a generic way to give users opportunity to request different data.

The reason for introducing fields is that the response is generally quite large, 760 bytes in my case and takes about 3 seconds to receive. One option is to get data on initialisation, but it is dynamic, for example after purchasing an item it has to be requested again.

It should also help users to solve #91 while keeping library code purely for an API interaction, sample code for this:

import duolingo

lingo = duolingo.Duolingo('I2UpM5so', '<password>')

user_data_resp = lingo._get_data_by_user_id(['shopItems'])

search_for = 'streak_freeze'
shop_items = list(filter(
    lambda item: item["id"] == search_for,
    user_data_resp['shopItems']
))

item = shop_items[0] if shop_items else None
if item and item["quantity"]:
    print(f'Current quantity of {search_for}: {item["quantity"]}')

Sample output:

Current quantity of streak_freeze: 2
igorskh commented 4 years ago

@joshcoales Thanks for comments, I've committed the changes you suggested.

Do you think the implementation of get_daily_xp_progress should be changed to use this method?

SpangleLabs commented 4 years ago

Looks good!