def get(self): stage_key = self.request.get('stage') stage=StageImages.get_by_id(int(stage_key)) if stage: dict = json.loads(stage.data) self.redirect(str(dict[random.randint(0,len(dict))])) else: self.redirect("http://c1.staticflickr.com/9/8256/8701161794_02c1243f4f_n.jpg")
def images_parser(): urlfetch.set_default_fetch_deadline(45) page = urllib2.urlopen("http://www.letour.fr/le-tour/2015/es/galeria.html") soup = BeautifulSoup(page, "html.parser") divs = soup.findAll("div",{"class":"diapo v2 photo"}) for div in divs: stage=None images_json=[] titles=div.findAll("h3") for title in titles: spans=title.findAll("span") for span in spans: title_search = re.search('Etapa (.*)', span.string, re.IGNORECASE) if title_search: stage = title_search.group(1) imgs = div.findAll("img") for img in imgs: images_json.append(img["lazysrc"].replace("s.jpg","b.jpg")) if stage: stage_images = StageImages(id=int(stage),data=json.dumps(images_json)) stage_images.put()