WillemVH's picture
Update app.py
e832d85 verified
from flask import Flask, request
import requests
import time
import os
app = Flask(__name__)
#get servo list from id
def get_servo_list(fileid):
fileid = int(fileid)
with open('database.txt', 'r') as file:
for current_line, line_content in enumerate(file, 1):
if current_line == fileid:
return line_content.strip()
return None
def update_servo_in_database(fileid, new_value):
try:
fileid = int(fileid)
#steal the lines
with open('database.txt', 'r') as file:
lines = file.readlines()
# make sure the line is not scamming me and is fake
if fileid < 1 or fileid > len(lines):
return False
#update it
lines[fileid - 1] = new_value + '\n' # do the minus bc it starts a 0 not 1
#put 'em all back
with open('database.txt', 'w') as file:
file.writelines(lines)
return True
except Exception as e:
return(f"database was not really updated 😟: {e}")
@app.route('/')
def main():
files = os.listdir('.')
return f'a very nice page, you must admit, heres a list of files: {files}'
@app.route('/servo')
def apirequests():
# servo?=1,3
# this code filters the id out V
full_query = request.query_string.decode('utf-8')
if full_query.startswith('='):
data = full_query[1:]
#ends here ^ and saves in data
id, servo = data.split(',')
#splitting them into id and servo
#then we can get the id from the database
servolist = get_servo_list(id)
one, two, three = servolist.split(',')
#split it up ^
servo = int(servo)
if servo == 1:
return one
if servo == 2:
return two
if servo == 3:
return three
# ^ identify the servo
@app.route('/debugchange')
def debuggly():
try:
# debugchange?=id,servo_number,new_value
full_query = request.query_string.decode('utf-8')
if full_query.startswith('='):
data = full_query[1:]
# get the parts of the url
parts = data.split(',')
if len(parts) < 3:
return "please use format debugchange?=id,servo_number,new_value lol", 400
id, servo_num, new_value = parts
#create or update lines
servolist = get_servo_list(id)
if servolist is None:
# 0 as default
servos = ['0', '0', '0']
else:
# use current values
servos = 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 180
new_value = int(new_value)
if new_value < 0 or new_value > 180:
return "please try to keep the value between 1 to 180", 400
# update the servo
servos[servo_num - 1] = str(new_value) # do the minus thing because list starts a 0 not 1 :(
# save to big database
updated_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}° as well :D"
else:
return f"updated line {id} with no problems ever! servo {servo_num} set to {new_value}° as well :D"
else:
return "yeah nah that didn't work :(", 500
except ValueError as e:
return f"invalid frogmat - {e}", 400
except Exception as e:
return f"oh no big error: {str(e)}", 500
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000, debug=False)