Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

new formats and sizes if availiable #13

Merged
merged 3 commits into from
Nov 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 60 additions & 7 deletions bodsdata.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
from textwrap import dedent
import ijson
import flatterer
import humanize


import boto3
Expand Down Expand Up @@ -292,7 +293,7 @@ def export_bigquery(source, parquet_path, table_name):

with open(parquet_path, "rb") as source_file:
client.load_table_from_file(
source_file, table_id, job_config=job_config, size=None, timeout=5
source_file, table_id, job_config=job_config, size=None, timeout=50
)


Expand Down Expand Up @@ -628,9 +629,33 @@ def create_samples(source, upload=False, size=10):
if upload:
bucket_location = f"data/{source}/samples.json"
upload_s3(filepath, bucket_location)
shutil.rmtree(f'{output_dir}/{source}/parquet')

return df_output


def create_parquet_zip(source, upload=False):
print('Create parquet zip')
with open(f'{output_dir}/{source}/datapackage.json', 'r') as f:
datapackage = json.load(f)

with zipfile.ZipFile(f'{output_dir}/{source}/parquet.zip', 'w', compression=zipfile.ZIP_DEFLATED) as f_zip:
for resource in datapackage['resources']:

output_file = f'{output_dir}/{source}/parquet/{resource["name"]}.parquet'

f_zip.write(
output_file,
arcname=f'{resource["name"]}.parquet',
)

os.unlink(output_file)

filepath = f'{output_dir}/{source}/parquet.zip'

if upload:
bucket_location = f"data/{source}/parquet.zip"
upload_s3(filepath, bucket_location)
shutil.rmtree(f'{output_dir}/{source}/parquet')


def download_file(url, source, name=None):
Expand Down Expand Up @@ -665,7 +690,7 @@ def download_file(url, source, name=None):
os.remove(filename)


def download_files_s3(source, s3_path_pattern, latest=False, bucket="bodsdata-oo"):
def download_files_s3(source, s3_path_pattern, latest=False, bucket="bodsdata-oo", sample=None):
""" Download file to form s3 with given regex pattern.

Parameters
Expand Down Expand Up @@ -694,9 +719,29 @@ def download_files_s3(source, s3_path_pattern, latest=False, bucket="bodsdata-oo
if latest and items:
items = [items[-1]]

for item in items:
for num, item in enumerate(items):
file_name = item.split('/')[-1]
bucket.download_file(item, f'{output_dir}/{source}_download/{file_name}')
if sample and num == sample:
break


def json_zip(source, upload=False):
print("Making json.zip")
with zipfile.ZipFile(f'{output_dir}/{source}/json.zip', 'w', compression=zipfile.ZIP_DEFLATED) as f_zip:
with f_zip.open( f'{source}.json', 'w') as output_file:
for item in glob.glob(f'{output_dir}/{source}_download/*'):
with open(item, 'rb') as input_file:
for line in input_file:
output_file.write(line)

os.unlink(item)

filepath = f'{output_dir}/{source}/json.zip'
if upload:
bucket_location = f"data/{source}/json.zip"
upload_s3(filepath, bucket_location)
os.unlink(filepath)


def remove_download(source):
Expand Down Expand Up @@ -834,17 +879,25 @@ def publish_metadata(source, title="", description="", upload=True):
if parts[0] != 'data' or parts[1] != source:
continue


if file_name.endswith("csv.zip"):
out["csv"] = item_url
out["csv_size"] = humanize.naturalsize(item.size)
if file_name.endswith("sqlite.zip"):
out["sqlite_zip"] = item_url
out["sqlite_zip_size"] = humanize.naturalsize(item.size)
if file_name.endswith("sqlite.db.gz"):
out["sqlite_gzip"] = item_url
out["sqlite_gzip_size"] = humanize.naturalsize(item.size)
if file_name.endswith("sql.gz"):
out["pg_dump"] = item_url
if file_name.endswith("parquet"):
out["parquet"][file_name] = item_url
out["pg_dump_size"] = humanize.naturalsize(item.size)
if file_name.endswith("parquet.zip"):
out["parquet_zip"] = item_url
out["parquet_zip_size"] = humanize.naturalsize(item.size)
if file_name.endswith("json.zip"):
out["json_zip"] = item_url
out["json_zip_size"] = humanize.naturalsize(item.size)


with open(f'{output_dir}/{source}/datapackage.json') as samples_file:
out['datapackage'] = json.load(samples_file)
Expand Down
8 changes: 8 additions & 0 deletions bodsdataweb/templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,14 @@ <h4 class="fw-bolder">Beneficial ownership data analysis tools</h4>
{% block scripts %}
<!-- Optional JavaScript -->
{{ bootstrap.load_js() }}
<script>
var tooltipTriggerList = [].slice.call(document.querySelectorAll('[data-bs-toggle="tooltip"]'))
var tooltipList = tooltipTriggerList.map(function (tooltipTriggerEl) {
return new bootstrap.Tooltip(tooltipTriggerEl)
})


</script>
{% endblock %}
</body>
</html>
15 changes: 12 additions & 3 deletions bodsdataweb/templates/home.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,18 @@ <h5 class="serif" >Discover, download and analyse sources of beneficial ownershi
{{ info.description_html|safe }}
</p>
<a href="{{ url_for('source', source=source) }}" class="card-link">Field Information</a>
<a href="{{ info.csv }}" class="card-link">CSV Download</a>
<a href="{{ info.sqlite_zip }}" class="card-link">SQLite Download</a>
<a href="{{ info.pg_dump }}" class="card-link">Postgresql Dump (gzip)</a>
<a href="{{ info.csv }}" class="card-link" {% if info.csv_size %}data-bs-toggle="tooltip" title="{{info.csv_size}}"{% endif %}>CSV Download</a>
<a href="{{ info.sqlite_zip }}" class="card-link" {% if info.sqlite_zip_size %}data-bs-toggle="tooltip" title="{{info.sqlite_zip_size}}"{% endif %}>SQLite Download</a>
<a href="{{ info.pg_dump }}" class="card-link" {% if info.pg_dump_size %}data-bs-toggle="tooltip" title="{{info.pg_dump_size}}"{% endif %}>Postgresql Dump (gzip)</a>

{% if info.json_zip %}
<a href="{{ info.json_zip }}" class="card-link" {% if info.json_zip_size %}data-bs-toggle="tooltip" title="{{info.json_zip_size}}"{% endif %}>JSON Download</a>
{% endif %}

{% if info.parquet_zip %}
<a href="{{ info.parquet_zip }}" class="card-link" {% if info.parquet_zip_size %}data-bs-toggle="tooltip" title="{{info.parquet_zip_size}}"{% endif %}>Parquet Download</a>
{% endif %}

<a href="https://console.cloud.google.com/bigquery?project=ocdsdata&p=bodsdata&d={{source}}&page=dataset" class="card-link">Big Query</a>
<a href="https://bods-data-datasette.openownership.org/{{source}}" class="card-link">Datasette</a>
</div>
Expand Down
15 changes: 12 additions & 3 deletions bodsdataweb/templates/source.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,18 @@ <h2 id="title" class="serif" >{{ metadata.title }} <small>({{metadata.update_dat
<div class="card" >
<div class="card-body">
<h4 id="downloads" class="card-title serif">Downloads and links to hosted databases</h4> </a>
<a href="{{ metadata.csv }}" class="card-link">CSV Download</a>
<a href="{{ metadata.sqlite_zip }}" class="card-link">SQLite Download</a>
<a href="{{ metadata.pg_dump }}" class="card-link">Postgresql Dump (gzip)</a>
<a href="{{ metadata.csv }}" class="card-link" {% if metadata.csv_size %}data-bs-toggle="tooltip" title="{{metadata.csv_size}}"{% endif %}>CSV Download</a>
<a href="{{ metadata.sqlite_zip }}" class="card-link" {% if metadata.sqlite_zip_size %}data-bs-toggle="tooltip" title="{{metadata.sqlite_zip_size}}"{% endif %}>SQLite Download</a>
<a href="{{ metadata.pg_dump }}" class="card-link" {% if metadata.pg_dump_size %}data-bs-toggle="tooltip" title="{{metadata.pg_dump_size}}"{% endif %}>Postgresql Dump (gzip)</a>

{% if metadata.json_zip %}
<a href="{{ metadata.json_zip }}" class="card-link" {% if metadata.json_zip_size %}data-bs-toggle="tooltip" title="{{metadata.json_zip_size}}"{% endif %}>JSON Download</a>
{% endif %}

{% if metadata.parquet_zip %}
<a href="{{ metadata.parquet_zip }}" class="card-link" {% if metadata.parquet_zip_size %}data-bs-toggle="tooltip" title="{{metadata.parquet_zip_size}}"{% endif %}>Parquet Download</a>
{% endif %}

<a href="https://console.cloud.google.com/bigquery?project=ocdsdata&p=bodsdata&d={{source}}&page=dataset" class="card-link">Big Query</a>
<a href="https://bods-data-datasette.openownership.org/{{source}}" class="card-link">Datasette</a>
</div>
Expand Down
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
"frozen-flask",
"bootstrap-flask",
"markdown",
"datasette"
"datasette",
"humanize"
]


Expand Down