-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
50 lines (40 loc) · 1.35 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import queue
import _thread
import urllib.request
import configparser
import xml.etree.ElementTree as et
q = queue.Queue()
config = configparser.ConfigParser()
cmd = ""
def worker(threadName, _api):
api = _api.strip()
global cmd
while cmd.lower() != "quit":
if not q.empty():
code = q.get(True, 50).strip()
output = urllib.request.urlopen("http://api.upcdatabase.org/xml/" + api + "/" + code).read()
data = et.fromstring(output)
if str(data[0].text).lower() == "true":
name = data[2].text
desc = data[4].text
if name is None or len(name) <= 0:
if desc is None or len(desc) <= 0:
name = "Unknown Product: " + code
else:
name = desc.strip()
print(name)
else:
print("Unregistered Product: " + code)
def scanner():
global cmd
while cmd.lower() != "quit":
cmd = input()
q.put(cmd, True, 50)
def main():
config.read("config.ini")
api = str(config["upcdatabase"]["api"])
print("Program started...")
worker_thread = _thread.start_new_thread(worker, ("worker", api))
scanner() # This contains loop for main thread
if __name__ == "__main__":
main()