Last active
August 29, 2015 13:57
-
-
Save jamescarr/9531577 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def retrieve_ubuntu_amis(region=u'us-east-1', dist=u'raring', arch=u'amd64', store=u'ebs'): | |
import requests | |
import json | |
from lxml import etree | |
from time import time | |
from StringIO import StringIO | |
now = int(time()) | |
url = 'http://cloud-images.ubuntu.com/locator/ec2/releasesTable?_={}'.format(now) | |
response = requests.get(url) | |
# piece of shit malformed JSON | |
components = response.content.split(',') | |
good_JSON = ','.join(components[:-1]) + components[-1] | |
the_images = json.loads(good_JSON) | |
for image in the_images['aaData']: | |
if image[0] == region and image[1] == dist \ | |
and image[3] == arch and image[4] == store: | |
#crap... ami is embedded in html. LET'S PULL IT OUT! | |
parser = etree.HTMLParser() | |
tree = etree.parse(StringIO(image[6]), parser) | |
return tree.xpath('//a/text()')[0] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment