Blatzar / schoolsoft-api-app

Reverse engineered schoolsoft api the app uses.
9 stars 1 forks source link

Question, what is this code used for? #3

Closed BonnieToGamer closed 4 years ago

BonnieToGamer commented 4 years ago
  1. found in get_updated_token()

    # Cuts off milliseconds.
    
    expiry_date = token_json['expiryDate'][:-4]
    
    # Assumes the date is formatted like "2020-08-12 17:48:22".
    
    unix_time = time.mktime(datetime.datetime.strptime(expiry_date, "%Y-%m-%d %H:%M:%S").timetuple())
    
    # Extra 5 minutes for good measure
    
    # The token seems to last 3 hours.
    
    if time.time() + 5*60 > unix_time:
    
        token_json = get_token(school, app_key_json, app_key_path, write_file_path)
    
    else:
    
        write_json(write_file_path, token_json)
    
    return token_json
  2. Found in get_calendar()

    unix_time_start = time.time()*1000 if not unix_time_start else unix_time_start*1000
    
    unix_time_end = (time.time() + 2592000)*1000 if not unix_time_end else unix_time_end*1000
    
    # No decimals can get passed to the api without errors.
    
    unix_time_start = round(unix_time_start)
    
    unix_time_end = round(unix_time_end)

    I'm trying to convert this API into C# and can't figure out what this is used for.

Blatzar commented 4 years ago

It's worth looking into unix time to understand this a bit better.

    expiry_date = token_json['expiryDate'][:-4]

    # Assumes the date is formatted like "2020-08-12 17:48:22".

    unix_time = time.mktime(datetime.datetime.strptime(expiry_date, "%Y-%m-%d %H:%M:%S").timetuple())

All this does is to read the expiry date on the token previously generated and covert it to unix time (seconds since 1970, which is easier to do calculations on). With that unix time I calculate if the token is expired and if it is I generate a new one.

if time.time() + 5*60 > unix_time: basically says if the current time + 5 minutes is over the expiry date of the token.

The schoolsoft calendar api is requested by entering a time range, unix_time_start and unix_time_end. The time range is based on unix time in milliseconds (unix time 1000). `unix_time_end = (time.time() + 2592000)1000basically means: the end time is the current time + one month (606024*30`)

Note that I round both start and end to avoid using decimals, that'll throw errors from the api.

BonnieToGamer commented 4 years ago

thank you, i knew what unix time was i just didn't understand the code itself

Blatzar commented 4 years ago

Is any code still unclear or should I close this?

BonnieToGamer commented 4 years ago

sorry forgot to close this