def post(self): """Simple get request handler.""" time_now = datetime.datetime.now() query_time = time_now - datetime.timedelta(hours=3) # listcam = {"name" : "image_url", "name" : "image_url",...} cam = memcache.get("listcam") if cam is not None: logging.info("From MEMCACHE") else: camresults = Webcam.all() cam = {} for a in camresults: cam[a.name] = a.image_url logging.info("From DATASTORE") memcache.set("listcam", cam) #return cam for j in cam.keys(): try: d_images = WebcamImage.all().filter("webcam =", j).filter("timestamp <", query_time).order("timestamp") # We need to keep at least 10 images d_count = d_images.count() if d_count > 5: for l in d_images: del_blob = blobstore.BlobInfo.get(l.blob.key()) if del_blob: del_blob.delete() logging.info("Delete %s" % (l.webcam)) l.delete() d_count = d_count - 1 if d_count == 10: break except Exception, f: logging.error("Error fetching data: %s" % f)
def get(self, start, finish): """Simple get request handler.""" self.response.headers["Content-Type"] = "text/html" logging.info("Cron job started") # listcam = {"name" : "image_url", "name" : "image_url",...} cam = memcache.get("listcam") if cam is not None: logging.info("From MEMCACHE") else: logging.info("From DATASTORE") camresults = Webcam.all() cam = {} for a in camresults: cam[a.name] = a.image_url memcache.set("listcam", cam) for listcam in cam.keys(): try: check_size = urllib.urlopen(cam[listcam]).read() image_size = len(check_size) q_images = WebcamImage.all().filter("webcam =", listcam).order("-timestamp") pic_index = 0 q_results = q_images.fetch(1) # Test just in case this is 1st time an image is checked try: q_blob_info = blobstore.BlobInfo.get(q_results[pic_index].blob.key()) old_size = q_blob_info.size except: old_size = 0 logging.info("Old size: %s - New size: %s" % (old_size, image_size)) if old_size != image_size: taskqueue.add(queue_name='fetching', url='/fetchQ', params={'cam': listcam,'url': cam[listcam]}) except Exception, e: logging.error("Error fetching data: %s" % e) self.redirect("/fetchPics")
def post(self): cam = self.request.get('cam') image_url = self.request.get('url') logging.info("Cam: %s URL %s" % (cam, image_url)) image_blob = files.blobstore.create(mime_type='image/jpeg') fetch_response = urlfetch.fetch(image_url) image_data = fetch_response.content with files.open(image_blob, "a") as f: f.write(image_data) logging.info("Write Image") files.finalize(image_blob) im = WebcamImage() im.webcam = cam im.blob = files.blobstore.get_blob_key(image_blob) im.put() # blob_info = blobstore.BlobInfo.get(im.blob.key()) logging.info("Fetching for webcam %s" % (cam))
def get(self): cam = Webcam.all().order("-name") counti = 0 pic_blobs = [] for j in cam: try: im = WebcamImage.all().filter("webcam =", j.name.lower()).order("webcam") #results = im.fetch(4) for i in im: try: pic_blobs = pic_blobs + [{"blob": i.blob.key(),"webcam": i.webcam,"size": i.blob.size,"timestamp": i.timestamp, "count" : counti}] counti = counti + 1 except Exception, e: logging.error("Error fetching data: %s %s" % e) counti = 0 except Exception, f: logging.error("Error fetching data: %s" % f)
def post(self, name, pic_index): if name == "" or pic_index == "": logging.warn("Encountered call for single picture with no params") return None logging.info("Serving a single picture:") q_images = WebcamImage.all().filter("webcam =", name.lower()).order("-timestamp") pic_index = int(pic_index) q_results = q_images.fetch(limit=1, offset=pic_index) # len(q_results) should never be greater than pic_index, but just in case if len(q_results) >= pic_index + 1: self.send_blob(blobstore.BlobInfo.get(q_results[pic_index].blob.key())) else: path = os.path.join(os.path.dirname(__file__), "placeholder.jpg") self.response.headers['Content-Type'] = 'image/jpg' self.response.out.write(file(path, "rb").read()) logging.warn("Query for picture returned no results, sending placeholder\n") return None
def get(self): logging.info("Serving all pictures...") im = WebcamImage.all().order("-timestamp") # limiting pictures to 15 - otherwise we may run out of CPU quota results = im.fetch(15) # rewrite the values - blob is a BlobReferenceProperty, but we need to pass # the key pic_blobs = [{"blob": i.blob.key(), "webcam": i.webcam, "size": i.blob.size, "timestamp": i.timestamp} for i in results] #"size": i.blob.size #size could be used in fetch to place in blob only new image, compare size to previous, ignore if same? template_values = { "images": pic_blobs } path = os.path.join(os.path.dirname(__file__), "images.html") self.response.out.write(template.render(path, template_values))