def post(self): UPDATE_RQST = 1 RETRIEVE_FROM_DATE = 2 RETRIEVE_FROM_DATES = 3 draw_types_lotto = {'55':0, '49':0, '45':0, '42':0} draw_types_digits = {'6d':0, '4d':0} draw_types_daily_3d = {'11am':0, '4pm':0, '9pm':0} draw_types_daily_2d = {'11am':0, '4pm':0, '9pm':0} date=CurrentDate() inp = self.request.body try: d=json.loads(inp) logging.debug(d) if not d: return except ValueError: self.response.headers['Content-Type'] = 'text/plain' self.response.set_status( 500,"Internal server error") self.response.write(' Invalid JSON object in request: '+inp) logging.error( 'Invalid JSON object: '+inp) return responseList=[] self.request_id = d['rid'] self.request_data = d['rdata'] self.request_extra = d['extra'] #self.month = e['v1'] #self.year = e['v3'] res = {} if self.request_id == UPDATE_RQST: for draw in draw_types_lotto: found = memcache.get(draw) if found: logging.debug(found) res[draw] = found for draw in draw_types_digits: found = memcache.get(draw) if found: res[draw] = found for time in draw_types_daily_2d: found = memcache.get('2d_'+time) if found: res['2d_'+time] = found for time in draw_types_daily_3d: found = memcache.get('3d_'+time) if found: res['3d_'+time] = found logging.debug(res) elif self.request_id == RETRIEVE_FROM_DATE: draw_date = DrawDate.get_by_key_name(self.request_data) if draw_date is None: cronJob().get() draw_date = DrawDate.get_by_key_name(self.request_data) self.response.set_status(200,"OK") resultElement={'id':1, 'rid':self.request_id, 'result':res} logging.debug(resultElement) responseList.append(resultElement) self.response.out.write(json.dumps(responseList))
def get(self): logging.debug('starting job..') draw_types_lotto = {'55':0, '49':0, '45':0, '42':0} draw_types_digits = {'6d':0, '4d':0} draw_types_daily_3d = {'11am':0, '4pm':0, '9pm':0} draw_types_daily_2d = {'11am':0, '4pm':0, '9pm':0} date_obj = CurrentDate() if date_obj.getwkday() == 0: #monday del draw_types_lotto['49'] del draw_types_lotto['42'] del draw_types_digits['6d'] elif date_obj.getwkday() == 1: #tuesday del draw_types_lotto['55'] del draw_types_lotto['45'] del draw_types_digits['4d'] elif date_obj.getwkday() == 2: del draw_types_lotto['49'] del draw_types_lotto['42'] del draw_types_digits['6d'] elif date_obj.getwkday() == 3: del draw_types_lotto['55'] del draw_types_lotto['45'] del draw_types_digits['4d'] elif date_obj.getwkday() == 4: #friday del draw_types_lotto['55'] del draw_types_lotto['49'] del draw_types_lotto['42'] del draw_types_digits['6d'] elif date_obj.getwkday() == 5: #saturday del draw_types_lotto['49'] del draw_types_lotto['45'] del draw_types_digits['4d'] elif date_obj.getwkday() == 6: #sunday del draw_types_lotto['55'] del draw_types_lotto['45'] del draw_types_lotto['42'] del draw_types_digits['6d'] del draw_types_digits['4d'] for draw in draw_types_lotto: found = memcache.get(draw) if found: draw_types_lotto[draw] = found for draw in draw_types_digits: found = memcache.get(draw) if found: draw_types_digits[draw] = found for time in draw_types_daily_2d: found = memcache.get('2d_'+time) if found: draw_types_daily_2d[time] = found for time in draw_types_daily_3d: found = memcache.get('3d_'+time) if found: draw_types_daily_3d[time] = found #keys_lotto = memcache.get('keys_lotto') #keys_digit = memcache.get('keys_digit') #keys_daily_2d = memcache.get('keys_daily_2d') #keys_daily_3d = memcache.get('keys_daily_3d') #draw_types_lotto_tmp = None #draw_types_digits_tmp = None #draw_types_daily_2d_tmp = None #draw_types_daily_3d_tmp = None #if keys_lotto is not None: # draw_types_lotto_tmp = memcache.get_multi(keys_lotto, key_prefix='lotto_draws') #if draw_types_lotto_tmp is not None: # draw_types_lotto.update(draw_types_lotto_tmp) # # if keys_digit is not None: # draw_types_digits_tmp = memcache.get_multi(keys_digit, key_prefix='digit_draws') # if draw_types_digits_tmp is not None: # draw_types_digits.update(draw_types_digits_tmp) # # if keys_daily_2d is not None: # draw_types_daily_2d_tmp = memcache.get_multi(keys_daily_2d, key_prefix='daily_2d') # if draw_types_daily_2d_tmp is not None: # draw_types_daily_2d.update(draw_types_daily_2d_tmp) # # if keys_daily_3d is not None: # draw_types_daily_3d_tmp = memcache.get_multi(keys_daily_3d, key_prefix='daily_3d') # if draw_types_daily_3d_tmp is not None: # draw_types_daily_3d.update(draw_types_daily_3d_tmp) draw_date = DrawDate.get_or_insert(date_obj.getyear()+ date_obj.getmonth()+ date_obj.getday(), day=date_obj.getday(), month=date_obj.getmonth(), year=date_obj.getyear()) for draw,done in draw_types_lotto.iteritems(): if not done: new = Scraper(date_obj.getyear(), date_obj.getmonth(), date_obj.getday(), draw).scrape() if new: lotto_results = LottoResults.get_or_insert(new[0], parent=draw_date, result=new[1], jackpot=new[2]) if len(new) == 4: lotto_results.winners = new[3] lotto_results.put() draw_types_lotto[draw] = new[1] memcache.add(key=draw, value=new[1], time=43200) #12hrs expire #memcache.add(key='keys_lotto', value=draw_types_lotto.keys(), time=86400) #memcache.add_multi(draw_types_lotto, key_prefix='lotto_draws', time=86400) for draw,done in draw_types_digits.iteritems(): if not done: new = Scraper(date_obj.getyear(), date_obj.getmonth(), date_obj.getday(), draw).scrape() if new: digits_results = DigitsResults_high.get_or_insert(new[0], parent=draw_date, result=new[1]) draw_types_digits[draw] = new[1] memcache.add(key=draw, value=new[1], time=43200) #12hrs expire #memcache.add(key='keys_digit', value=draw_types_digits.keys(), time=86400) #memcache.add_multi(draw_types_digits, key_prefix='digit_draws', time=86400) for time,done in draw_types_daily_2d.iteritems(): if not done: new = Scraper(date_obj.getyear(), date_obj.getmonth(), date_obj.getday(), '2d').scrape() if new: daily_results = DigitsResults_low.get_or_insert(new[0], parent=draw_date) if 'n.a' not in new[1]: if draw_types_daily_2d['11am'] != new[1]: daily_results.eleven_am_result = new[1] draw_types_daily_2d['11am'] = new[1] memcache.add(key='2d_11am', value=new[1]) if 'n.a' not in new[2]: if draw_types_daily_2d['4pm'] != new[2]: daily_results.four_pm_result = new[2] draw_types_daily_2d['4pm'] = new[2] memcache.add(key='2d_4pm', value=new[2]) if 'n.a' not in new[3]: if draw_types_daily_2d['9pm'] != new[3]: daily_results.nine_pm_result = new[3] draw_types_daily_2d['9pm'] = new[3] memcache.add(key='2d_9pm', value=new[3]) daily_results.put() #memcache.add(key='keys_daily_2d', value=draw_types_daily_2d.keys(), time=14400) #memcache.add_multi(draw_types_daily_2d, key_prefix='daily_2d', time=14400) for time,done in draw_types_daily_3d.iteritems(): if not done: new = Scraper(date_obj.getyear(), date_obj.getmonth(), date_obj.getday(), '3d').scrape() if new: daily_results = DigitsResults_low.get_or_insert(new[0], parent=draw_date) if 'n.a' not in new[1]: if draw_types_daily_3d['11am'] != new[1]: daily_results.eleven_am_result = new[1] draw_types_daily_3d['11am'] = new[1] memcache.add(key='3d_11am', value=new[1]) if 'n.a' not in new[2]: if draw_types_daily_3d['4pm'] != new[2]: daily_results.four_pm_result = new[2] draw_types_daily_3d['4pm'] = new[2] memcache.add(key='3d_4pm', value=new[2]) if 'n.a' not in new[3]: if draw_types_daily_3d['9pm'] != new[3]: daily_results.nine_pm_result = new[3] draw_types_daily_3d['9pm'] = new[3] memcache.add(key='3d_9pm', value=new[3]) daily_results.put()