Skip to content

Commit

Permalink
fix: Update Udemy checkout process for improved error handling and mo…
Browse files Browse the repository at this point in the history
…dify CLI settings for additional sites
  • Loading branch information
techtanic committed Nov 2, 2024
1 parent 6296aca commit 57fdae1
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 17 deletions.
45 changes: 30 additions & 15 deletions base.py
Original file line number Diff line number Diff line change
Expand Up @@ -940,7 +940,7 @@ def discounted_checkout(self, coupon, course_id) -> dict:
"price": {"amount": 0, "currency": self.currency.upper()},
}
],
"is_cart": False,
"is_cart": True,
},
}
headers = {
Expand Down Expand Up @@ -973,13 +973,17 @@ def discounted_checkout(self, coupon, course_id) -> dict:
else:
raise ValueError("CSRF token not found")

r: dict = self.client.post(
r = self.client.post(
"https://www.udemy.com/payment/checkout-submit/",
json=payload,
headers=headers,
).json()

return {"status": r.get("status") == "succeeded", "detail": r.get("detail")}
)
try:
r = r.json()
except:
self.print(r.text, color="red")
self.print("Unknown Error: Report this to the developer", color="red")
return r

def free_checkout(self, course_id):
self.client.get(f"https://www.udemy.com/course/subscribe/?courseId={course_id}")
Expand All @@ -1003,15 +1007,8 @@ def handle_discounted_course(self, course_id):

def process_coupon(self, course_id, coupon_code, amount):
checkout_response = self.discounted_checkout(coupon_code, course_id)
if checkout_response["status"]:
self.print("Successfully Enrolled To Course :)", color="green")
self.successfully_enrolled_c += 1
self.enrolled_courses[course_id] = self.get_now_to_utc()
self.amount_saved_c += amount
self.save_course()
time.sleep(3.7)
else:
self.print(checkout_response["detail"], color="light blue")
if msg:=checkout_response.get("detail"):
self.print(msg, color="red")
try:
wait_time = int(re.search(r"\d+", checkout_response["detail"]).group(0))
except:
Expand All @@ -1020,6 +1017,24 @@ def process_coupon(self, course_id, coupon_code, amount):
)
self.print(checkout_response, color="red")
wait_time = 60
self.print(f">>> Pausing script for {wait_time} seconds\n", color="red")
time.sleep(wait_time + 1.5)
self.process_coupon(course_id, coupon_code, amount)
elif checkout_response["status"] == "succeeded":
self.print("Successfully Enrolled To Course :)", color="green")
self.successfully_enrolled_c += 1
self.enrolled_courses[course_id] = self.get_now_to_utc()
self.amount_saved_c += amount
self.save_course()
time.sleep(3.75)
elif checkout_response["status"] == "failed":
message = checkout_response["message"]
if "item_already_subscribed" in message:
self.print("Already Enrolled", color="light blue")
self.already_enrolled_c += 1
else:
self.print("Unknown Error: Report this to the developer", color="red")
self.print(checkout_response)
else:
self.print("Unknown Error: Report this to the developer", color="red")
self.print(checkout_response)

4 changes: 2 additions & 2 deletions duce-cli-settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@
},
"sites": {
"Real Discount": true,
"Discudemy": false,
"IDownloadCoupons": false,
"Discudemy": true,
"IDownloadCoupons": true,
"Tutorial Bar": false,
"E-next": false,
"Course Vania": false,
Expand Down

0 comments on commit 57fdae1

Please sign in to comment.