|
| 1 | +import pandas as pd |
| 2 | +import yaml |
| 3 | +from glob import glob |
| 4 | +from uuid import uuid5, NAMESPACE_URL |
| 5 | +from uuid import uuid5, NAMESPACE_URL |
| 6 | +from s3_update import backup_file |
| 7 | +from ingest_common import connection |
| 8 | +import io |
| 9 | +import csv |
| 10 | +dccs = pd.read_csv('https://cfde-drc.s3.amazonaws.com/database/files/current_dccs.tsv', sep="\t", index_col=0, header=0) |
| 11 | +# map dcc names to their respective ids |
| 12 | +dcc_mapper = {} |
| 13 | +for i, v in dccs.loc[:,'short_label'].items(): |
| 14 | + dcc_mapper[v] = i |
| 15 | +data = {} |
| 16 | +center_publication = [] |
| 17 | +for filename in glob('../../src/pages/centers/*.md'): |
| 18 | + with open(filename) as o: |
| 19 | + markdown = o.read() |
| 20 | + m = markdown.split("---") |
| 21 | + row = yaml.safe_load(m[1]) |
| 22 | + if "label" in row: |
| 23 | + label = row['label'] |
| 24 | + uid = str(uuid5(NAMESPACE_URL, label)) |
| 25 | + data[uid] = { |
| 26 | + "label": row["label"], |
| 27 | + "short_label": row.get("short_label"), |
| 28 | + "short_description": row.get("short_description"), |
| 29 | + "description": row.get("description"), |
| 30 | + "homepage": row.get("homepage"), |
| 31 | + "icon": row.get("icon"), |
| 32 | + "grant_num": row.get("grant_num"), |
| 33 | + "active": row.get("active"), |
| 34 | + } |
| 35 | + if row.get("publications"): |
| 36 | + for pub in set(row["publications"]): |
| 37 | + center_publication.append({"center_id": uid, "publication_id": pub}) |
| 38 | + |
| 39 | +center_df = pd.DataFrame.from_dict(data, orient="index").fillna('') |
| 40 | +center_df.index.name = "id" |
| 41 | +center_publication_df = pd.DataFrame.from_records(center_publication, columns=['center_id', 'publication_id']) |
| 42 | + |
| 43 | +## Update S3 |
| 44 | +backup_file(center_df, "centers", quoting=False) |
| 45 | +backup_file(center_publication_df, "center_publication", False) |
| 46 | + |
| 47 | +## ingest |
| 48 | + |
| 49 | +print("ingesting...") |
| 50 | + |
| 51 | +cur = connection.cursor() |
| 52 | + |
| 53 | +cur.execute(''' |
| 54 | + DELETE FROM center_publications; |
| 55 | +''') |
| 56 | + |
| 57 | +cur.execute(''' |
| 58 | + DELETE FROM centers; |
| 59 | +''') |
| 60 | + |
| 61 | +cur.execute(''' |
| 62 | + create table centers_tmp |
| 63 | + as table centers |
| 64 | + with no data; |
| 65 | +''') |
| 66 | + |
| 67 | +p_buf = io.StringIO() |
| 68 | +center_df.to_csv(p_buf, header=True, quoting=csv.QUOTE_NONE, sep="\t") |
| 69 | +p_buf.seek(0) |
| 70 | +columns = next(p_buf).strip().split('\t') |
| 71 | +cur.copy_from(p_buf, 'centers_tmp', |
| 72 | + columns=columns, |
| 73 | + null='', |
| 74 | + sep='\t', |
| 75 | +) |
| 76 | +column_string = ", ".join(columns) |
| 77 | +set_string = ",\n".join(["%s = excluded.%s"%(i,i) for i in columns]) |
| 78 | +cur.execute(''' |
| 79 | + insert into centers (%s) |
| 80 | + select %s |
| 81 | + from centers_tmp |
| 82 | + on conflict (id) |
| 83 | + do update |
| 84 | + set %s |
| 85 | + ; |
| 86 | + '''%(column_string, column_string, set_string)) |
| 87 | +cur.execute('drop table centers_tmp;') |
| 88 | + |
| 89 | + |
| 90 | +cur = connection.cursor() |
| 91 | +cur.execute(''' |
| 92 | + create table center_publications_tmp |
| 93 | + as table center_publications |
| 94 | + with no data; |
| 95 | +''') |
| 96 | + |
| 97 | + |
| 98 | +cp_buf = io.StringIO() |
| 99 | +center_publication_df.to_csv(cp_buf, header=True, sep="\t", index=None) |
| 100 | +cp_buf.seek(0) |
| 101 | +columns = next(cp_buf).strip().split('\t') |
| 102 | +cur.copy_from(cp_buf, 'center_publications_tmp', |
| 103 | + columns=columns, |
| 104 | + null='', |
| 105 | + sep='\t', |
| 106 | +) |
| 107 | + |
| 108 | +column_string = ", ".join(columns) |
| 109 | + |
| 110 | +cur.execute(''' |
| 111 | + insert into center_publications (%s) |
| 112 | + select %s |
| 113 | + from center_publications_tmp |
| 114 | + on conflict |
| 115 | + do nothing |
| 116 | + ; |
| 117 | + '''%(column_string, column_string)) |
| 118 | +cur.execute('drop table center_publications_tmp;') |
| 119 | +connection.commit() |
| 120 | + |
| 121 | +print("Ingested centers") |
| 122 | + |
0 commit comments