26 lines
978 B
Python
26 lines
978 B
Python
import sys, json, time, urllib.parse, urllib.request
|
|
UA = "RoboMovelDeckBot/1.0 (educational presentation; contact: student@unicap.br)"
|
|
def q(term, limit=6):
|
|
params = {
|
|
"action":"query","format":"json","generator":"search",
|
|
"gsrsearch":term,"gsrnamespace":"6","gsrlimit":str(limit),
|
|
"prop":"imageinfo","iiprop":"url|mime|size",
|
|
}
|
|
url = "https://commons.wikimedia.org/w/api.php?" + urllib.parse.urlencode(params)
|
|
req = urllib.request.Request(url, headers={"User-Agent":UA})
|
|
try:
|
|
with urllib.request.urlopen(req, timeout=30) as r:
|
|
d = json.load(r)
|
|
except Exception as e:
|
|
print("ERR", term, e); return
|
|
pages = d.get("query",{}).get("pages",{})
|
|
print("=== "+term+" ===")
|
|
for v in pages.values():
|
|
ii = (v.get("imageinfo") or [{}])[0]
|
|
print(v.get("title"),"|",ii.get("mime"),"|",ii.get("size"),"|",ii.get("url"))
|
|
time.sleep(3.5)
|
|
|
|
terms = sys.argv[1:]
|
|
for t in terms:
|
|
q(t)
|