Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding set course functionality #93

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ Note: You are now required to provide a password to get any data from the Duolin
- [Buy Streak Freeze](#buy-streak-freeze)
###### Switch account being read
- [Set username](#set-username)
###### Switch current course
- [Set course](#set-course)
###### Language Information
- [Get Language Details](#get-language-details)
- [Get Language Progress](#get-language-progress)
Expand Down Expand Up @@ -272,6 +274,25 @@ print(lingo.get_languages())
['French']
```

#### Set course
`lingo.set_course(new_from_language, new_learning_language)`

Sets the users current course, and reloads user data. This will allow you to read data about the new course.
The function will allow you to swap to a course you are not currently taking, or one you are.
This will require you to be signed in as the original user. Returns True if successful, false otherwise.
```py
# Sample Request
lingo = Duolingo("kartik","...")
print(lingo.set_course("en","es"))

```
##### Parameters
`new_from_language` (string) **required**
-- The language you already know that you are learning from. Needs to be the abbreviation.

`new_learning_language` (string) **required**
-- The language you are learning. Also needs to be the abbreviation.

#### Get Language Details
`lingo.get_language_details(language_name)`

Expand Down
25 changes: 22 additions & 3 deletions duolingo.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,12 @@ def __init__(self, username, password=None, *, jwt=None, session_file=None):
self.user_data = Struct(**self._get_data())
self.voice_url_dict = None

def _make_req(self, url, data=None):
def _make_req(self, url, data=None, mode=None):
headers = {}
if self.jwt is not None:
headers['Authorization'] = 'Bearer ' + self.jwt
headers['User-Agent'] = self.USER_AGENT
req = requests.Request('POST' if data else 'GET',
req = requests.Request(mode if mode else ('POST' if data else 'GET'),
url,
json=data,
headers=headers,
Expand Down Expand Up @@ -136,6 +136,22 @@ def set_username(self, username):
self.username = username
self.user_data = Struct(**self._get_data())

def set_course(self, new_from_language, new_learning_language):
"""
Changes the current course a user is using
:param new_from_language: The language you already know that you are learning from needs to be the abbreviation
:param new_learning_language: The language you are learning needs to be the abbreviation
"""
user_id = self.get_user_id()
data = {
"fromLanguage": new_from_language,
"learningLanguage": new_learning_language}
language_url = "https://www.duolingo.com/2017-06-30/users/%d?fields=courses,currentCourse,fromLanguage,learningLanguage" % (
user_id)
response = self._make_req(language_url, data, mode="PATCH")
self.user_data = Struct(**self._get_data())
return response.ok

def get_leaderboard(self, unit, before):
"""
Get user's rank in the week in descending order, stream from
Expand Down Expand Up @@ -361,6 +377,10 @@ def get_user_info(self):

return self._make_dict(fields, self.user_data)

def get_user_id(self):
"""Get user's id."""
return getattr(self.user_data, "id", None)

def get_streak_info(self):
"""Get user's streak informations."""
fields = ['daily_goal', 'site_streak', 'streak_extended_today']
Expand Down Expand Up @@ -611,7 +631,6 @@ def get_related_words(self, word, language_abbr=None):
return [w for w in overview['vocab_overview']
if w['lexeme_id'] in related_lexemes]


def get_word_definition_by_id(self, lexeme_id):
"""
Get the dictionary entry from
Expand Down