Added scanner python script
This commit is contained in:
parent
f1355b4969
commit
0a30eb6d68
2 changed files with 40 additions and 3 deletions
|
@ -16,12 +16,12 @@ $fp = fopen($file, 'r');
|
|||
$result = json_decode(fread($fp, filesize($file)));
|
||||
fclose($fp);
|
||||
|
||||
$price = 'N/A';
|
||||
$returnVal = 'N/A';
|
||||
|
||||
// Get the price from the given code and remove 1 in quantity. Do not get the price if quantity is at 0
|
||||
foreach ($result->articles as $articleObject) {
|
||||
if ($articleObject->code === $_POST["code"] && intval($articleObject->quantity) > 0) {
|
||||
$price = $articleObject->price;
|
||||
$returnVal = $articleObject->name . "\n". $articleObject->price . "€";
|
||||
$articleObject->quantity = strval(intval($articleObject->quantity) - 1);
|
||||
}
|
||||
}
|
||||
|
@ -31,5 +31,5 @@ $fp = fopen('../data/stock.json', 'w');
|
|||
fwrite($fp, json_encode($result));
|
||||
fclose($fp);
|
||||
|
||||
echo $price;
|
||||
echo $returnVal;
|
||||
|
||||
|
|
37
zapette.py
Normal file
37
zapette.py
Normal file
|
@ -0,0 +1,37 @@
|
|||
# run pip3 install requests
|
||||
import requests
|
||||
import json
|
||||
|
||||
API_ENDPOINT = "http://localhost/proximo/ajax/scan_article.php"
|
||||
|
||||
def get_password():
|
||||
with open('pass') as f:
|
||||
password = f.readline()
|
||||
return password.strip()
|
||||
|
||||
|
||||
def search_product(code):
|
||||
# data to be sent to api
|
||||
data = {
|
||||
'password': get_password(),
|
||||
'code': str(code)
|
||||
}
|
||||
# sending post request and saving response as response object
|
||||
r = requests.post(url=API_ENDPOINT, data=json.dumps(data))
|
||||
return r.text
|
||||
|
||||
|
||||
def main():
|
||||
code_input = input('Scannez le code\n')
|
||||
try:
|
||||
code = int(code_input)
|
||||
result = search_product(code)
|
||||
print(result)
|
||||
except requests.exceptions.MissingSchema:
|
||||
print("Format URL invalide !")
|
||||
except requests.exceptions.ConnectionError:
|
||||
print("URL invalide !")
|
||||
except ValueError:
|
||||
print("Code invalide !")
|
||||
|
||||
main()
|
Loading…
Reference in a new issue