Explorer
/proc/117/root/tmp/check_image_urls.py
← Zurück ↓ Download
import sqlite3,json,urllib.request,urllib.error,concurrent.futures
con=sqlite3.connect("file:/var/lib/sma-data/signals.db?mode=ro",uri=True); con.row_factory=sqlite3.Row
rows=con.execute("select signal_id,source_type,source_name,image_url,image_found,source_url,topic from signals where observed_at >= datetime('now','-30 days') and image_url is not null and image_url != ''").fetchall(); con.close()
def check(r):
 u=r['image_url']
 try:
  req=urllib.request.Request(u,headers={'User-Agent':'Mozilla/5.0'},method='GET')
  with urllib.request.urlopen(req,timeout=12) as x:
   b=x.read(64); return {'id':r['signal_id'],'url':u,'status':x.status,'ctype':x.headers.get('Content-Type',''),'bytes_head':len(b),'source_type':r['source_type'],'found':r['image_found']}
 except Exception as e: return {'id':r['signal_id'],'url':u,'status':None,'error':str(e),'source_type':r['source_type'],'found':r['image_found']}
with concurrent.futures.ThreadPoolExecutor(max_workers=12) as ex: out=list(ex.map(check,rows))
from collections import Counter
print('TOTAL',len(out),'STATUS',Counter(str(x.get('status')) for x in out),'CTYPE',Counter(x.get('ctype','').split(';')[0] for x in out))
print(json.dumps(out,ensure_ascii=False,indent=2))