Skip to content

Instantly share code, notes, and snippets.

@jamescarr
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jamescarr/9531577 to your computer and use it in GitHub Desktop.
Save jamescarr/9531577 to your computer and use it in GitHub Desktop.
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