Commit 986c4a2f authored by abuddenberg's avatar abuddenberg
Browse files

First try at client for Angels metadata survey

parent 00c019e6
from gcis_client import GcisClient, AssociationException
from webform_client import WebformClient
from nca3_client import Nca3Client
from survey_client import SurveyClient
from os.path import expanduser, exists
from os import makedirs, getenv
......@@ -19,4 +20,6 @@ gcis_dev_auth = (getenv('GCIS_DEV_USER'), getenv('GCIS_DEV_KEY'))
gcis_auth = (getenv('GCIS_USER'), getenv('GCIS_KEY'))
webform_token = getenv('WEBFORM_TOKEN')
\ No newline at end of file
webform_token = getenv('WEBFORM_TOKEN')
survey_token = getenv('SURVEY_TOKEN')
\ No newline at end of file
......@@ -57,6 +57,9 @@ class Gcisbase(object):
class GcisObject(Gcisbase):
def __init__(self, data, **kwargs):
if type(data) is not dict:
raise TypeError('Expected dict, got {t}'.format(t=type(data)))
#Special case for contributors
contrib_list = data.pop('contributors', None)
self.contributors = [Contributor(contrib) for contrib in contrib_list] if contrib_list else []
__author__ = 'abuddenberg'
import getpass
import requests
import re
from gcis_clients.domain import Figure
def get_credentials():
#First check our magic enviroment variable (SURVEY_TOKEN)
from gcis_clients import webform_token
if webform_token is not None:
return webform_token
return getpass.getpass('Survey token: ')
def parse_title(graphic_title):
match ='\d+\.\d+', graphic_title)
if match:
return, graphic_title[match.end(0):].strip()
return None, graphic_title
class SurveyClient:
def __init__(self, url, token, local_image_dir=None, remote_dir='/system/files/'):
self.base_url = url
#If token was not provided, obtain it
if token is None:
token = get_credentials()
self.token = token
if local_image_dir:
self.images_dir = local_image_dir
from gcis_clients import default_image_dir
self.images_dir = default_image_dir()
self.remote_image_dir = remote_dir
def get_list(self):
url = '{b}/metadata/list?token={t}'.format(b=self.base_url, t=self.token)
return requests.get(url).json()
def get_survey(self, fig_url, download_images=False):
full_url = '{b}{url}?token={t}'.format(b=self.base_url, url=fig_url, t=self.token)
survey_json = requests.get(full_url).json()
fig_json = survey_json[0]['t1']['figure']
#It's not worth trying to translations on this data; it's too different
f = Figure({})
f.figure_num, f.title = parse_title(fig_json['graphics_title'])
f.identifier = fig_json['figure_id'] if fig_json['figure_id'] else re.sub('\W', '_', f.title).lower()
f.time_start, f.time_end = fig_json['period_record']
f.lat_min, f.lat_max, f.lon_min, f.lon_max = fig_json['spatial_extent']
f.create_dt = fig_json['graphics_create_date']
__blah = [
'report_identifier', 'chapter', 'submission_dt',
'source_citation', 'attributes', 'chapter_identifier', 'images'
return f
......@@ -175,4 +175,4 @@ def test_dataset_special_properties():
if __name__ == "__main__" and __package__ is None:
__package__ = "gcis_client.test.test_suite"
# test_get_credentials_modes()
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment