Skip to content

Instantly share code, notes, and snippets.

@annegentle
Created March 10, 2014 16:04
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 annegentle/9467910 to your computer and use it in GitHub Desktop.
Save annegentle/9467910 to your computer and use it in GitHub Desktop.
import time
import pyrax
import pyrax.exceptions as exc
pyrax.set_setting("identity_type", "rackspace")
pyrax.set_credential_file("rscreds.txt")
cf = pyrax.cloudfiles
# Point to where the photos are stored on your hard drive
# Make sure you have bootstrap styles, js scripts, background images here too
photodir = "/Users/annegentle/tools/balloonmap/ballooncss3/balloonmappingpics"
# Create a remote public container with CDN support
remote = cf.create_container("wphotos")
# Make container public, TTL is time to live, 900 seconds
cf.make_container_public(remote, ttl = 900)
# Upload files from folder
ignoreext = ["bootstrap*", ".DS_Store"]
print "Beginning folder upload of photos only"
#cf.upload_folder(photodir, remote, ignore=ignoreext)
upload_key, total_bytes = cf.upload_folder(photodir, remote, ignore=ignoreext)
print "Total bytes to upload:", total_bytes
uploaded = 0
while uploaded < total_bytes:
uploaded = cf.get_uploaded(upload_key)
print "Progress: %4.2f%%" % ((uploaded * 100.0) / total_bytes)
time.sleep(1)
print "Initial photo upload complete"
print "Container name:", str(remote.name)
gallerypagestart = """
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Balloon Mapping Photos</title>
<meta name="description" content="">
<meta name="author" content="">
<!-- HTML5 shim, for IE6-8 support of HTML elements -->
<!--[if lt IE 9]>
<script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<script src="js/jquery.min.js"></script>
<script src="js/bootstrap-dropdown.js"></script>
<script type='text/javascript'>
$(document).ready(function(){
$('#topbar').dropdown();
});
</script>
<link href="css/bootstrap.min.css" rel="stylesheet">
<style type="text/css">
/* Override some defaults */
html, body {
background: url('images/bootstrap.jpg') repeat top center;
}
body {
padding-top: 40px; /* 40px to make the container go all the way to the bottom of the topbar */
}
.container > footer p {
text-align: center; /* center align it with the container */
}
.container {
width: 820px; /* downsize our container to make the content feel a bit tighter and more cohesive. NOTE: this removes two full columns from the grid, meaning you only go to 14 columns and not 16. */
}
/* The white background content wrapper */
.content {
background-color: #fff;
padding: 20px;
margin: 0 -20px; /* negative indent the amount of the padding to maintain the grid system */
-webkit-border-radius: 0 0 6px 6px;
-moz-border-radius: 0 0 6px 6px;
border-radius: 0 0 6px 6px;
-webkit-box-shadow: 0 1px 2px rgba(0,0,0,.15);
-moz-box-shadow: 0 1px 2px rgba(0,0,0,.15);
box-shadow: 0 1px 2px rgba(0,0,0,.15);
}
/* Page header tweaks */
.page-header {
background-color: #f5f5f5;
padding: 20px 20px 10px;
margin: -20px -20px 20px;
}
/* Give a quick and non-cross-browser friendly divider */
.content .span4 {
margin-left: 0;
padding-left: 19px;
border-left: 1px solid #eee;
}
.topbar .btn {
border: 0;
}
#thumbnail img{
max-width:220px;
max-height:154px;
}
.bs-grid {
margin:0 -19px 20px -16px;
overflow:hidden
}
.bs-grid-list
{
padding-left:0;
list-style:none
}
.bs-grid li
{ float:left;
width:25%;
height:165px;
padding:10px;
font-size:10px;
line-height:1.4;
text-align:center;
border:1px solid #fff;
background-color:#f9f9f9
}
</style>
</head>
<body>
<div class="container">
<div class="content">
<div class="page-header">
<h1>Photos from the sky</h1>
</div>
<div class="row">
<div class="span10">
<h2>Balloon mapping at the University of Texas in Austin.</h2>
<div class='cleaner_h20'></div>
<p>On a sunny February day for the <a href="https://www.engr.utexas.edu/wep/k12/girlday">Introduce a Girl to Engineering Day</a> hosted by the Women in Engineering Program, we launched a five-foot in diameter helium balloon with a camera attached. Here are the photos, hosted on Rackspace Cloud Files with a static web page built with Python. Enjoy!</p>
<div class="bs-grid">
<ul class="bs-grid-list">
"""
gallerypageend = """
</ul>
</div>
</div> <!-- /row -->
</div> <!-- /content -->
<footer>
<p><a href="http://creativecommons.org/licenses/by-sa/4.0/">CC-by-SA</a></p>
</footer>
</div> <!-- /container -->
</body>
</html>
"""
# Make an object that can be uploaded as an index.html file
# Iterate through all photos to get links
imgcodestart = '<li>'
imgcodeend ='" alt="photo" class="img-thumbnail"/> </li>' + '\n'
# Create an empty list to hold the photo img src html code lines
imgsrclist = []
photo_names = remote.get_objects()
# Now go through the list of object names
for object_name in photo_names:
imgsrc = thumbdiv + imgcodestart + str(object_name.name) + imgcodeend
imgsrclist.append(imgsrc)
html_photos = ''.join(imgsrclist)
imagelistingpage = gallerypagestart + html_photos + gallerypageend
# Uploads the index.html made from the listing generated by going through names
cf.store_object(remote, 'index.html', imagelistingpage, content_type = 'text/html', ttl = 31536000)
# Upload Bootstrap assets
print "Uploading Bootstrap"
upload_key, total_bytes = cf.upload_folder(photodir, remote, ignore=".DS_Store")
print "Final upload complete"
# Set an index.html for a static page for that new public container
cf.set_container_web_index_page(remote, "index.html")
print "Page for display is at: ", remote.cdn_uri + "/index.html"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment