Commit ba795e35 authored by abuddenberg's avatar abuddenberg
Browse files

Added support for image uploads

parent 362926e4
AK.jpg

48.1 KB

GPN.jpg

57 KB

GPS.jpg

55.7 KB

HI.jpg

52.1 KB

MW.jpg

50.9 KB

NE.jpg

52.3 KB

NW.jpg

51.6 KB

SE.jpg

50 KB

SW.jpg

52.5 KB

US.jpg

51.4 KB

#!/usr/bin/python
import urllib
import urllib2
import json
from domain import Figure
from pickle import dump
# base_url = 'http://data.gcis-test-front.joss.ucar.edu/report/nca3draft'
base_url = 'http://data-stage.globalchange.gov/report/nca3'
#user_agent = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'
headers = {
'Accept': 'application/json',
'Authorization': 'Basic YW5kcmV3LmJ1ZGRlbmJlcmdAbm9hYS5nb3Y6MjFjMjM2Y2YxYmZmMmRjMmMyOTNlZjljMWVjNTY1NWFhZWIyYjQ0ZDUyMzc3OGE1'
}
def main():
# drupal_id, create_dt = get_webform('1320')
# my_thing, post_url = get_figure(drupal_id)
#
# my_thing['create_dt'] = create_dt
# req = urllib2.Request(post_url, json.dumps(my_thing), headers)
# response = urllib2.urlopen(req)
# print response.read()
# print [key for key in get_figure_listing('our-changing-climate')['temperature-change'].keys()]
# for figure in get_figure_listing('our-changing-climate'):
# print figure.
out = open('../chapter2_listing.pk1', 'w')
dump(get_figure_listing('our-changing-climate'), out)
out.close()
def do_update():
print ''
update_url = '{base_url}/form/update'.format(base_url=base_url)
def get_figure(identifier):
query = '{url}{id}.json'.format(url=base_url, id=identifier)
resp = json.load(urllib2.urlopen(query))
del(resp['images'])
del(resp['chapter'])
del(resp['parents'])
return resp, query
#Full listing
def get_figure_listing(chapter=None):
#Enable filtering by chapter
chapter_filter = '/chapter/' + chapter if chapter else ''
url = '{base}{chap}/figure?{p}'.format(base=base_url, chap=chapter_filter, p=urllib.urlencode({'all': '1'}))
req = urllib2.Request(url, None, headers)
return [Figure(**figure) for figure in json.load(urllib2.urlopen(req))]
#Numerical identifiers
# def print_chapter_figure_list():
# for entry in figure_listing:
# chapter_num = ''
#
# if 'chapter' in entry and 'number' in entry['chapter'] and entry['ordinal']:
# chapter_num = entry['chapter']['number']
# ordinal = entry['ordinal']
#
# print '{0}.{1} '.format(chapter_num, ordinal), entry['identifier']
if __name__ == "__main__":
main()
......@@ -3,10 +3,9 @@
import httplib
import urllib
import json
import requests
from domain import Figure, Image
from pickle import dump
base_url = 'data.gcis-dev-front.joss.ucar.edu'
# base_url = 'http://data-stage.globalchange.gov/report/nca3'
headers = {
......@@ -72,7 +71,10 @@ def associate_image_with_figure(image_id, report, figure_id):
return resp.status, resp.reason, resp.read()
def upload_image_file(image_id)
def upload_image_file(image_id, filename, filepath):
url = 'http://{}/image/files/{}'.format(base_url, image_id)
# return requests.put(url, headers=headers, files={'file': (filename, open(filepath, 'rb'))})
return requests.put(url, data=open(filepath, 'rb'), headers=headers)
#Full listing
def get_figure_listing(report, chapter=None):
......
......@@ -6,7 +6,7 @@ import json
import re
from domain import Figure, Image
from gcis_client import update_image, get_figure, create_image, associate_image_with_figure
from gcis_client import update_image, get_figure, create_image, associate_image_with_figure, upload_image_file
prod = {'base': 'http://resources.assessment.globalchange.gov', 'token': 'mgTD63FAjG'}
dev_base = 'http://dev.nemac.org/asides10/metadata/figures/all?token=A2PNYxRuG9'
......@@ -71,6 +71,7 @@ figure = get_webform('/metadata/figures/3175')
for image in figure.images:
image.filename = file_map[image.identifier]
print upload_image_file('69da6d93-4426-4061-a2a1-7b3d01f2dc1c', 'AK.jpg', '../AK.jpg').text
# f = get_figure(report='nca3draft', chapter='our-changing-climate', figure='temperature-change')
#
......
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