Skip to content

Commit

Permalink
fix: Update headers and error handling in Udemy class; modify CLI set…
Browse files Browse the repository at this point in the history
…tings
  • Loading branch information
techtanic committed Nov 2, 2024
1 parent a392f21 commit 6296aca
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 15 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ tempCodeRunnerFile.py
DUCE-GUI-windows.spec
duce.py
gui-test.py
duce-cli-settings.json
duce-gui-settings.json
duce-cli-settings.json
50 changes: 42 additions & 8 deletions base.py
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,9 @@ def load_settings(self):
with open(f"duce-{self.interface}-settings.json") as f:
self.settings = json.load(f)
except FileNotFoundError:
with open(resource_path(f"default-duce-{self.interface}-settings.json")) as f:
with open(
resource_path(f"default-duce-{self.interface}-settings.json")
) as f:
self.settings = json.load(f)
if (
self.interface == "cli" and "use_browser_cookies" not in self.settings
Expand Down Expand Up @@ -937,15 +939,45 @@ def discounted_checkout(self, coupon, course_id) -> dict:
"discountInfo": {"code": coupon},
"price": {"amount": 0, "currency": self.currency.upper()},
}
]
],
"is_cart": False,
},
}
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:132.0) Gecko/20100101 Firefox/132.0",
"Accept": "application/json, text/plain, */*",
"Accept-Language": "en-US",
"Referer": f"https://www.udemy.com/payment/checkout/express/course/{course_id}/?discountCode={coupon}",
"Content-Type": "application/json",
"X-Requested-With": "XMLHttpRequest",
"x-checkout-is-mobile-app": "false",
"Origin": "https://www.udemy.com",
"DNT": "1",
"Sec-GPC": "1",
"Connection": "keep-alive",
"Sec-Fetch-Dest": "empty",
"Sec-Fetch-Mode": "cors",
"Sec-Fetch-Site": "same-origin",
"Priority": "u=0",
"Pragma": "no-cache",
"Cache-Control": "no-cache",
}
csrftoken = None
for cookie in self.client.cookies:
if cookie.name == "csrftoken":
csrftoken = cookie.value
break

if csrftoken:
headers["X-CSRFToken"] = csrftoken
else:
raise ValueError("CSRF token not found")

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

return {"status": r.get("status") == "succeeded", "detail": r.get("detail")}

Expand Down Expand Up @@ -980,12 +1012,14 @@ def process_coupon(self, course_id, coupon_code, amount):
time.sleep(3.7)
else:
self.print(checkout_response["detail"], color="light blue")
try:
try:
wait_time = int(re.search(r"\d+", checkout_response["detail"]).group(0))
except:
self.print("Unknown Error: Report this link to the developer", color="red")
self.print(
"Unknown Error: Report this link to the developer", color="red"
)
self.print(checkout_response, color="red")
wait_time = 60
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)
12 changes: 6 additions & 6 deletions duce-cli-settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@
},
"sites": {
"Real Discount": true,
"Discudemy": true,
"IDownloadCoupons": true,
"Tutorial Bar": true,
"E-next": true,
"Course Vania": true,
"Udemy Freebies": true
"Discudemy": false,
"IDownloadCoupons": false,
"Tutorial Bar": false,
"E-next": false,
"Course Vania": false,
"Udemy Freebies": false
},
"min_rating": 0.0,
"instructor_exclude": [
Expand Down

0 comments on commit 6296aca

Please sign in to comment.