Skip to content

Commit

Permalink
Merge pull request #42 from TRoboto/fixbug
Browse files Browse the repository at this point in the history
Fix list commands
  • Loading branch information
TRoboto authored Aug 13, 2021
2 parents 2cdd545 + c232496 commit 3385e7b
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 29 deletions.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

setup(
name="datacamp-downloader",
version="3.0",
version="3.1",
author="Mohammad Al-Fetyani",
author_email="m4bh@hotmail.com",
description="Download your completed courses on Datacamp easily!",
Expand Down
2 changes: 1 addition & 1 deletion src/datacamp_downloader/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
LOGIN_URL = "https://www.datacamp.com/users/sign_in"
LOGIN_DETAILS_URL = "https://www.datacamp.com/api/users/signed_in"

SESSION_FILE = tempfile.gettempdir() + "/.datacamp.v2"
SESSION_FILE = tempfile.gettempdir() + "/.datacamp.v3"

PROFILE_URL = "https://www.datacamp.com/profile/{slug}"
COURSE_DETAILS_API = "https://campus-api.datacamp.com/api/courses/{id}/"
Expand Down
52 changes: 27 additions & 25 deletions src/datacamp_downloader/datacamp_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ def init(self):
self.has_active_subscription = False
self.loggedin = False
self.login_data = None
self.profile_data = None

self.courses = []
self.tracks = []
Expand Down Expand Up @@ -145,6 +146,16 @@ def set_token(self, token):
self.session.add_token(token)
self._set_profile()

def get_profile_data(self):
if not self.profile_data:
profile = self.session.get(PROFILE_URL.format(slug=self.login_data["slug"]))
self.session.driver.minimize_window()
soup = BeautifulSoup(profile, "html.parser")
self.profile_data = self.session.to_json(
soup.find(id="__NEXT_DATA__").string
)
return self.profile_data

@login_required
@animate_wait
def list_completed_tracks(self, refresh):
Expand All @@ -167,12 +178,13 @@ def list_completed_courses(self, refresh):
table.add_row(["ID", "Title", "Datasets", "Exercises", "Videos"])
table_so_far = table.draw()
Logger.clear_and_print(table_so_far)
for course in self.get_completed_courses(refresh):
for i, course in enumerate(self.get_completed_courses(refresh), 1):
all_exercises_count = sum([c.nb_exercises for c in course.chapters])
videos_count = sum([c.number_of_videos for c in course.chapters])
course.order = i
table.add_row(
[
course.order,
i,
course.title,
len(course.datasets),
all_exercises_count - videos_count,
Expand Down Expand Up @@ -366,23 +378,10 @@ def get_completed_tracks(self, refresh=False):

self.tracks = []

profile = self.session.get(PROFILE_URL.format(slug=self.login_data["slug"]))
self.session.driver.minimize_window()

soup = BeautifulSoup(profile, "html.parser")
tracks_title = soup.findAll("div", {"class": "track-block__main"})
tracks_link = soup.findAll(
"a", {"href": re.compile("^/tracks"), "class": "shim"}
)
for i in range(len(tracks_link)):
link = "https://www.datacamp.com" + tracks_link[i]["href"]
self.tracks.append(
Track(
f"t{i + 1}",
tracks_title[i].getText().replace("\n", " ").strip(),
link,
)
)
data = self.get_profile_data()
completed_tracks = data["props"]["pageProps"]["completed_tracks"]
for i, track in enumerate(completed_tracks, 1):
self.tracks.append(Track(f"t{i}", track["title"].strip(), track["url"]))
all_courses = set()
# add courses
for track in self.tracks:
Expand All @@ -407,11 +406,15 @@ def get_completed_courses(self, refresh=False):

self.courses = []

for course in self._get_courses_from_link(
PROFILE_URL.format(slug=self.login_data["slug"])
):
self.courses.append(course)
yield course
data = self.get_profile_data()
completed_courses = data["props"]["pageProps"]["completed_courses"]
for course in completed_courses:
fetched_course = self.get_course(course["id"])
if not fetched_course:
continue
self.session.driver.minimize_window()
self.courses.append(fetched_course)
yield fetched_course

if not self.courses:
return []
Expand Down Expand Up @@ -461,7 +464,6 @@ def _get_courses_from_link(self, link: str):
continue
course = self.get_course(int(id))
if course:
course.order = i
yield course

def _get_chapter_name(self, chapter: Chapter):
Expand Down
2 changes: 1 addition & 1 deletion src/datacamp_downloader/downloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from .helper import Logger
from .templates.lang import Language

__version__ = "3.0.0"
__version__ = "3.1.0"


def version_callback(value: bool):
Expand Down
5 changes: 4 additions & 1 deletion src/datacamp_downloader/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,12 @@ def get(self, url):
def get_json(self, url):
page = self.get(url)
page = self.driver.find_element(By.TAG_NAME, "pre").text
parsed_json = json.loads(page)
parsed_json = self.to_json(page)
return parsed_json

def to_json(self, page: str):
return json.loads(page)

def get_element_by_id(self, id: str) -> WebElement:
return self.driver.find_element(By.ID, id)

Expand Down

0 comments on commit 3385e7b

Please sign in to comment.