Spaces:
Sleeping
Sleeping
File size: 3,741 Bytes
b5b6910 941e798 455ecf5 095db3f 3940b7c 49f251b 3940b7c 5db2c1e e832d85 b5b6910 c7bf148 ac4f2a4 b5b6910 d399ddb c7bf148 d399ddb 4f26df3 c7bf148 3940b7c 4f26df3 3940b7c fe4cd68 1a02733 fe4cd68 1a02733 fe4cd68 1a02733 fe4cd68 5db2c1e e832d85 5db2c1e e832d85 5db2c1e e832d85 5db2c1e 941e798 326c62d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
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) |