Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import requests
- __author__ = 'Andres Torti'
- class OctoprintAPI:
- def __init__(self, host):
- self.host = host
- self.s = requests.Session()
- self.s.headers.update({'X-Api-Key': 'B0BE78429FCA477DA8399CE75928E093',
- 'Content-Type': 'application/json'})
- def is_printer_connected(self):
- if self.s.get('http://' + self.host + ':5000/api/printer').status_code != 200:
- return False
- else:
- return True
- def set_bed_temp(self, temp):
- r = self.s.post('http://' + self.host + ':5000/api/printer/bed', json={'command': 'target', 'target': temp})
- print(r)
- print(r.content)
- print()
- def pause_job(self):
- r = self.s.post('http://' + self.host + ':5000/api/job', json={'command': 'pause'})
- def resume_job(self):
- r = self.s.post('http://' + self.host + ':5000/api/job', json={'command': 'pause'})
- def start_job(self):
- r = self.s.post('http://' + self.host + ':5000/api/job', json={'command': 'start'})
- def cancel_job(self):
- r = self.s.post('http://' + self.host + ':5000/api/job', json={'command': 'cancel'})
- def get_print_progress(self):
- data = self.s.get('http://' + self.host + ':5000/api/job').content.decode('utf-8').split('\n')
- for line in data:
- if 'completion' in line:
- # check if null
- if 'null' in line:
- return 0
- else:
- return int(float(line[line.find(':')+1:line.find(',')]))
- return 0
- def get_total_print_time(self):
- data = self.s.get('http://' + self.host + ':5000/api/job').content.decode('utf-8').split('\n')
- for line in data:
- if 'estimatedPrintTime' in line:
- # check if null
- if 'null' in line:
- return 0
- else:
- return int(float(line[line.find(':')+1:line.find(',')]))
- return 0
- def get_elapsed_print_time(self):
- data = self.s.get('http://' + self.host + ':5000/api/job').content.decode('utf-8').split('\n')
- for line in data:
- if 'printTime' in line:
- # check if null
- if 'null' in line:
- return 0
- else:
- return int(float(line[line.find(':')+1:line.find(',')]))
- return 0
- def get_file_printing(self):
- data = self.s.get('http://' + self.host + ':5000/api/job').content.decode('utf-8').split('\n')
- for line in data:
- if 'name' in line:
- # check if null
- if 'null' in line:
- return "Detenido..."
- else:
- return line[line.find(':')+1:line.find(',')].replace('"', '').strip()
- return "Detenido..."
- def get_extruder_target_temp(self):
- data = self.s.get('http://' + self.host + ':5000/api/printer/tool').content.decode('utf-8').split('\n')
- for line in data:
- if 'target' in line:
- if 'null' in line:
- return 0
- else:
- return int(float(line[line.find(':')+1:line.find(',')]))
- return 0
- def get_extruder_current_temp(self):
- data = self.s.get('http://' + self.host + ':5000/api/printer/tool').content.decode('utf-8').split('\n')
- for line in data:
- if 'actual' in line:
- return int(float(line[line.find(':')+1:line.find(',')]))
- return 0
- def get_bed_temp(self):
- data = self.s.get('http://' + self.host + ':5000/api/printer/bed').content.decode('utf-8').split('\n')
- for line in data:
- if 'target' in line:
- return int(float(line[line.find(':')+1:line.find('\'')]))
- return 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement