Created
April 9, 2019 18:38
-
-
Save ByK95/ee99e11257ae27130f67cc32e6517122 to your computer and use it in GitHub Desktop.
Script that downloads file from server and pipes it to external program
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# https://stackoverflow.com/questions/24562590/login-to-webpage-from-script-using-requests-and-django | |
import requests | |
import re | |
import datetime | |
import time | |
import subprocess | |
base_url = 'http://localhost:8000' | |
url_login= base_url +'/accounts/login/' | |
orderlist_url = base_url + '/producers/orderlist' | |
check_order_url = base_url + '/producers/checkorder' | |
order_url = base_url + '/producers/order/' | |
order_file_url = order_url + 'file/' | |
order_done_url = order_url + 'done/' | |
TIMER_DELAY = 120 | |
DEFAULT_FILENAME = 'model.gcode' | |
regex = r'<li>.*?<\/li>' | |
PRINTER_ID = 14 | |
prev_date_check = None | |
user = 'username' | |
password = 'password' | |
def on_unix(filename): | |
""" | |
Function that creates unix command text for a given filename | |
and saves this into file named command which is later executed | |
by subprocess.Popen | |
""" | |
_command = "(echo connect;echo load {};echo print;) | pronsole".format(filename) | |
with open('command', 'wb') as f: | |
f.write(_command) | |
def get_priority_token(tokens,PRINTER_ID): | |
""" | |
Function that parses "14-dca6e2d8f368b9c258c5" where | |
'14' indicates to printer id 'dca6e2d8f368b9c258c5' is | |
file token this url will redirect to file url returns None | |
if tokens doesnt have given printer id | |
""" | |
for token in tokens: | |
_token = token[4:-5].split('-') | |
_printer_id = int(_token[0]) | |
if _printer_id == PRINTER_ID: | |
return _token[1] | |
return None | |
if __name__ == '__main__': | |
""" | |
Script that fetches file from printer site and saves it with | |
DEFAULT_FILENAME name locally it firstly logins to website and | |
stores csrftoken after that will check 'check_order_url' and | |
read response header | |
'Last-Update' is date shows when last order is booked | |
current user has will cache that number to 'prev_date_check' | |
will request for header['Last-Update'] if it passes then send request | |
to the part list | |
then loades 'orderlist_url' where orders listed with tokens | |
parses them and 'get_priority_token' returns first token match with printer id | |
downloades that file into local storage | |
**for gcode using tokens might be good idea | |
""" | |
client = requests.session() | |
client.cookies.clear() | |
r1 = client.get(url_login) | |
csrftoken = client.cookies['csrftoken'] | |
login_data = {'username':user,'password':password, 'csrfmiddlewaretoken':csrftoken} | |
client_logged = client.post(url_login,data=login_data,allow_redirects=True) | |
while True: | |
check = client.get(check_order_url).headers['Last-Update'] | |
last_update = datetime.datetime.strptime(check, '%a, %d %b %Y %H:%M:%S GMT') | |
if prev_date_check is None or last_update > prev_date_check: | |
payload = client.get(orderlist_url) | |
tokens = re.findall(regex,payload.text) | |
file_token = get_priority_token(tokens,PRINTER_ID) | |
if file_token: | |
r = client.get(order_file_url + file_token, allow_redirects=True) | |
open(DEFAULT_FILENAME,'wb').write(r.content) | |
on_unix(DEFAULT_FILENAME) | |
p = subprocess.Popen(["/bin/bash","command"],stdin=subprocess.PIPE,stdout=subprocess.PIPE) | |
stdout = p.communicate() #remove stdout cz it may couse unnecessary memory usage | |
else: | |
time.sleep(TIMER_DELAY) | |
prev_date_check = check |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment