sync_figures.py 3.72 KB
Newer Older
1
2
3
4
__author__ = 'abuddenberg'

from webform_client import WebformClient
from gcis_client import GcisClient
5
from os.path import exists
6
import json
7
8
9
10
11
12

# webform_dev = ('http://dev.nemac.org/asides10/metadata/figures/all?token=A2PNYxRuG9')

webform = WebformClient('http://resources.assessment.globalchange.gov', 'mgTD63FAjG')

gcis_url = 'http://data.gcis-dev-front.joss.ucar.edu'
13
14
gcis = GcisClient(gcis_url, 'andrew.buddenberg@noaa.gov', '4cd31dc7173eb47b26f616fb07db607f25ab861552e81195')
# stage = GcisClient('http://data-stage.globalchange.gov', 'andrew.buddenberg@noaa.gov', 'ef427a895acf26d4f0b1f053ba7d922791b76f7852e7efee')
15

16
17
18
19
20
21
22
23
24
25
26
27
sync_metadata_tree = {
    #Reports
    'nca3draft': {
        #Chapters
        'our-changing-climate': [
            #(webform_url, gcis_id)
            # ('/metadata/figures/2506', 'observed-change-in-very-heavy-precipitation'),
            # ('/metadata/figures/2997', 'observed-change-in-very-heavy-precipitation-2'),
            # ('/metadata/figures/2677', 'observed-us-precipitation-change'),
            # ('/metadata/figures/3175', 'observed-us-temperature-change'),
            # ('/metadata/figures/3074', 'ten-indicators-of-a-warming-world'),
            # ('/metadata/figures/3170', 'global-temperature-and-carbon-dioxide'),
28
            # ('/metadata/figures/3293', 'observed-increase-in-frostfree-season-length'),
29
30
            # ('/metadata/figures/3294', 'projected-changes-in-frostfree-season-length'),
            # ('/metadata/figures/3305', 'variation-of-storm-frequency-and-intensity-during-the-cold-season-november--march') #incomplete
31
32
33
        ],
        'rural': [
            ('/metadata/figures/3306', 'length-growing-season')
34
35
36
37
38
39
        ]
    }
}

#These are artifacts from our collection efforts; largely duplicates
webform_skip_list = []
40
41


42
43
def main():
    print_webform_list()
44

45
46
    # print webform_skip_list
    # sync()
47
48


49
50
51
def print_webform_list():
    for item in webform.get_list():
        webform_url = item['url']
52
53
54
55
56
57
58
        f = webform.get_webform(webform_url)

        if 'ready_for_publication' in f.original and f.original['ready_for_publication'] == 'yes':
            print webform_url, '***Ready For Publication***'
            print f
        else:
            webform_skip_list.append(webform_url)
59
60


61
62
63
64
def sync():
    for report_id in sync_metadata_tree:
        for chapter_id in sync_metadata_tree[report_id]:
            for figure_ids in sync_metadata_tree[report_id][chapter_id]:
65
                webform_url, gcis_id = figure_ids
66

67
68
69
                if webform_url in webform_skip_list:
                    print 'Skipping: ' + webform_url
                    continue
70

71
72
73
74
75
76
77
78
79
80
81
                sync_images(webform_url, gcis_id)
                sync_metadata(report_id, chapter_id, webform_url, gcis_id)


def sync_metadata(report_id, chapter_id, webform_url, gcis_id):
    #Merge data from both systems into one object...
    figure_obj = webform.get_webform(webform_url).merge(
        gcis.get_figure(report_id, gcis_id, chapter_id=chapter_id)
    )
    #...then send it.
    gcis.update_figure(report_id, chapter_id, figure_obj)
82
83


84
85
86
87
def sync_images(webform_url, gcis_id):
    figure = webform.get_webform(webform_url)
    #Now identifiers don't need to be matched
    figure.identifier = gcis_id
88

89
90
    webform.download_all_images(figure)
    upload_images_to_gcis(figure)
91
92


93
94
95
96
97
98
99
100
def upload_images_to_gcis(figure, report_id='nca3draft'):
    #Make sure we have all the images required for a COMPLETE update
    for image in figure.images:
        if not exists(image.local_path):
            raise Exception('Local file missing ' + image.local_path)
    for image in figure.images:
        for resp in gcis.create_image(image, report_id=report_id, figure_id=figure.identifier):
            print resp.status_code, resp.text
101
102


103
104
if __name__ == '__main__':
    main()