Skip to content
Snippets Groups Projects
Commit e1f93f55 authored by tuhe's avatar tuhe
Browse files

First commit

parents
No related branches found
No related tags found
No related merge requests found
Pipeline #9143 failed
secrets.py
server.pem
\ No newline at end of file
image: python:3.8
#
#.install_firefox_geckodriver: &install_firefox_geckodriver
before_script:
- TZ=America/Sao_Paulo # Numeric inputs: 2 - 134.
- ln -snf /usr/share/zoneinfo/$TZ /etc/localtime
- echo $TZ > /etc/timezone
# Add the needed libraries.
- apt update && apt -y upgrade
# - apt-get update -y
# - apt install -y python3-pip python3.10 python-is-python3
- apt -y install curl wget
- apt install -y xvfb libssl-dev openssl # wget firefox
- openssl req -new -x509 -keyout server.pem -out server.pem -days 365 -nodes -batch
- apt-get install gconf-service libasound2 libatk1.0-0 libc6 libcairo2 libcups2 libdbus-1-3 libexpat1 libfontconfig1 libgcc1 libgconf-2-4 libgdk-pixbuf2.0-0 libglib2.0-0 libgtk-3-0 libnspr4 libpango-1.0-0 libpangocairo-1.0-0 libstdc++6 libx11-6 libx11-xcb1 libxcb1 libxcomposite1 libxcursor1 libxdamage1 libxext6 libxfixes3 libxi6 libxrandr2 libxrender1 libxss1 libxtst6 ca-certificates fonts-liberation libappindicator1 libnss3 lsb-release xdg-utils --assume-yes
- wget -nv -O ~/FirefoxSetup.tar.bz2 "https://download.mozilla.org/?product=firefox-latest&os=linux64"
- tar xjf ~/FirefoxSetup.tar.bz2 -C /opt/
- ln -s /opt/firefox/firefox /usr/lib/firefox
- export PATH=$PATH:/opt/firefox/
- wget -nv -O ~/geckodriver.tar.gz "https://github.com/mozilla/geckodriver/releases/download/v0.31.0/geckodriver-v0.31.0-linux64.tar.gz"
- tar -zxvf ~/geckodriver.tar.gz -C /opt/
- export PATH=$PATH:/opt/
# - sudo -H pip3 install bpython selenium
# - apt-get update # && apt-get upgrade --assume-yes
# - apt-get install gconf-service libasound2 libatk1.0-0 libc6 libcairo2 libcups2 libdbus-1-3 libexpat1 libfontconfig1 libgcc1 libgconf-2-4 libgdk-pixbuf2.0-0 libglib2.0-0 libgtk-3-0 libnspr4 libpango-1.0-0 libpangocairo-1.0-0 libstdc++6 libx11-6 libx11-xcb1 libxcb1 libxcomposite1 libxcursor1 libxdamage1 libxext6 libxfixes3 libxi6 libxrandr2 libxrender1 libxss1 libxtst6 ca-certificates fonts-liberation libappindicator1 libnss3 lsb-release xdg-utils --assume-yes
# - wget -nv -O ~/FirefoxSetup.tar.bz2 "https://download.mozilla.org/?product=firefox-latest&os=linux64"
# - tar xjf ~/FirefoxSetup.tar.bz2 -C /opt/
# - ln -s /opt/firefox/firefox /usr/lib/firefox
# - export PATH=$PATH:/opt/firefox/
# - wget -nv -O ~/geckodriver.tar.gz "https://github.com/mozilla/geckodriver/releases/download/v0.28.0/geckodriver-v0.28.0-linux64.tar.gz"
# - tar -zxvf ~/geckodriver.tar.gz -C /opt/
# - export PATH=$PATH:/opt/
- pip install -r requirements.txt
# - export GECKO_DRIVER_VERSION='v0.31.0'
# - wget https://github.com/mozilla/geckodriver/releases/download/$GECKO_DRIVER_VERSION/geckodriver-$GECKO_DRIVER_VERSION-linux64.tar.gz
# - tar -xvzf geckodriver-$GECKO_DRIVER_VERSION-linux64.tar.gz
# - rm geckodriver-$GECKO_DRIVER_VERSION-linux64.tar.gz
# - chmod +x geckodriver
# - cp geckodriver /usr/local/bin/
# - 'command -v ssh-agent >/dev/null || ( apt-get update -y && apt-get install openssh-client -y )'
##
## Run ssh-agent (inside the build environment)
##
# - eval $(ssh-agent -s)
##
## Add the SSH key stored in SSH_PRIVATE_KEY variable to the agent store
## We're using tr to fix line endings which makes ed25519 keys work
## without extra base64 encoding.
## https://gitlab.com/gitlab-examples/ssh-private-key/issues/1#note_48526556
##
# - echo "$SSH_PRIVATE_KEY" | tr -d '\r' | ssh-add -
- echo "$client_secret" > src/unlearn/client_secret.txt
- echo "$client_id" > src/unlearn/client_id.txt
- echo "$username" > src/unlearn/username.txt
- echo "$password" > src/unlearn/password.txt
- cat src/unlearn/client_secret.txt
- cat src/unlearn/client_id.txt
- cat src/unlearn/username.txt
- cat src/unlearn/password.txt
##
## Create the SSH directory and give it the right permissions
##
# - mkdir -p ~/.ssh
# - chmod 700 ~/.ssh
pages:
stage: deploy
script:
- pwd
- cd src
- xvfb-run -s "-screen 0 1400x900x24" python -m unlearn.tokenpage
# - python -m unlearn.tokenpage
- cd ..
- ls
- pwd
artifacts:
paths:
- public
# unlearn
## Getting started
To make it easy for you to get started with GitLab, here's a list of recommended next steps.
Already a pro? Just edit this README.md and make it your own. Want to make it easy? [Use the template at the bottom](#editing-this-readme)!
## Add your files
- [ ] [Create](https://docs.gitlab.com/ee/user/project/repository/web_editor.html#create-a-file) or [upload](https://docs.gitlab.com/ee/user/project/repository/web_editor.html#upload-a-file) files
- [ ] [Add files using the command line](https://docs.gitlab.com/ee/gitlab-basics/add-file.html#add-a-file-using-the-command-line) or push an existing Git repository with the following command:
```
cd existing_repo
git remote add origin https://lab.compute.dtu.dk/tuhe/unlearn.git
git branch -M main
git push -uf origin main
```
## Integrate with your tools
- [ ] [Set up project integrations](https://lab.compute.dtu.dk/tuhe/unlearn/-/settings/integrations)
## Collaborate with your team
- [ ] [Invite team members and collaborators](https://docs.gitlab.com/ee/user/project/members/)
- [ ] [Create a new merge request](https://docs.gitlab.com/ee/user/project/merge_requests/creating_merge_requests.html)
- [ ] [Automatically close issues from merge requests](https://docs.gitlab.com/ee/user/project/issues/managing_issues.html#closing-issues-automatically)
- [ ] [Enable merge request approvals](https://docs.gitlab.com/ee/user/project/merge_requests/approvals/)
- [ ] [Automatically merge when pipeline succeeds](https://docs.gitlab.com/ee/user/project/merge_requests/merge_when_pipeline_succeeds.html)
## Test and Deploy
Use the built-in continuous integration in GitLab.
- [ ] [Get started with GitLab CI/CD](https://docs.gitlab.com/ee/ci/quick_start/index.html)
- [ ] [Analyze your code for known vulnerabilities with Static Application Security Testing(SAST)](https://docs.gitlab.com/ee/user/application_security/sast/)
- [ ] [Deploy to Kubernetes, Amazon EC2, or Amazon ECS using Auto Deploy](https://docs.gitlab.com/ee/topics/autodevops/requirements.html)
- [ ] [Use pull-based deployments for improved Kubernetes management](https://docs.gitlab.com/ee/user/clusters/agent/)
- [ ] [Set up protected environments](https://docs.gitlab.com/ee/ci/environments/protected_environments.html)
***
# Editing this README
When you're ready to make this README your own, just edit this file and use the handy template below (or feel free to structure it however you want - this is just a starting point!). Thank you to [makeareadme.com](https://www.makeareadme.com/) for this template.
## Suggestions for a good README
Every project is different, so consider which of these sections apply to yours. The sections used in the template are suggestions for most open source projects. Also keep in mind that while a README can be too long and detailed, too long is better than too short. If you think your README is too long, consider utilizing another form of documentation rather than cutting out information.
## Name
Choose a self-explaining name for your project.
## Description
Let people know what your project can do specifically. Provide context and add a link to any reference visitors might be unfamiliar with. A list of Features or a Background subsection can also be added here. If there are alternatives to your project, this is a good place to list differentiating factors.
## Badges
On some READMEs, you may see small images that convey metadata, such as whether or not all the tests are passing for the project. You can use Shields to add some to your README. Many services also have instructions for adding a badge.
## Visuals
Depending on what you are making, it can be a good idea to include screenshots or even a video (you'll frequently see GIFs rather than actual videos). Tools like ttygif can help, but check out Asciinema for a more sophisticated method.
## Installation
Within a particular ecosystem, there may be a common way of installing things, such as using Yarn, NuGet, or Homebrew. However, consider the possibility that whoever is reading your README is a novice and would like more guidance. Listing specific steps helps remove ambiguity and gets people to using your project as quickly as possible. If it only runs in a specific context like a particular programming language version or operating system or has dependencies that have to be installed manually, also add a Requirements subsection.
## Usage
Use examples liberally, and show the expected output if you can. It's helpful to have inline the smallest example of usage that you can demonstrate, while providing links to more sophisticated examples if they are too long to reasonably include in the README.
## Support
Tell people where they can go to for help. It can be any combination of an issue tracker, a chat room, an email address, etc.
## Roadmap
If you have ideas for releases in the future, it is a good idea to list them in the README.
## Contributing
State if you are open to contributions and what your requirements are for accepting them.
For people who want to make changes to your project, it's helpful to have some documentation on how to get started. Perhaps there is a script that they should run or some environment variables that they need to set. Make these steps explicit. These instructions could also be useful to your future self.
You can also document commands to lint the code or run tests. These steps help to ensure high code quality and reduce the likelihood that the changes inadvertently break something. Having instructions for running tests is especially helpful if it requires external setup, such as starting a Selenium server for testing in a browser.
## Authors and acknowledgment
Show your appreciation to those who have contributed to the project.
## License
For open source projects, say how it is licensed.
## Project status
If you have run out of energy or time for your project, put a note at the top of the README saying that development has slowed down or stopped completely. Someone may choose to fork your project or volunteer to step in as a maintainer or owner, allowing your project to keep going. You can also make an explicit request for maintainers.
clean.sh 0 → 100644
# clone the repo (skip if you already have a cloned repo locally)
#git clone git@github.com:USERNAME/REPOSITORY.git
#cd REPOSITORY
# remove all history locally
rm -rf .git
# create a new local repo
git init
# add everything
git add .
git commit -m "First commit"
# nuke history on GitHub (irreversable)
git remote add origin git@lab.compute.dtu.dk:tuhe/unlearn.git
git push -u --force origin master
\ No newline at end of file
File added
File added
File added
import requests
from urllib.parse import urlparse
import json
import os
from http.server import BaseHTTPRequestHandler
from threading import Thread
from auth_code_flow import FlowManager
from diskcache import Cache
from selenium import webdriver
from selenium.webdriver.common.by import By
import socketserver
import ssl
import requests
from bs4 import BeautifulSoup
def whoami(lc):
url = "https://testdtu.brightspace.com/d2l/api/lp/1.36/users/whoami"
payload = {}
headers = {
'Authorization': 'Bearer eyJhbGciO...wVQ'
}
headers = lc.headers
response = requests.request("GET", url, headers=headers, data=payload)
print(response.text)
return response
class LearnConfig:
server = "testdtu.brightspace.com/"
lp_version = "1.36"
headers = {'Authorization': 'Bearer bla-bla' }
scope = "content:*:* core:*:*"
def get_header(self):
headers = {
# cc = Cache(directory="cache")
'Authorization': f"Bearer {self.cc['access_token']}"
}
return headers
def isodate(self, date):
d = date.isoformat()
return d.split(".")[0] + ".000Z"
orgunitId = "8407"
def is_token_valid(self):
response = whoami(self)
json = response.json()
if "FirstName" in json:
print("Hello there", json['FirstName'])
return True
print("Authentication failed")
print(json)
return False
def _oauth2_refresh(self, username='tuhe', password=None):
url = "https://auth.brightspace.com/core/connect/token"
# access_token_url = "https://auth.brightspace.com/core/connect/token"
# authorize_url = "https://auth.brightspace.com/oauth2/auth" # "http://api.example.com/oauth/access_token",
base_url = "https://auth.brightspace.com"
redirect_uri = 'https://localhost:3434/redirecturi'
"""
https://auth.brightspace.com/oauth2/auth?response_type=code&client_id=4801a477-11c7-4cfc-8732-736f041578be&scope=core%3A*%3A*&redirect_uri=https%3A%2F%2Flocalhost%3A3434%2Fredirecturi
http://auth.brightspace.com/oauth2/auth?client_id=4801a477-11c7-4cfc-8732-736f041578be&redirect_uri=https%3A%2F%2Flocalhost%3A3434%2Fredirecturi&response_type=code&scope=core%3A%2A%3A%2A&state=my_state
"""
from unlearn.secrets import client_secret, client_id
se_flow_manager = FlowManager(
base_uri=base_url,
client_id=client_id,
client_secret=client_secret, # "your client secret", # please read this from env vars for security
redirect_uri=redirect_uri,
scope=self.scope,
access_token_path="/core/connect/token",
authorization_path="/oauth2/auth",
response_type='code',
)
ll = start_localhost(se_flow_manager)
state = "my_state"
se_auth_url = se_flow_manager.get_authorization_endpoint(state)
print(se_auth_url)
print("Making driver...")
driver = webdriver.Firefox()
print("Getting url...")
driver.get(se_auth_url)
usernane = driver.find_element(By.ID, "userName")
usernane.send_keys(username)
if password is None:
password = input(f'Please provide password for {username=}')
fpassword = driver.find_element(By.ID, "password")
fpassword.send_keys(password)
button = driver.find_element(By.TAG_NAME, "button")
button.click()
print("> Code obtained from server", S.CODE)
# lc = LearnConfig(refresh_token=S.CODE)
code = S.CODE
ll.shutdown()
print("Code obtained is", code)
resp = se_flow_manager.fetch_access_token(code, state, post_form_data=True)
resp_json = resp.json()
access_token = resp_json['access_token']
refresh_token = resp_json['refresh_token']
print("Obtained tokens through login: ")
print(f"> {access_token=}")
print(f"> {refresh_token=}")
self.cc['access_token'] = access_token
self.cc['refresh_token'] = refresh_token
print("Checking if valid...")
# Check if valid:
self.is_token_valid()
print("All done!")
def formaturl(self, url, **kwargs):
# url = "/d2l/api/bas/(version)/orgunits/(orgUnitId)/classlist/"
url = f"https://{self.server}{url[1:]}"
url = url.replace("(version)", self.lp_version)
url = url.replace("(orgUnitId)", self.orgunitId)
for k, v in kwargs.items():
url = url.replace(f"({k})", str(v))
# print("Brightspace url: ", url)
return url
def GET(self, url, data=None):
if "(" in url or ")" in url:
print("Url was bad", url)
assert False
if data is None:
data = {}
print(f"LearnConfig-GET> {url=}")
print(f"LearnConfig-GET> {data=}")
response = requests.request("GET", url,
headers=self.headers,
params=data)
print(f"LearnConfig-GET> {response=}")
return response
def __init__(self, refresh_token=None, password=None, clear_tokens=False):
cc = Cache(directory= os.path.join( os.path.dirname(__file__), "cache") )
from unlearn.secrets import client_secret, client_id
self.client_secret = client_secret
self.client_id = client_id
# clear_tokens = True
if clear_tokens:
if "refresh_token" in cc:
del cc['refresh_token']
if "access_token" in cc:
del cc['access_token']
# if "access_token" in cc:
# del cc['access_token']
self.cc = cc
# if refresh_token is not None:
# cc['refresh_token'] = refresh_token
while True:
if "access_token" in cc:
print("is there")
# Try to connect...
self.token = cc.get("access_token")
self.headers['Authorization'] = f"Bearer {self.token}"
if self.is_token_valid():
print("Got a valid token!")
a = 234
break
else:
del cc['access_token']
print("The token sucks.")
continue
else:
if "refresh_token" in cc:
url = "https://auth.brightspace.com/core/connect/token"
print("Getting refresh token using", refresh_token)
refresh_token = cc['refresh_token']
# refresh_token = "rt.eu-wefI"
# self.client_id = "48"
# self.client_secret = "niL_8"
import urllib.parse
# scope = urllib.parse.quote(self.scope)
# 'Hell%C3%B6%20W%C3%B6rld%40Python'
scope = self.scope
scope = scope.replace(":", '%3A').replace(" ", '%20')
print(scope)
# core%3A*%3A*
# content%3A*%3A*%20core%3A*%3A*
# payload=f'grant_type=refresh_token&refresh_token={refresh_token}&client_id={self.client_id}&client_secret={self.client_secret}&scope=core%3A*%3A*'
payload=f'grant_type=refresh_token&refresh_token={refresh_token}&client_id={self.client_id}&client_secret={self.client_secret}&scope={scope}'
headers = {'Content-Type': 'application/x-www-form-urlencoded' }
response = requests.request("POST", url, headers=headers, data=payload)
print(response.json())
cc['refresh_token'] = response.json()['refresh_token']
cc['access_token'] = response.json()['access_token']
self.is_token_valid()
print("Authentication success.")
break
else:
# Do oauth2 refreshing...
self._oauth2_refresh(password=password)
# s = input("Please give me a refresh token")
# cc['refresh_token'] = s
# print("Saved refresh token", s)
print("no it is not there")
print("Phew! Authentication completed.")
class S(BaseHTTPRequestHandler):
def _set_headers(self):
self.send_response(200)
self.send_header("Content-type", "text/html")
self.end_headers()
def _html(self, message):
"""This just generates an HTML document that includes `message`
in the body. Override, or re-write this do do more interesting stuff.
"""
content = f"<html><body><h1>{message}</h1></body></html>"
return content.encode("utf8") # NOTE: must return a bytes object!
def do_GET(self):
self._set_headers()
query = urlparse(self.path)
print("HANDLING GET QUERY", query)
# print("query.query is", query.query)
# print()
qq = query.query
print(f"{qq=}")
if len(qq) == 0:
print("Query is empty, returning...")
return
sq = qq.split('=')
print(sq)
sq1 = sq[1]
print("sq[1]", sq1)
sq2 = sq1.split('&')
print(sq2)
code = sq2[0]
print("ciode is", code)
S.CODE = code
self.wfile.write(self._html(f"url: {self.path}<br>Code was: {code}"))
def do_HEAD(self):
self._set_headers()
def do_POST(self):
# Doesn't do anything with posted data
self._set_headers()
self.wfile.write(self._html("POST!"))
class LocalListen(Thread):
def shutdown(self):
print("> localhost: stopping https server...")
self.httpd.shutdown()
print("> localhost: stopped.")
def run(self):
print("hello world")
PORT = 3434
with socketserver.TCPServer(("localhost", PORT), S) as httpd:
httpd.socket = ssl.wrap_socket(httpd.socket, certfile=os.path.join(os.path.dirname(__file__), 'server.pem'), server_side=True)
print("> localhost: serving at port", PORT)
print(httpd.server_address)
self.httpd = httpd
httpd.serve_forever()
print("> localhost: Stopping...")
def start_localhost(flow_manager=None):
S.flow_manager = flow_manager
ll = LocalListen()
ll.start()
return ll
if __name__ == '__main__':
print("Hello world")
lc = LearnConfig()
ll = LocalListen()
ll.start()
ll.httpd.shutdown()
user = 'tuhe'
psw = '...'
params = {'client_id': "4801abe",
'redirect_uri': "https://localhost:3434/redirecturi",
'response_type': "code",
'force_login': "1",
'scope': 'core:*:*',
'client_secret': 'niLX8'}
url = 'https://auth.brightspace.com/oauth2/auth'
token_url = 'https://auth.brightspace.com/core/connect/token'
driver = webdriver.Firefox()
with requests.session() as s:
OAuth_AccessRequest = s.get(url, params=params)
driver.get(OAuth_AccessRequest.url)
# username = driver.find_element(by="id",value="userName")
# password = driver.find_element(by="id", value="password")
# button = driver.find_element(by="type", value="button")
usernane = driver.find_element(By.ID, "userName")
usernane.send_keys("tuhe")
password = driver.find_element(By.ID, "password")
password.send_keys("..")
# submit = driver.find_element(By.ID, "formId")
# submit.submit()
button = driver.find_element(By.TAG_NAME, "button")
button.click()
print("Clicked, seeeing what happens next.")
button = driver.find_element(by="type", value="button")
import datetime
import json
import requests
from learn_config import LearnConfig
def get_calendar_events(lc):
""" https://{{server}}d2l/api/le/{{lp_version}}/{{orgunitId}}/calendar/events/ """
# def whoami(lc):
url = f"https://{lc.server}d2l/api/le/{lc.lp_version}/{lc.orgunitId}/calendar/events/"
payload = {}
response = requests.request("GET", url, headers=lc.headers, data=payload)
print(response.text)
return response
def put_calendar_event(lc, start_date, end_date, title="Lecture", description="Do some stuff today", location_name="Auditorim 666"):
"""
[{'CalendarEventId': 131048,
'OrgUnitId': 8407,
'CreatorUserId': 3397,
'Title': 'This even is a lecture! feel free to drop dead. ',
'Description': 'Oy veh.',
'CalendarEventViewUrl': 'https://testdtu.brightspace.com/d2l/le/calendar/8407/event/146759/detailsview',
'IsAllDayEvent': False,
'GroupId': None,
'StartDateTime': '2022-10-26T23:00:00.000Z',
'EndDateTime': '2022-10-27T01:00:00.000Z',
'IsRecurring': False,
'RecurrenceInfo': None,
'StartDay': None,
'EndDay': None,
'LocationId': None,
'LocationName': 'Auditorium',
'OrgUnitName': 'Tue Herlau- Sandbox',
'OrgUnitCode': 'OFFERING-SANDBOX-92378',
'IsAssociatedWithEntity': False,
'AssociatedEntity': None,
'HasVisibilityRestrictions': False,
'VisibilityRestrictions': {'Type': 1,
'Range': None,
'HiddenRangeUnitType': None,
'StartDate': None,
'EndDate': None},
'Presenters': []}]
"""
url = f"https://testdtu.brightspace.com/d2l/api/le/{lc.lp_version}/{lc.orgunitId}/calendar/event/"
payload = json.dumps({
"Title": title, # "Today's Special 666b",
"Description": description, # "<p>Soup <b>is relly good</p><p>and also healthy</p>",
"StartDateTime": lc.isodate(start_date), # "2019-05-02T20:00:00.000Z",
"EndDateTime": lc.isodate(end_date), # "2019-05-02T20:30:00.000Z",
"StartDay": None,
"EndDay": None,
"GroupId": None,
"RecurrenceInfo": None,
"LocationId": None,
"LocationName": location_name,
"AssociatedEntity": None,
"VisibilityRestrictions": {
"Type": 1,
"Range": None,
"HiddenRangeUnitType": None,
"StartDate": None,
"EndDate": None
}
})
headers = lc.get_header()
headers['Content-Type'] = 'application/json'
response = requests.request("POST", url, headers=headers, data=payload)
print(response.text)
def formaturl(lc, url, **kwargs):
# url = "/d2l/api/bas/(version)/orgunits/(orgUnitId)/classlist/"
url = f"https://{lc.server}{url[1:]}"
url = url.replace("(version)", lc.lp_version)
url = url.replace("(orgUnitId)", lc.orgunitId)
for k, v in kwargs.items():
url = url.replace(f"({k})", str(v))
print("Brightspace url: ", url)
return url
# / d2l / api / bas / (version) / orgunits / (orgUnitId) / classlist /
def get_users_lists(lc):
url = "/d2l/api/le/(version)/(orgUnitId)/classlist/"
url = formaturl(lc, url)
# url = formaturl(lc, "/d2l/api/bas/(version)/orgunits/(orgUnitId)/classlist/")
print("url is", url)
payload = {}
response = requests.request("GET", url, headers=lc.headers, data=payload)
print(response.text)
print(response)
return response.json()
a = 234
pass
def get_modules(lc):
url = "/d2l/api/le/(version)/(orgUnitId)/content/root/"
url = formaturl(lc, url)
payload = {}
response = requests.request("GET", url, headers=lc.headers, data=payload)
print(response)
return response.json()
a = 234
def get_contents(lc):
url = "/d2l/api/le/(version)/(orgUnitId)/content/toc"
# url = "/d2l/api/lp/(version)/collection/(objectId)/contents/"
url = formaturl(lc, url)
get_modules(lc)
payload = {}
response = requests.request("GET", url, headers=lc.headers, data=payload)
# / d2l / api / le / (version) / (orgUnitId) / content / toc
return response.json()
def get_content_root(lc):
url = "/d2l/api/le/(version)/(orgUnitId)/content/root/"
url = formaturl(lc, url)
payload = {}
response = requests.request("GET", url, headers=lc.headers, data=payload)
return response.json()
def get_progress(lc):
"""
/d2l/api/le/(version)/(orgUnitId)/content/userprogress/
"""
# url = f"https://{lc.server}d2l/api/le/{lc.lp_version}/{lc.orgunitId}/content/userprogress/"
url = "/d2l/api/le/(version)/(orgUnitId)/content/userprogress/"
url2 = "/d2l/api/le/(version)/(orgUnitId)/content/userprogress/"
url = formaturl(lc, url)
print(url)
# url = f"https://{lc.server}d2l/api/le/{lc.lp_version}/{lc.orgunitId}/calendar/events/"
payload = {}
# contents = get_contents(lc)
modules = get_modules(lc)
users = get_users_lists(lc)
toc = get_contents(lc)
mod_id = modules[0]['Id']
mod_id = modules[0]['Structure'][0]['Id']
# url = "/d2l/api/le/(version)/(orgUnitId)/content/userprogress/(topicId)"
topics = get_content_root(lc)
mod_id = topics[0]['Id']
# url = formaturl(lc, url).replace("(topicId)", str(mod_id))
# print(url)
topic_id = topics[0]['Id']
# topic_id = 431617
# url = "/d2l/api/le/(version)/(orgUnitId)/content/topics/(topicId)"
# url = formaturl(lc, url, topicId=topic_id)
print(url)
payload = {'userId': int(users[0]['Identifier']), 'objectId': topic_id}
# payload = {'userId': int(users[0]['Identifier']) }
print("payload", payload)
# payload = {}
r = lc.GET(lc.formaturl("/d2l/api/le/(version)/(orgUnitId)/content/toc"))
real_topic_id = r.json()['Modules'][0]['Modules'][0]['Topics'][0]['TopicId']
r = lc.GET(formaturl(lc, "/d2l/api/le/(version)/(orgUnitId)/content/topics/(topicId)/completions/", topicId=real_topic_id))
lc.GET(formaturl(lc, "/d2l/api/le/(version)/(orgUnitId)/content/userprogress/"), {'userId': int(users[1]['Identifier']), 'objectId': mod_id})
lc.GET(formaturl(lc, "/d2l/api/le/(version)/(orgUnitId)/content/userprogress/"), {'userId': int(users[1]['Identifier']), 'objectId': topic_id})
lc.GET(formaturl(lc, "/d2l/api/le/(version)/(orgUnitId)/content/userprogress/"), {'userId': int(users[1]['Identifier']), 'objectId': topic_id})
r = lc.GET(formaturl(lc, "/d2l/api/le/(version)/(orgUnitId)/content/userprogress/(topicId)", topicId=topic_id), {'userId': int(users[1]['Identifier'])} )
lc.GET(lc.formaturl("/d2l/api/le/(version)/locales/"))
r2 = lc.GET(formaturl(lc, "/d2l/api/le/(version)/content/completions/(userId)/", userId=int(users[0]['Identifier'])), {} )
user_id = int(users[1]['Identifier'])
mod_id = modules[0]['Id']
ids = [431617, 431616, 431620, 431619, 431618, 431621]
for id in ids:
r3 = lc.GET(formaturl(lc, "/d2l/api/le/(version)/(orgUnitId)/content/userprogress/"),
dict(userId=user_id, objectId=str(id)))
print(r3)
u = "31054"
u = int(u)
r3 = lc.GET(formaturl(lc, "/d2l/api/le/(version)/(orgUnitId)/content/completions/"), dict(userIdsCSV=u,ignoreInvalid=True) )
r4 =lc.GET(formaturl(lc, "/d2l/api/le/(version)/(orgUnitId)/content/completions/mycount/"), dict(level=3))
print(r4.json())
print(response.text)
print(response)
a = 234
if __name__ == '__main__':
lc = LearnConfig(clear_tokens=False)
# users = get_users_lists(lc)
progress = get_progress(lc)
get_calendar_events(lc)
from datetime import datetime, timedelta
put_calendar_event(lc, start_date=datetime.now(), end_date=datetime.now() + timedelta(hours=2), title="Do stuff for lecture 4", description="<p><b>MORE</b></p>", location_name="Room 022")
import os
if __name__ == "__main__":
pages = os.path.dirname(__file__) + "/../../public"
with open(os.path.dirname(__file__) + "/password.txt", 'r') as f:
password = f.read()
if not os.path.isdir(pages):
os.mkdir(pages)
from unlearn.learn_config import LearnConfig
from webdriver_manager.firefox import GeckoDriverManager
from selenium import webdriver
print("opening...")
lc = LearnConfig(password=password)
print("Is token valid?", lc.is_token_valid())
print("token", lc.cc['access_token'])
print("refresh", lc.cc['refresh_token'])
with open(pages + "/index.html", 'w') as f:
f.write(f"hello world!<p>token: {lc.cc['access_token']}</p><p>refresh_token {lc.cc['refresh_token']}</p>")
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment