def __init__(self, user, api_url='https://api.librus.pl/2.0', user_agent='LibrusMobileApp', cache=cache_lib.AlchemyCache()): """ Tworzy sesję z API Synergii. :param librus_tricks.auth.SynergiaUser user: Użytkownik sesji :param str api_url: Bazowy url api, zmieniaj jeżeli chcesz używać proxy typu beeceptor :param str user_agent: User-agent klienta http, domyślnie się podszywa pod aplikację :param librus_tricks.cache.CacheBase cache: Obiekt, który zarządza cache """ self.user = user self.session = requests.session() self.session.headers.update({'User-Agent': user_agent}) self.__auth_headers = {'Authorization': f'Bearer {user.token}'} self.__api_url = api_url if cache_lib.CacheBase in cache.__class__.__bases__: self.cache = cache self.li_session = self else: raise exceptions.InvalidCacheManager( f'{cache} can not be a cache object!') self.__message_reader = None
def ensure_session(): if 'session' not in globals(): global EMAIL global PASSWORD global session try: session = use_json() except Exception: EMAIL = os.environ['librus_email'] PASSWORD = os.environ['librus_password'] session = create_session( EMAIL, PASSWORD, cache=cache.AlchemyCache(engine_uri='sqlite:///:memory:')) session.user.dump_credentials()
def __init__(self, user, api_url='https://api.librus.pl/2.0', user_agent='LibrusMobileApp', cache=cache_lib.AlchemyCache(), synergia_user_passwd=None): """ :param user: :param api_url: :param user_agent: :param cache: :param synergia_user_passwd: """ self.user = user self.session = requests.session() if cache_lib.CacheBase in cache.__class__.__bases__: self.cache = cache self.li_session = self else: raise exceptions.InvalidCacheManager( f'{cache} can not be a cache object!') if synergia_user_passwd: self.message_reader = MessageReader(self.user.login, synergia_user_passwd, cache_backend=self.cache) else: self.message_reader = None self.__auth_headers = { 'Authorization': f'Bearer {user.token}', 'User-Agent': user_agent } self.__api_url = api_url
import logging logging.basicConfig( level=logging.DEBUG, format= '%(asctime)s - %(levelname)s - %(module)s:%(lineno)d - %(funcName)s - %(message)s' ) from flask import Flask, render_template, request from librus_tricks import create_session, exceptions, cache from librus_tricks.tools import * app = Flask(__name__) my_cache = cache.AlchemyCache() @app.route('/') def home_view(): return render_template('home.html') @app.route('/average', methods=['POST']) def count_averages(): logging.info('Recived json %s', request.json) try: session = create_session(request.json['email'], request.json['password'], cache=my_cache) logging.info('Session created %s', session) except exceptions.LibrusPortalInvalidPasswordError: logging.info('Bad password')
import os import sys sys.path.extend(['./']) email = os.environ['librus_email'] password = os.environ['librus_password'] from librus_tricks import exceptions, create_session, cache session = create_session(email, password, cache=cache.AlchemyCache(engine_uri='sqlite:///:memory:')) def test_auth(): return session.user.is_revalidation_required(use_query=True) def test_attendance(): return session.attendances() def test_exams(): return session.exams() def test_grades(): return session.grades() def test_timetable(): return session.tomorrow_timetable, session.today_timetable, session.timetable()
import os import sys sys.path.extend(['./']) email = os.environ['librus_email'] password = os.environ['librus_password'] from librus_tricks import create_session, cache session = create_session( email, password, cache=cache.AlchemyCache(engine_uri='sqlite:///:memory:')) def test_grade(): grades = session.grades() teachers = [x.teacher for x in grades] subjects = [x.subject for x in grades] cats = [x.category for x in grades] return teachers, subjects, cats def test_attedance(): att = session.attendances() types = [x.type for x in att] return att, types