Advertisement
TTLC198

Test KAIScheduleParser

Jun 18th, 2022
1,098
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.26 KB | None | 0 0
  1. import json
  2. from datetime import timedelta, datetime
  3. import numpy as np
  4.  
  5. import requests
  6. import sqlite3
  7.  
  8. kaiUrl = 'https://kai.ru/raspisanie'
  9.  
  10.  
  11. def normalizeString(string):
  12.     while "  " in string:
  13.         string = string.replace("  ", " ")
  14.     return string[:-1]
  15.  
  16.  
  17. def getConnectionString():
  18.     with open(
  19.             r'C:\Users\yoreh\RiderProjects\KAIFreeAudiencesBotProject\KAIFreeAudiencesBot\KAIFreeAudiencesBot\appsettings.Development.json') as file:
  20.         config = json.load(file)
  21.         return config['ConnectionStrings']['ScheduleConnectionSqlite'][12:]
  22.  
  23.  
  24. def getGroup(groupnum):
  25.     params = dict(p_p_id='pubStudentSchedule_WAR_publicStudentSchedule10',
  26.                   p_p_lifecycle='2',
  27.                   p_p_resource_id='getGroupsURL',
  28.                   query=groupnum)
  29.     responseJson = requests.get(kaiUrl, params=params).json()
  30.     groups = []
  31.     for i in responseJson:
  32.         groups.append(type('', (object,),
  33.                            {
  34.                                'id': str(i['id']),
  35.                                'group': i['group']
  36.                            })())
  37.  
  38.     return groups
  39.  
  40.  
  41. def getScheduleById(groupid):
  42.     params = dict(p_p_id='pubStudentSchedule_WAR_publicStudentSchedule10',
  43.                   p_p_lifecycle='2',
  44.                   p_p_resource_id='schedule',
  45.                   groupId=groupid)
  46.     responseJson = requests.get(kaiUrl, params=params).json()
  47.     return responseJson
  48.  
  49.  
  50. def main():
  51.     schedules = []
  52.     timeRanges = []
  53.     teachers = []
  54.     lessons = []
  55.     groups = []
  56.     classrooms = []
  57.  
  58.     db = sqlite3.connect(getConnectionString())
  59.     cur = db.cursor()
  60.  
  61.     for i in range(1, 2):
  62.         for j in getGroup(str(i)):
  63.             schedule = getScheduleById(j.id)
  64.             if len(schedule) != 0:
  65.                 groups.append(type('', (object,),
  66.                                    {
  67.                                        'id': j.id,
  68.                                        'group_number': j.group
  69.                                    })())
  70.                 schedules.append(schedule)
  71.  
  72.     for schGroups in schedules:
  73.         for schDays in schGroups.values():
  74.             for schLessons in schDays:
  75.                 dayTime = normalizeString(schLessons['dayTime'])
  76.                 if dayTime != '':
  77.                     if len(timeRanges) > 0:
  78.                         if not any (dayTime == tr.start_time for tr in timeRanges):
  79.                             timeRanges.append(type('', (object,),
  80.                                                    dict(start_time=dayTime, end_time=datetime.strftime((datetime.strptime(dayTime, '%H:%M') + timedelta(hours=1, minutes=30)), '%H:%M')))())
  81.                     else:
  82.                         timeRanges.append(type('', (object,),
  83.                                                dict(start_time=dayTime, end_time=datetime.strftime((datetime.strptime(dayTime, '%H:%M') + timedelta(hours=1, minutes=30)), '%H:%M')))())
  84.  
  85.                 audNum = normalizeString(schLessons['audNum'])
  86.                 building = normalizeString(schLessons['buildNum'])
  87.                 if audNum != '' and building != '':
  88.                     if len(classrooms) > 0:
  89.                         if not any(audNum == cr.classroom_number and building == cr.building for cr in classrooms):
  90.                             classrooms.append(type('', (object,),
  91.                                                    dict(classroom_number=audNum, building=building))())
  92.                     else:
  93.                         classrooms.append(type('', (object,),
  94.                                                dict(classroom_number=audNum, building=building))())
  95.  
  96.                 full_name = normalizeString(schLessons['prepodName'])
  97.                 if full_name != '':
  98.                     if len(teachers) > 0:
  99.                         if not any(full_name == t.full_name for t in teachers):
  100.                             teachers.append(type('', (object,),
  101.                                                  dict(full_name=full_name))())
  102.                     else:
  103.                         teachers.append(type('', (object,),
  104.                                              dict(full_name=full_name))())
  105.  
  106.     print('123')
  107.  
  108.     # db.commit()
  109.     # db.close()
  110.  
  111.  
  112. if __name__ == '__main__':
  113.     main()
  114.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement