#!/usr/bin/env python3 """Job #1500 prior-art channel probe (read-only, no credentials). Channels reachable from this computer, each with a calibration query whose known positive must come back in the same run: * arXiv API (metadata only: title/abstract/authors, conjunctive) * zbMATH API (bibliographic + review text; HTTP 404 = a NEGATIVE on this API, per research/SEARCH-CONVENTIONS.md section 5) Every raw reply is written to work/job1500-searches/ and hashed; the summary prints one line per query with the number of hits and the first few titles. Nothing is interpreted here: the report does the reading. """ import hashlib import json import os import re import time import urllib.error import urllib.parse import urllib.request HERE = os.path.dirname(os.path.abspath(__file__)) OUT = os.path.join(HERE, 'job1500-searches') os.makedirs(OUT, exist_ok=True) UA = 'solveathome-prior-art/1.0 (research; contact: department agent)' ARXIV = [ # calibration ('CAL arxiv: ti:"Jacobsthal function"', 'ti:"Jacobsthal function"'), # object: BV for the Mobius function at level 1/2 ('arxiv: all:"Bombieri-Vinogradov" AND all:"Mobius function"', 'all:"Bombieri-Vinogradov" AND all:"Mobius function"'), ('arxiv: all:"level of distribution" AND all:"Mobius function"', 'all:"level of distribution" AND all:"Mobius function"'), ('arxiv: abs:"Mobius function" AND abs:"arithmetic progressions"', 'abs:"Mobius function" AND abs:"arithmetic progressions"'), ('arxiv: ti:"Bombieri-Vinogradov" AND all:"multiplicative functions"', 'ti:"Bombieri-Vinogradov" AND all:"multiplicative functions"'), ('arxiv: all:"Motohashi" AND all:"induction principle"', 'all:"Motohashi" AND all:"induction principle"'), ('arxiv: all:"prefix" AND all:"Bombieri-Vinogradov"', 'all:"prefix" AND all:"Bombieri-Vinogradov"'), ('arxiv: all:"Granville" AND all:"Shao" AND all:"Bombieri"', 'all:"Granville" AND all:"Shao" AND all:"Bombieri"'), ] ZBMATH = [ ('CAL zbmath: Jacobsthal function', 'Jacobsthal function'), ('zbmath: Moebius function arithmetic progressions Bombieri', 'Moebius function arithmetic progressions Bombieri'), ('zbmath: level of distribution Moebius function', 'level of distribution Moebius function'), ('zbmath: Bombieri-Vinogradov multiplicative functions', 'Bombieri-Vinogradov multiplicative functions'), ('zbmath: Motohashi induction principle Bombieri', 'Motohashi induction principle Bombieri'), ] def fetch(url, tries=2, timeout=45): last = None for _ in range(tries): try: req = urllib.request.Request(url, headers={'User-Agent': UA}) with urllib.request.urlopen(req, timeout=timeout) as r: return r.status, r.read() except urllib.error.HTTPError as e: return e.code, e.read() except Exception as e: # network hiccup last = repr(e) time.sleep(3) return None, last.encode() def sha256(b): return hashlib.sha256(b).hexdigest() def titles(xml): out = [] for entry in re.findall(r'(.*?)', xml, re.S): t = re.search(r'(.*?)', entry, re.S) i = re.search(r'(.*?)', entry, re.S) a = re.findall(r'(.*?)', entry) if t: out.append({ 'title': re.sub(r'\s+', ' ', t.group(1)).strip(), 'id': (i.group(1).strip() if i else ''), 'authors': ', '.join(a[:3]), }) return out def main(): record = {'arxiv': [], 'zbmath': []} for label, q in ARXIV: url = ('http://export.arxiv.org/api/query?search_query=' + urllib.parse.quote(q) + '&start=0&max_results=20' + '&sortBy=relevance') status, body = fetch(url) name = 'arxiv-%02d.xml' % (len(record['arxiv']) + 1) with open(os.path.join(OUT, name), 'wb') as f: f.write(body) ts = titles(body.decode('utf-8', 'replace')) if status == 200 else [] rec = {'label': label, 'query': q, 'url': url, 'status': status, 'file': name, 'sha256': sha256(body), 'bytes': len(body), 'hits': len(ts), 'titles': ts} record['arxiv'].append(rec) print('%-64s status=%s hits=%d sha=%s' % (label[:64], status, len(ts), rec['sha256'][:12])) for t in ts[:6]: print(' - %s | %s | %s' % (t['id'].split('/abs/')[-1], t['title'][:96], t['authors'][:60])) time.sleep(3.5) for label, q in ZBMATH: url = ('https://api.zbmath.org/v1/document/_search?search_string=' + urllib.parse.quote(q) + '&results_per_page=20&page=0') status, body = fetch(url) name = 'zbmath-%02d.json' % (len(record['zbmath']) + 1) with open(os.path.join(OUT, name), 'wb') as f: f.write(body) hits = [] if status == 200: try: d = json.loads(body.decode('utf-8', 'replace')) for r in (d.get('result') or [])[:20]: hits.append({ 'title': (r.get('title') or {}).get('title') if isinstance(r.get('title'), dict) else r.get('title'), 'year': r.get('year'), 'source': r.get('source'), 'zbmath_url': r.get('zbmath_url'), }) except Exception as e: hits = [{'parse_error': repr(e)}] rec = {'label': label, 'query': q, 'url': url, 'status': status, 'file': name, 'sha256': sha256(body), 'bytes': len(body), 'hits': len(hits), 'results': hits} record['zbmath'].append(rec) print('%-64s status=%s hits=%d sha=%s' % (label[:64], status, len(hits), rec['sha256'][:12])) for h in hits[:6]: print(' - %s (%s) %s' % (str(h.get('title'))[:88], h.get('year'), str(h.get('source'))[:60])) time.sleep(1.5) path = os.path.join(OUT, 'summary.json') with open(path, 'w') as f: json.dump(record, f, indent=1) print('SUMMARY', path, sha256(json.dumps(record, sort_keys=True).encode())) if __name__ == '__main__': main()