· 4 years ago · Mar 14, 2021, 07:20 AM
1import requests
2import json
3
4### MODIFY HEADERS ###
5# Replace these headers with get request header made to penpencil api by competishun website
6headers = {
7 "authority": "api.penpencil.xyz",
8 "accept": "application/json, text/plain, */*",
9 "user-agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.86 Safari/537.36",
10 "client-id": "COPY CLIENT ID", ### Change required here
11 "client-version": "49",
12 "authorization": "Bearer COPY_BEARER_TOKEN", ### Change required here
13 "client-type": "WEB",
14 "sec-gpc": "1",
15 "origin": "https://competishun.com",
16 "sec-fetch-site": "cross-site",
17 "sec-fetch-mode": "cors",
18 "sec-fetch-dest": "empty",
19 "referer": "https://competishun.com/",
20 "accept-language": "en-US,en;q=0.9",
21}
22
23
24class NegativeItemsLeft(Exception):
25 pass
26
27
28PHYSICS_URL = "https://api.penpencil.xyz/v1/batches/5f8448501fee4800e869b9ee/subjects/5f8448503c837f0018c5a58c/videos"
29CHEM_URL = "https://api.penpencil.xyz/v1/batches/5f8448501fee4800e869b9ee/subjects/5f8448503c837f0018c5a593/videos"
30MATHS_URL = "https://api.penpencil.xyz/v1/batches/5f8448501fee4800e869b9ee/subjects/5f8448503c837f0018c5a59a/videos"
31
32
33params = {"page": 1, "tag": "", "contentType": "notes"}
34
35
36def get_pdf_links(url):
37 pdf_links = {}
38
39 params["page"] = 1
40 c = 1
41 while c < 25: # This is for safety so that the loop doesn't run infinitely
42 # Ideally it shouldn't even run 24 times
43 response = requests.get(url, headers=headers, params=params)
44 js = response.json()
45
46 data = js["data"]
47 if data:
48 for solution in data:
49 topic = solution["homeworkIds"][0]["topic"]
50 key = solution["homeworkIds"][0]["attachmentIds"][0]["key"]
51 base_url = solution["homeworkIds"][0]["attachmentIds"][0]["baseUrl"]
52 direct_pdf_link = requests.compat.urljoin(base_url, key)
53 pdf_links[topic] = direct_pdf_link
54 else:
55 print("Breaking because no data")
56 break
57
58 params["page"] += 1
59 c += 1
60 return pdf_links
61
62
63print("Getting pdf link for physics")
64physics = get_pdf_links(PHYSICS_URL)
65print("Getting pdf link for chemistry")
66chemistry = get_pdf_links(CHEM_URL)
67print("Getting pdf link for maths")
68maths = get_pdf_links(MATHS_URL)
69
70with open("final.txt", "w") as file:
71 for subject in [physics, chemistry, maths]:
72 for (key, value) in subject.items():
73 file.write(key + ":\n")
74 file.write(value + "\n\n")
75 file.write("\n\n\n####### NEW SUBJECT #######\n\n\n")