Niv Sardi commited on
Commit
cf048df
1 Parent(s): 4b45b50

python/vendor: use REST api for screenshoting

Browse files
Files changed (1) hide show
  1. python/vendor.py +22 -15
python/vendor.py CHANGED
@@ -7,32 +7,39 @@ from progress.bar import ChargingBar
7
 
8
  from entity import Entity
9
  from common import defaults,mkdir
10
- import screenshot
11
  import web
12
 
13
  PARALLEL = 20
14
 
15
- def query_vendor_site(e: Entity):
16
- fn = web.get_cert(e)
17
- lfn = web.get_logos(e)
18
- sfn = screenshot.sc_entity(e)
19
- return (fn, lfn, sfn)
 
 
 
 
20
 
21
  def from_csv(fn: str, n_workers = PARALLEL):
 
22
  with open(fn, newline='') as csvfile:
23
  reader = csv.DictReader(csvfile)
24
  with concurrent.futures.ThreadPoolExecutor(max_workers = n_workers) as executor:
25
- futures = {executor.submit(query_vendor_site, e): e for e in [Entity.from_dict(d) for d in reader]}
26
- bar = ChargingBar('Processing', max=len(futures))
 
 
 
 
 
 
27
  for f in concurrent.futures.as_completed(futures):
28
- url = futures[f]
29
  try:
30
- (cert, logos) = f.result()
31
- except Exception as exc:
32
- print('%r generated an exception: %s' % (url, exc))
33
- raise
34
- else:
35
- print(cert, logos)
36
  bar.next()
37
  bar.finish()
38
 
 
7
 
8
  from entity import Entity
9
  from common import defaults,mkdir
 
10
  import web
11
 
12
  PARALLEL = 20
13
 
14
+ def do_screenshot(e: Entity):
15
+ sfn = requests.post('http://puppet:8000/screenshot', json={
16
+ 'url': e.url,
17
+ 'id': e.id,
18
+ 'path': f'{defaults.SCREENSHOT_PATH}/{e.bco}.png',
19
+ 'logos': f'{defaults.LOGOS_DATA_PATH}/{e.bco}.png'
20
+ })
21
+
22
+ ACTIONS = [web.get_cert, web.get_logos, do_screenshot]
23
 
24
  def from_csv(fn: str, n_workers = PARALLEL):
25
+ mkdir.make_dirs([defaults.SCREENSHOT_PATH])
26
  with open(fn, newline='') as csvfile:
27
  reader = csv.DictReader(csvfile)
28
  with concurrent.futures.ThreadPoolExecutor(max_workers = n_workers) as executor:
29
+ futures = {}
30
+ entities = [Entity.from_dict(d) for d in reader]
31
+ bar = ChargingBar('vendor', max=len(entities*len(ACTIONS)))
32
+
33
+ for e in entities:
34
+ futures.update({executor.submit(f, e): (e, f) for f in ACTIONS})
35
+ print('waiting for futures')
36
+
37
  for f in concurrent.futures.as_completed(futures):
38
+ (e, a) = futures[f]
39
  try:
40
+ f.result()
41
+ except Exception as err:
42
+ print(f'{a}({e.url}) generated an exception: {err}')
 
 
 
43
  bar.next()
44
  bar.finish()
45