Giter Club home page Giter Club logo

librus-apix's Introduction

librus-apix logo

Librus Synergia web scraper.

Be sure to visit the documentation.

Installation

pip install librus-apix

Important

It's advised to run all tests before trying everything out. Some schools have different librus setups which may cause errors/warnings If you find any open an issue or contribute with a PR

Running tests

First, follow the steps to clone the repo and install librus-apix locally, then install pytest.

pip install pytest

Run the tests

Retrieve your token key and test on actual data

pytest --token {output of token.API_Key}

[Dev] Test using a mock server

  • For developing purposes I've created a simple mock html server

    # generate all html pages and run server
    python scripts/generate_all.py
    python server.py
    # now unless you've changed the server.py default port you should be good to go and run
    pytest
    # if you did change the port, you have to edit the tests/conftest.py file accordingly

Quick Start

Setting up client

from librus_apix.client import Client, Token, new_client

# create a new client with empty Token()
client: Client = new_client()
# update the token
_token: Token = client.get_token("username", "password")
# now you can pass your client to librus-apix functions

Save and Load token

from librus_apix.client import Token, Client, new_client

key = client.token.API_Key

# you can store this key and later load it in ways like this:
## Load directly into token object
token = Token(API_Key=token_key)
client: Client = new_client(token=token)
## or put it into existing client
client.token = token
## or into empty token
client.token.API_Key = key

Getting the Math grades

from librus_apix.grades import get_grades

grades, average_grades, descriptive_grades = get_grades(client)

for semester in grades:
  for mark in semester["Mathematics"]:
      print(mark.grade)
for semester in descriptive_grades:
  for mark in semester["Emotional development"]:
      print(mark.grade)

Getting the Announcements

from librus_apix.announcements import get_announcements

announcements = get_announcements(client)

for a in announcements:
  print(a.description)

Getting the attendance

from librus_apix.attendance import get_attendance

first_semester, second_semester = get_attendance(client)

for attendance in first_semester:
  print(attendance.symbol, attendance.date)

Getting the attendance frequency

from librus_apix.attendance import get_attendance_frequency

first, second, overall = get_attendance_frequency(client)
print(f"{first*100}%")

Getting the Homework

from librus_apix.homework import get_homework, homework_detail

# date from-to up to 1 month 
date_from = '2023-03-02'
date_to = '2023-03-30'

homework = get_homework(client, date_from, date_to)

for h in homework:
  print(h.lesson, h.completion_date)
  href = h.href
  details = homework_detail(client, href)
  print(details)

Sending Messages

from librus_apix.messages import recipient_groups, get_recipients, send_message

groups = recipient_groups(client)
recipients = get_recipients(client, groups[0])
my_recipient = recipients["John Brown"]
my_second_recipient = recipients["Barbara Brown"]

sent = send_message(client,
                   "Message Title",
                   "Message\n content",
                   [my_recipient, my_second_recipient]
)
if sent == True:
  print("Sent!")
else:
  print("Error sending a message!")

Getting the Messages

from librus_apix.messages import get_received, message_content

messages = get_received(client, page=1)
for message in messages:
  print(message.title)
  href = message.href
  print(message_content(client, href))

Getting the Schedule

from librus_apix.schedule import get_schedule, schedule_detail
month = '2'
year = '2023'
schedule = get_schedule(client, month, year)
for day in schedule:
  for event in schedule[day]:
    print(event.title)
    prefix, href = event.href.split('/')
    details = schedule_detail(client, prefix, href)
    print(details)

Getting the Timetable

from datetime import datetime
from librus_apix.timetable import get_timetable

monday_date = '2023-04-3'
monday_datetime = datetime.strptime(monday_date, '%Y-%m-%d')
timetable = get_timetable(client, monday_datetime)
for weekday in timetable:
  for period in timetable[weekday]:
    print(period.subject, period.teacher_and_classroom)

Notifications (a.k.a. spamming endpoints)

# initial should be always called with a new token
initial_notifications, new_ids = get_initial_notification_data(client)
sleep(150)
# after that you retrieve the new Notifications with new_ids filtered out 
new_notifications, new_ids = get_new_notification_data(client, new_ids)
# see more in docs

Getting the lucky number

from librus_apix.student_information import student_information

info = student_information(client)
print(info.lucky_number)

Adding a proxy

# Proxy can be added with
client = new_client(proxy={"https": "http://my-proxy.xyz"})
# or
client.proxy = {"https": "http://my-proxy.xyz"}

Working On The Project

git clone https://github.com/RustySnek/librus-apix
cd librus-apix
python -m venv venv
source ./venv/bin/activate
pip install requirements.txt
# Installing library with editable flag
pip install -e .

librus-apix's People

Contributors

anetczuk avatar dependabot[bot] avatar maciejkrol18 avatar rustysnek avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

librus-apix's Issues

? lucky number

When returning student information it will try to convert lucky_number to int, but if the school has lucky numbers disabled, the lucky number will show as ? which it will try to convert to int, resulting in ValueError: invalid literal for int() with base 10: '?'

ValueError when using some of the functions

I'm encountering a ValueError when calling some of the functions provided - more specifically, get_grades, get_attendance and get_schedule. I'm new to python so lmk if there's some sort of mistake on my side.

get_grades

Code

from librus_apix.grades import get_grades
grades, average_grades = get_grades(token)

for mark in grades["Mathematics"]:
    print(mark.grade)

Result

Traceback (most recent call last):
  File "\main.py", line 8, in <module>
    grades, average_grades = get_grades(token)
  File "\AppData\Local\Programs\Python\Python310\lib\site-packages\librus_apix\grades.py", line 95, in get_grades
    key, value = attr.split(': ')
ValueError: too many values to unpack (expected 2)

get_attendance

Code

from librus_apix.attendance import get_attendance
first_semester, second_semester = get_attendance(token)

for attendance in first_semester:
    print(attendance.symbol, attendance.date)

Result

Traceback (most recent call last):
  File "\main.py", line 16, in <module>
    first_semester, second_semester = get_attendance(token)
  File "\AppData\Local\Programs\Python\Python310\lib\site-packages\librus_apix\attendance.py", line 63, in get_attendance
    _type, date, lesson, subject, teacher, hour, excursion, by = (
ValueError: not enough values to unpack (expected 8, got 7)

get_schedule

Code

from librus_apix.schedule import get_schedule, schedule_detail
month = '2'
year = '2023'
schedule = get_schedule(token, month, year)
for day in schedule:
  for event in schedule[day]:
    print(event.title)
    prefix, href = event.href.split('/')
    details = schedule_detail(token, prefix, href)
    print(details)

Result: Here the code successfully prints out one event from the schedule and then gets the error

Zastępstwo z Jan Kowalski na lekcji nr: 5 (Matematyka)
Traceback (most recent call last):
  File "\main.py", line 38, in <module>
    prefix, href = event.href.split('/')
ValueError: not enough values to unpack (expected 2, got 1)

Handle disabled features

Some schools may disable features like for example grade averages. Handle stuff like that so it doesn't break everything

Error with importing in README.md

In the README file, functions are imported as follows:

from librus-apix.get_token import get_token

This causes an error, as it should look like so:

from librus_apix.get_token import get_token

(underscore instead of a hyphen)

timetable get errors

Hi i have a problem with getting a timetable here is my code (from example)

from datetime import datetime
from librus_apix.timetable import get_timetable
from librus_apix.get_token import get_token

username = "login"
password = "pass"
token = get_token(username, password)

monday_date = '2023-10-09'
monday_datetime = datetime.strptime(monday_date, '%Y-%m-%d')
timetable = get_timetable(token, monday_datetime)
for weekday in timetable:
  for period in timetable[weekday]:
    print(period.subject, period.teacher_and_classroom)

and this is errors

Traceback (most recent call last):
 
File "/Users/uwmpr/Downloads/librus-apix-main/librus_apix/mojetesty.py", line 15, in <module>
    timetable = get_timetable(token, monday_datetime)
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/uwmpr/Downloads/librus-apix-main/librus_apix/timetable.py", line 40, in get_timetable
    lesson = periods[period].select(
             ~~~~~~~^^^^^^^^^^^^^
IndexError: list index out of range  

Does anyone want to help me? thanks! And thanks for this code it helps me a lot with my own project.

can't split ':' values

Hi
Looks like the API was changes for in the more complexes example I've got grades problem reading using example scripts

Traceback (most recent call last):
File "/home/pg/apps/librus-apix/ania/grades_example.py", line 12, in
grades, semester_grades = get_grades(token, sort_by[0])
File "/home/pg/apps/librus-apix/librus_apix/grades.py", line 95, in get_grades
key, value = attr.split(': ')
ValueError: too many values to unpack (expected 2)

for the other kid looks pretty ok
Subject: Etyka
Subject: Język angielski
|6| - 2023-10-27
|5| - 2023-11-12
Subject: Religia
etc.
grades_issue

Hope that could help for the future software usage
thanks a lot

Add marking homework as done

Currently can't implement marking homework as done, due to POST: https://synergia.librus.pl/homework/markHomeworkAsDone requiring studentID which isn't present anywhere.

No mention of "sort_by" argument of get_grades() in README

In the README, the usage of get_grades is shown as follows:

from librus_apix.grades import get_grades

grades, average_grades = get_grades(token)

for mark in grades["Mathematics"]:
    print(mark.grade)

Running this code results in TypeError: get_grades() missing 1 required positional argument: 'sort_by'

Malformed or expired token

Hi,

BTW Great work with this library.
I'm trying to go with getting grades and I got an exception.
I'm sure the credentials are good, token is OK.

root@sandbox:~/librus-apix/examples# python3 grades_example.py Traceback (most recent call last): File "/root/librus-apix/examples/grades_example.py", line 12, in <module> grades, semester_grades = get_grades(token, sort_by[0]) File "/usr/local/lib/python3.9/dist-packages/librus_apix/grades.py", line 68, in get_grades tr = no_access_check( File "/usr/local/lib/python3.9/dist-packages/librus_apix/helpers.py", line 8, in no_access_check raise TokenError("Malformed or expired token.") librus_apix.exceptions.TokenError: Malformed or expired token.

Maybe html structure on the portal has been changed ?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.