|
@@ -1,37 +1,174 @@
|
1
|
1
|
# run pip3 install requests
|
2
|
2
|
import requests
|
3
|
3
|
import json
|
|
4
|
+import os
|
|
5
|
+from signal import signal, SIGINT
|
|
6
|
+from sys import exit
|
4
|
7
|
|
5
|
|
-API_ENDPOINT = "https://srv-falcon.etud.insa-toulouse.fr/~proximo/ajax/scan_article.php"
|
|
8
|
+# API_ENDPOINT = "https://etud.insa-toulouse.fr/~proximo/ajax/zapette.php"
|
|
9
|
+API_ENDPOINT = "http://localhost/proximo/ajax/zapette.php"
|
6
|
10
|
|
7
|
|
-def get_password():
|
8
|
|
- with open('pass') as f:
|
9
|
|
- password = f.readline()
|
10
|
|
- return password.strip()
|
|
11
|
+class bcolors:
|
|
12
|
+ HEADER = '\033[95m'
|
|
13
|
+ OKBLUE = '\033[94m'
|
|
14
|
+ OKGREEN = '\033[92m'
|
|
15
|
+ WARNING = '\033[93m'
|
|
16
|
+ FAIL = '\033[91m'
|
|
17
|
+ ENDC = '\033[0m'
|
|
18
|
+ BOLD = '\033[1m'
|
|
19
|
+ UNDERLINE = '\033[4m'
|
11
|
20
|
|
|
21
|
+class scan_types:
|
|
22
|
+ SELL = 'v',
|
|
23
|
+ BUY = 'a',
|
12
|
24
|
|
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
|
|
25
|
+class error_types:
|
|
26
|
+ NONE = 0,
|
|
27
|
+ NETWORK = 1,
|
|
28
|
+ URL = 2,
|
|
29
|
+ INPUT = 3,
|
|
30
|
+ NO_EXIST = 4,
|
22
|
31
|
|
|
32
|
+class Scanner:
|
|
33
|
+
|
|
34
|
+ def __init__(self):
|
|
35
|
+ self.ask_type()
|
|
36
|
+ self.password = self.get_password()
|
|
37
|
+ self.scannedArticles = []
|
|
38
|
+
|
|
39
|
+ def ask_type(self):
|
|
40
|
+ typeInput = input("\nVoulez vous " + bcolors.OKGREEN + "acheter" + bcolors.ENDC + " ou " + bcolors.FAIL + "vendre" + bcolors.ENDC + " ? [" + bcolors.OKGREEN + "a" + bcolors.ENDC + "/" + bcolors.FAIL + "v" + bcolors.ENDC + "] ")
|
|
41
|
+ if (typeInput.lower() == 'a'):
|
|
42
|
+ self.type = scan_types.BUY
|
|
43
|
+ else:
|
|
44
|
+ self.type = scan_types.SELL
|
|
45
|
+
|
|
46
|
+ def get_password(self):
|
|
47
|
+ with open('pass') as f:
|
|
48
|
+ password = f.readline()
|
|
49
|
+ return password.strip()
|
|
50
|
+
|
|
51
|
+ def display_type(self):
|
|
52
|
+ if (self.type == scan_types.SELL):
|
|
53
|
+ print(" ==> Mode " + bcolors.FAIL + bcolors.BOLD + "VENTE")
|
|
54
|
+ else:
|
|
55
|
+ print(" ==> Mode " + bcolors.OKGREEN + bcolors.BOLD + "ACHAT")
|
|
56
|
+ print(bcolors.ENDC)
|
|
57
|
+
|
|
58
|
+ def scan_product(self, code):
|
|
59
|
+ data = {
|
|
60
|
+ 'password': self.password,
|
|
61
|
+ 'action': 'scan',
|
|
62
|
+ 'data': str(code)
|
|
63
|
+ }
|
|
64
|
+ r = requests.post(url=API_ENDPOINT, data=json.dumps(data))
|
|
65
|
+ if (r.json()['status'] == 0):
|
|
66
|
+ article = r.json()["data"]
|
|
67
|
+ self.scannedArticles.append(article)
|
|
68
|
+ return r.json()['status'] == 0
|
|
69
|
+
|
|
70
|
+ def display_cart(self):
|
|
71
|
+ total = 0.0
|
|
72
|
+ for article in self.scannedArticles:
|
|
73
|
+ print(article["name"] + ' : ' + bcolors.BOLD + article["price"] + '€' + bcolors.ENDC)
|
|
74
|
+ total += float(article["price"])
|
|
75
|
+ # Print only to only 2 decimals
|
|
76
|
+ total_display = "{:.2f}".format(total)
|
|
77
|
+ print(bcolors.OKGREEN + "Total: " + bcolors.BOLD + total_display + '€' + bcolors.ENDC)
|
|
78
|
+
|
|
79
|
+ def send_cart(self):
|
|
80
|
+ scanned_list = []
|
|
81
|
+ modifier = -1 if self.type == scan_types.SELL else 1
|
|
82
|
+ for article in self.scannedArticles:
|
|
83
|
+ scanned_list.append({"id": article["id"], "quantity": modifier})
|
|
84
|
+ data = {
|
|
85
|
+ 'password': self.password,
|
|
86
|
+ 'action': 'validate',
|
|
87
|
+ 'data': scanned_list
|
|
88
|
+ }
|
|
89
|
+ r = requests.post(url=API_ENDPOINT, data=json.dumps(data))
|
|
90
|
+ return r.json()['status'] == 0
|
|
91
|
+
|
|
92
|
+def ask_confirmation(message):
|
|
93
|
+ confirm_input = input(message)
|
|
94
|
+ return confirm_input.lower() == 'o'
|
|
95
|
+
|
|
96
|
+def clear_screen():
|
|
97
|
+ os.system('clear')
|
|
98
|
+ print("Appuyez sur " + bcolors.BOLD + "[CTRL + C]" + bcolors.ENDC + " à tout moment pour quitter.\n")
|
|
99
|
+
|
|
100
|
+def printStartScreen():
|
|
101
|
+ clear_screen()
|
|
102
|
+ print(bcolors.BOLD)
|
|
103
|
+ print(bcolors.WARNING + "#########################################")
|
|
104
|
+ print("#/ \#")
|
|
105
|
+ print("# " + bcolors.FAIL + "-=|" + bcolors.OKGREEN + " ZAPETTE " + bcolors.FAIL + "|=-" + bcolors.WARNING + " #")
|
|
106
|
+ print("#\ /#")
|
|
107
|
+ print("#########################################")
|
|
108
|
+ print(bcolors.ENDC)
|
|
109
|
+ print("Bienvenue dans le programme de la Zapette !")
|
|
110
|
+
|
|
111
|
+def display_scan_header(scanner, last_error):
|
|
112
|
+ clear_screen()
|
|
113
|
+ scanner.display_type()
|
|
114
|
+ print("Scannez le codes puis appuyez sur [ENTRÉE] pour valider.")
|
|
115
|
+ print("Appuyez sur [ENTRÉE] sans code pour valider la commande.\n")
|
|
116
|
+ scanner.display_cart()
|
|
117
|
+ if (last_error == error_types.URL):
|
|
118
|
+ print(bcolors.FAIL + "Format URL invalide !" + bcolors.ENDC)
|
|
119
|
+ elif (last_error == error_types.NETWORK):
|
|
120
|
+ print(bcolors.FAIL + "URL invalide !" + bcolors.ENDC)
|
|
121
|
+ elif (last_error == error_types.INPUT):
|
|
122
|
+ print(bcolors.FAIL + "Code invalide !" + bcolors.ENDC)
|
|
123
|
+ elif (last_error == error_types.NO_EXIST):
|
|
124
|
+ print(bcolors.FAIL + "L'article n'existe pas." + bcolors.ENDC)
|
|
125
|
+ print()
|
|
126
|
+
|
|
127
|
+def confirm_end_scan(scanner):
|
|
128
|
+ display_scan_header(scanner, error_types.NONE)
|
|
129
|
+ return ask_confirmation("Voulez vous vraiment terminer et envoyer les modifications ? [o/n] ")
|
|
130
|
+
|
|
131
|
+def handler(signal_received, frame):
|
|
132
|
+ os.system('clear')
|
|
133
|
+ print('Programme de la zapette terminé.')
|
|
134
|
+ exit(0)
|
|
135
|
+
|
|
136
|
+def validate_cart(scanner):
|
|
137
|
+ clear_screen()
|
|
138
|
+ print("Envoi des modifications au serveur...")
|
|
139
|
+ if (scanner.send_cart()):
|
|
140
|
+ print(bcolors.OKGREEN + bcolors.BOLD + "Succès !" + bcolors.ENDC)
|
|
141
|
+ else:
|
|
142
|
+ print(bcolors.FAIL + bcolors.BOLD + "Échec !" + bcolors.ENDC)
|
|
143
|
+ input("\nAppuyez sur [ENTRÉE] pour continuer...")
|
23
|
144
|
|
24
|
145
|
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 !")
|
|
146
|
+ signal(SIGINT, handler)
|
|
147
|
+ printStartScreen()
|
|
148
|
+ while True:
|
|
149
|
+ scanner = Scanner()
|
|
150
|
+ last_error = error_types.NONE
|
|
151
|
+ while True:
|
|
152
|
+ display_scan_header(scanner, last_error)
|
|
153
|
+ code_input = input('=> ')
|
|
154
|
+ if (code_input == ""):
|
|
155
|
+ if (confirm_end_scan(scanner)):
|
|
156
|
+ validate_cart(scanner)
|
|
157
|
+ break
|
|
158
|
+ else:
|
|
159
|
+ continue
|
|
160
|
+ try:
|
|
161
|
+ code = int(code_input)
|
|
162
|
+ if (scanner.scan_product(code)):
|
|
163
|
+ last_error = error_types.NONE
|
|
164
|
+ else:
|
|
165
|
+ last_error = error_types.NO_EXIST
|
|
166
|
+ except requests.exceptions.MissingSchema:
|
|
167
|
+ last_error = error_types.URL
|
|
168
|
+ except requests.exceptions.ConnectionError:
|
|
169
|
+ last_error = error_types.NETWORK
|
|
170
|
+ except ValueError:
|
|
171
|
+ last_error = error_types.INPUT
|
|
172
|
+ clear_screen()
|
36
|
173
|
|
37
|
174
|
main()
|