Browse Source

Added scanner python script

keplyx 4 years ago
parent
commit
0a30eb6d68
2 changed files with 40 additions and 3 deletions
  1. 3
    3
      ajax/scan_article.php
  2. 37
    0
      zapette.py

+ 3
- 3
ajax/scan_article.php View File

@@ -16,12 +16,12 @@ $fp = fopen($file, 'r');
16 16
 $result = json_decode(fread($fp, filesize($file)));
17 17
 fclose($fp);
18 18
 
19
-$price = 'N/A';
19
+$returnVal = 'N/A';
20 20
 
21 21
 // Get the price from the given code and remove 1 in quantity. Do not get the price if quantity is at 0
22 22
 foreach ($result->articles as $articleObject) {
23 23
     if ($articleObject->code === $_POST["code"] && intval($articleObject->quantity) > 0) {
24
-        $price = $articleObject->price;
24
+        $returnVal = $articleObject->name . "\n". $articleObject->price . "€";
25 25
         $articleObject->quantity = strval(intval($articleObject->quantity) - 1);
26 26
     }
27 27
 }
@@ -31,5 +31,5 @@ $fp = fopen('../data/stock.json', 'w');
31 31
 fwrite($fp, json_encode($result));
32 32
 fclose($fp);
33 33
 
34
-echo $price;
34
+echo $returnVal;
35 35
 

+ 37
- 0
zapette.py View File

@@ -0,0 +1,37 @@
1
+# run pip3 install requests
2
+import requests
3
+import json
4
+
5
+API_ENDPOINT = "http://localhost/proximo/ajax/scan_article.php"
6
+
7
+def get_password():
8
+    with open('pass') as f:
9
+        password = f.readline()
10
+    return password.strip()
11
+
12
+
13
+def search_product(code):
14
+    # data to be sent to api
15
+    data = {
16
+        'password': get_password(),
17
+        'code': str(code)
18
+    }
19
+    # sending post request and saving response as response object
20
+    r = requests.post(url=API_ENDPOINT, data=json.dumps(data))
21
+    return r.text
22
+
23
+
24
+def main():
25
+    code_input = input('Scannez le code\n')
26
+    try:
27
+        code = int(code_input)
28
+        result = search_product(code)
29
+        print(result)
30
+    except requests.exceptions.MissingSchema:
31
+        print("Format URL invalide !")
32
+    except requests.exceptions.ConnectionError:
33
+        print("URL invalide !")
34
+    except ValueError:
35
+        print("Code invalide !")
36
+
37
+main()

Loading…
Cancel
Save