Spaces:
Sleeping
Sleeping
Update app.py
Browse files10 unmodified lines1112131415161721 unmodified lines394041424310 unmodified linesif current_line == fileid:return line_content.strip()return None@app.route('/')def main():files = os.listdir('.')21 unmodified linesif servo == 3:return three# ^ identify the servoif __name__ == '__main__':app.run(host='0.0.0.0', port=5000, debug=False)10 unmodified lines11121314151617181920212223242526272829303132333421 unmodified lines565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810 unmodified linesif current_line == fileid:return line_content.strip()return Nonedef update_servo_in_database(fileid, new_value):try:fileid = int(fileid)#steal the lineswith open('database.txt', 'r') as file:lines = file.readlines()# make sure the line is not scamming me and is fakeif fileid < 1 or fileid > len(lines):return False#update itlines[fileid - 1] = new_value + '\n' # do the minus bc it starts a 0 not 1#put 'em all backwith open('database.txt', 'w') as file:file.writelines(lines)return Trueexcept Exception as e:print(f"database was not really updated 😟: {e}")return False@app.route('/')def main():files = os.listdir('.')21 unmodified linesif servo == 3:return three# ^ identify the servo@app.route('/debugchange')def debuggly():try:# debugchange?=id,servo_number,new_valuefull_query = request.query_string.decode('utf-8')if full_query.startswith('='):data = full_query[1:]# get the parts of the urlparts = data.split(',')if len(parts) < 3:return "please use format debugchange?=id,servo_number,new_value lol", 400id, servo_num, new_value = parts#create or update linesservolist = get_servo_list(id)if servolist is None:# 0 as defaultservos = ['0', '0', '0']else:# use current valuesservos = servolist.split(',')servo_num = int(servo_num)if servo_num < 1 or servo_num > len(servos):return f"keep the servo number between 1 and 3", 400# make sure it's between 1 and 180new_value = int(new_value)if new_value < 0 or new_value > 180:return "Error: Servo value must be between 0-180 degrees", 400# update the servoservos[servo_num - 1] = str(new_value) # do the minus thing because list starts a 0 not 1 :(# save to big databaseupdated_line = ','.join(servos)if update_servo_in_database(id, updated_line):if servolist is None:return f"created new line {id} with no problems ever! servo {servo_num} set to {new_value}°<br>New values: {updated_line}"else:return f"updated line {id}! servo {servo_num} set to {new_value}°<br>new values: {updated_line}"else:return "yeah nah that didn't work :(", 500except ValueError as e:return f"invalid frogmat - {e}", 400except Exception as e:return f"oh no big error: {str(e)}", 500if __name__ == '__main__':app.run(host='0.0.0.0', port=5000, debug=False)
app.py+66-1