|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +import cgi |
| 4 | +import json |
| 5 | +import sys |
| 6 | + |
| 7 | +import mysql.connector |
| 8 | + |
| 9 | +print("Content-Type: text/plain; charset=utf-8") |
| 10 | +print() |
| 11 | + |
| 12 | +form = cgi.FieldStorage() |
| 13 | + |
| 14 | +if "topics" not in form: |
| 15 | + topics = ["should"] |
| 16 | +else: |
| 17 | + topics = form.getfirst("topics", "[]") |
| 18 | + topics = json.loads(topics) |
| 19 | + if not topics: |
| 20 | + print("error") |
| 21 | + sys.exit(1) |
| 22 | +try: |
| 23 | + with mysql.connector.connect(user="root", password="", database="public") as db: |
| 24 | + with db.cursor(buffered=True) as cursor: |
| 25 | + select = "SELECT eprintid, id_number, edition, number, title, abstract FROM eprint " |
| 26 | + where = "WHERE eprint_status='archive' AND " |
| 27 | + for idx, topic in enumerate(topics): |
| 28 | + if idx != len(topics) - 1: |
| 29 | + where += f"keywords LIKE '%{topic}%' AND " |
| 30 | + else: |
| 31 | + where += f"keywords LIKE '%{topic}%' " |
| 32 | + order = "ORDER BY keywords DESC;" |
| 33 | + cursor.execute(select + where + order) |
| 34 | + results = {} |
| 35 | + for (eprintid, id_number, edition, number, title, abstract) in cursor: |
| 36 | + id_number = id_number.decode() if isinstance(id_number, bytes) else id_number |
| 37 | + edition = edition.decode() if isinstance(edition, bytes) else edition |
| 38 | + number = number.decode() if isinstance(number, bytes) else number |
| 39 | + title = title.decode() if isinstance(title, bytes) else title |
| 40 | + abstract = abstract.decode() if isinstance(abstract, bytes) else abstract |
| 41 | + results[eprintid] = [eprintid, id_number, edition, number, title, abstract] |
| 42 | + eprintids = results.keys() |
| 43 | + if eprintids: |
| 44 | + eprintids = "(" + ",".join(map(str, eprintids)) + ")" |
| 45 | + select = "SELECT eprintid, pos, main FROM document " |
| 46 | + where = f"WHERE eprintid IN {eprintids} AND placement=1 AND format='image' AND security='public';" |
| 47 | + cursor.execute(select + where) |
| 48 | + for (eprintid, pos, main) in cursor: |
| 49 | + results[eprintid].append(main.decode() if isinstance(main, bytes) else main) |
| 50 | + results[eprintid].append(pos.decode() if isinstance(pos, bytes) else pos) |
| 51 | + results = json.dumps(results) |
| 52 | + print(results) |
| 53 | + print() |
| 54 | +except Exception as e: |
| 55 | + print("error") |
| 56 | + print(str(e)) |
0 commit comments