-
Notifications
You must be signed in to change notification settings - Fork 1
/
ingest_newspaper_metadata.py
39 lines (27 loc) · 1.24 KB
/
ingest_newspaper_metadata.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import os
import sys
import json
import pandas as pd
import numpy as np
if len(sys.argv) != 4:
print('''Usage: python {} METADATA_FILE DB_URI OUTPUT_TABLE
The metadata file should be JSON extracted from the newseum website (schema as of 4/6/17).
The DB_URI should be in sqlalchemy format.
Newspaper metadata from the file will be combined with existing newspapers in the output table.
'''.format(__file__))
sys.exit(1)
METADATA_FILE, DB_URI, OUTPUT_TABLE = sys.argv[1:]
try:
existing_metadata = pd.read_sql_table(OUTPUT_TABLE, DB_URI)
except ValueError:
existing_metadata = pd.DataFrame()
print('Previous number of newspapers in DB: {}'.format(existing_metadata.shape[0]))
with open(METADATA_FILE) as f:
metadata = json.load(f)
metadata_df = pd.DataFrame(metadata)
metadata_df['longitude'] = metadata_df['longitude'].apply(float)
metadata_df['latitude'] = metadata_df['latitude'].apply(float)
metadata_df.rename(columns={'paperId': 'slug'}, inplace=True) # To avoid capitalization issues with postgres
df = pd.concat([metadata_df, existing_metadata], ignore_index=True).drop_duplicates(subset=['slug'])
print('Current number of newspapers in DB: {}'.format(df.shape[0]))
df.to_sql(OUTPUT_TABLE, DB_URI, if_exists='replace', index=False)