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

🐛 Handle missing 'course' key in get_course_id to prevent KeyError #178

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ duce.py
new_enroll_test.py
data.json
gui-test.py
.venv/
48 changes: 34 additions & 14 deletions base.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import time
import traceback
from datetime import datetime, timezone
from decimal import Decimal
from decimal import Decimal, InvalidOperation
from urllib.parse import parse_qs, unquote, urlparse, urlsplit, urlunparse

import cloudscraper
Expand Down Expand Up @@ -740,29 +740,43 @@ def get_course_id(self, url):
url = re.sub(r"\W+$", "", unquote(url))
try:
r = self.client.get(url)
r.raise_for_status()
except requests.exceptions.ConnectionError:
if self.debug:
print(r.text)
print("Connection error:", url)
return "retry", url, False
except requests.exceptions.HTTPError as e:
if self.debug:
print(f"HTTP error {e.response.status_code}: {e}")
return "invalid", url, False

soup = bs(r.content, "html5lib")
if self.debug:
with open("test/soup.html", "w", encoding="utf-8") as f:
f.write(str(soup))
course_id = soup.find("body").get("data-clp-course-id", "invalid")
if course_id == "invalid":
return "invalid", url, False

dma = json.loads(soup.find("body")["data-module-args"])
if self.debug:
with open("test/dma.json", "w") as f:
json.dump(dma, f, indent=4)
try:
dma = json.loads(soup.find("body")["data-module-args"])
if self.debug:
with open("test/dma.json", "w") as f:
json.dump(dma, f, indent=4)
course_info = dma.get("serverSideProps", {}).get("course", None)
if not course_info:
if self.debug:
print("Course information is missing in serverSideProps")
return "invalid", url, False

course_id = soup.find("body").get("data-clp-course-id", "invalid")
is_free = not course_info.get("isPaid", True)
if not self.debug and self.is_course_excluded(dma):
return "excluded", r.url, False

is_free = not dma["serverSideProps"]["course"].get("isPaid", True)
if not self.debug and self.is_course_excluded(dma):
return "excluded", r.url, False
return course_id, r.url, is_free

return course_id, r.url, is_free
except (KeyError, TypeError, json.JSONDecodeError):
if self.debug:
print("Error parsing course data:", traceback.format_exc())
return "invalid", url, False

def is_course_excluded(self, dma):
instructors = [
Expand Down Expand Up @@ -819,7 +833,13 @@ def check_course(self, course_id, coupon_code=None):
status = r["redeem_coupon"]["discount_attempts"][0]["status"]
coupon_valid = discount == 100 and status == "applied"

return Decimal(amount), coupon_valid
# can be "retry" or Decimal
try:
amount = Decimal(amount)
except (InvalidOperation, ValueError):
amount = "retry"

return amount, coupon_valid

def start_enrolling(self):
self.remove_duplicate_courses()
Expand Down