Exemple #1
0
    def run(self):
        # Debug / Logging of Imports
        if not os.path.exists(settings.IMPORT_LOG_PATH):
            os.mkdir(settings.IMPORT_LOG_PATH)
            
        path = os.path.join(settings.IMPORT_LOG_PATH, datetime.datetime.now().strftime("%Y-%m-%dT%H-%M-%S"))
        if not os.path.exists(path):
            os.mkdir(path)
            
        file_dump = os.path.join(path, "import.xml")
        file_log = os.path.join(path, "import.log")
                
        data = self._provider()
        
        fp_dump = open(file_dump, "w")
        fp_log = open(file_log, "w")
        
        fp_dump.write(data)
        fp_dump.close()
        
        # Start Import
        xml = ElementTree.fromstring(data)
        
        for state in xml.findall("StateProv"):
            if state.get("stateprov_name") != "Texas":
                continue
                        
            division, _ = LotteryCountryDivision.objects.get_or_create(
                name=state.get("stateprov_name"), remote_id=state.get("stateprov_id"), \
                remote_country=state.get("country"))
            division.save()
                            
            for _game in state.findall("game"):
                _meta = self._filter_game(_game.get("game_name"))
                if not _meta:
                    continue
                
                fp_log.write("Found Game %s\n" % _meta)
                
                # game, _ = LotteryGame.objects.get_or_create(name=_meta["game_name"], code=_meta["game_format"])
                                
                # component, _ = LotteryGameComponent.objects.get_or_create(remote_id=_game.get("game_id"), name=_meta["comp_name"])
                game, _ = LotteryGame.objects.get_or_create(
                            name=_meta["game_name"],
                            code=_meta["game_format"]
                            ) 
                component, _ = LotteryGameComponent.objects.get_or_create(
                                remote_id=_game.get("game_id"),
                                name=_meta["comp_name"],
                                format=_meta["comp_format"]
                                )
                component.parent = game
                component.division.add(division)
                component.format = _meta["comp_format"]
                component.identifier = _meta["comp_identifier"]
                component.save()
                
                handler = GameManager.get(_meta["game_format"])
                fp_log.write("\tUsing Handler %s\n" % handler)
                
                # Load Jackpot
                jackpot = _game.find("jackpot")
                jackpot_date = None
                jackpot_prize = None
                                
                if jackpot is not None:
                    jackpot_date = datetime.datetime.strptime(jackpot.get("date"), "%m/%d/%Y").date()
                    jackpot_prize = int(jackpot.text)
                    fp_log.write("\t[jackpot] Found Jackpot %s\n" % jackpot_prize)
                    fp_log.write("\t[jackpot] Jackpot Date %s\n" % jackpot_date)
                else:
                    fp_log.write("\t[jackpot] No Jackpot\n")
                
                # Load Last Draw
                last_draw_date = _game.find("lastdraw_date")
                last_draw_numbers = _game.find("lastdraw_numbers")
                
                fp_log.write("\n\t[last] last_draw_date %s\n" % last_draw_date.text if last_draw_date is not None else None )
                fp_log.write("\t[last] last_draw_numbers %s\n" % last_draw_numbers.text if last_draw_numbers is not None else None)
                
                if last_draw_date is not None and last_draw_numbers is not None:
                    last_draw_date = datetime.datetime.strptime(last_draw_date.text, "%m/%d/%Y").date()
                    fp_log.write("\t[last] last_draw_date %s\n" % last_draw_date)
                    
                    if _meta["game_name"] in ["All or Nothing"] and not jackpot:
                        jackpot_prize = 250000
                        jackpot_date = last_draw_date
                    
                    _notification = False
                    
                    # Find Draw Object
                    try:
                        draw, created = \
                        LotteryDraw.objects.get_or_create(component=component,
                                                         date=last_draw_date,
                                                         division_id=1)
                    except Exception as e:
                        print e
                    if not draw.result:
                        _notification = True
                        
                    draw.result = handler.decode(last_draw_numbers.text, format=_meta["comp_format"])
                    if _meta["game_name"] == "Powerball":
                        if len(literal_eval(draw.result)) != 7:
                            raise ValueError("Unexpected Result Value: %s" % draw.result)
                        else:
                            draw.powerplay = literal_eval(draw.result)[-1]
                    
                    fp_log.write("\t[last] draw id %s, created: %s \n" % (draw.pk, created))
                    
                    # If the draw object was created OR the draw became official, 
                    # fire a notification for Draw Results
                    if created or not draw.official:
                        _notification = True
                    
                    draw.official = True
                    
                    if jackpot_prize is not None and jackpot_date == last_draw_date:
                        draw.jackpot = jackpot_prize
                    
                    draw.save()
                    print "Draw ID %s of state %s" % (draw.id, draw.division.remote_id)
                    fp_log.write("\t[last] draw id %s SAVED\n" % draw.pk)
                    fp_log.write("\t[last] %s\n" % draw.representation())
                    fp_log.write("\t[last] Firing Notification: %s\n" % _notification)
                    
                    if _meta["game_name"] == "Powerball" and len(ast.literal_eval(draw.result)) == 7:
                        # For last_draw_date

                        powerplay = ast.literal_eval(draw.result)[6:7]
                        fp_log.write("\t[last] Found Powerplay %s \n" % powerplay)
                        # first create a fake powerplay game component
                        fp_log.write("\t[last] Creating Fake Powerplay Component with value %s \n" % powerplay)
                        powerplay_component, _ = LotteryGameComponent.objects.get_or_create(
                                        remote_id=_game.get("game_id"),
                                        name="Powerplay"
                                        )
                        powerplay_component.parent = game
                        powerplay_component.division.add(division)
                        powerplay_component.format = "Powerplay"
                        powerplay_component.identifier = "Powerplay"
                        powerplay_component.save()
                        fp_log.write("\t[last] Fake Powerplay Object Saved %s \n" % powerplay_component.id)
                        # creating draw object for Powerplay
                        fp_log.write("\t[last] Creating Powerplay Draw \n")
                        powerplay_draw, powerplay_created = LotteryDraw.objects.get_or_create(
                                                component=powerplay_component,
                                                date=last_draw_date,
                                                division_id=1,
                                                result=powerplay
                                                )
                        # If the draw object was created OR the draw became official, 
                        # fire a notification for Draw Results
                        if powerplay_created or not powerplay_draw.official:
                            _notification = True

                        powerplay_draw.official = True
                        powerplay_draw.save()
                        fp_log.write("\t[last] Powerplay Object Saved %s \n" % powerplay_draw.id)
                        # Finally Storing the draw result without powerplay
                        draw.result = str(ast.literal_eval(draw.result)[0:6])
                        draw.save()

                    if _notification:
                        notify_draw_result_all.apply_async(countdown=0, 
                            kwargs={"draw_id": draw.pk})
                
                # Load Current Draw
                next_draw_date = _game.find("nextdraw_date")
                
                fp_log.write("\n\t[next] Processing NEXT Draw\n")
                fp_log.write("\t[next] next_draw_date %s\n" % next_draw_date.text if next_draw_date is not None else None)
                
                if next_draw_date is not None:
                    next_draw_date = datetime.datetime.strptime(next_draw_date.text, "%m/%d/%Y").date()
                    fp_log.write("\t[next] next_draw_date %s\n" % next_draw_date)
                    
                    _notification = False
                    
                    # Find Draw Object
                    draw, created = LotteryDraw.objects.get_or_create(component=component,
                                                                     date=next_draw_date,
                                                                     division_id=1)
                    
                    # If the draw object was created OR the draw became official, 
                    # fire a notification for Draw Results
                    if created or not draw.official:
                        _notification = True
                                   
                    draw.official = True
                    
                    fp_log.write("\t[next] draw id %s, created: %s \n" % (draw.pk, created))
                    
                    if jackpot_date == next_draw_date:
                        draw.jackpot = jackpot_prize
                        fp_log.write("\t[next] draw prize %s\n" % jackpot_prize)
                    
#                    fp_log.write("\t[last] Firing Notification: %s\n" % (draw.jackpot and _notification,))
#                    if draw.jackpot and _notification:
#                        notify_frenzy_all.apply_async(countdown=300, 
#                            kwargs={"draw_id": draw.pk})
                    
                    fp_log.write("\t[next] %s" % draw.representation())
                    draw.save()
                    
                fp_log.write("\n\n-----------------------------------------\n\n")
                
        # Close Logger
        fp_log.close()
    def run(self):
        
        # Debug / Logging of Imports
        log_path=os.path.join(settings.IMPORT_LOG_PATH,self.log_draw_folder)
        if not os.path.exists(log_path):
            os.mkdir(log_path)
            
        path = os.path.join(log_path,datetime.datetime.now().strftime("draw-%Y-%m-%dT%H-%M-%S"))
        if not os.path.exists(path):
            os.mkdir(path)
            
        file_dump = os.path.join(path, "common_draw_import.xml")
        file_log = os.path.join(path, "common_draw_import.log")
                
        data = self._provider()
        
        fp_dump = open(file_dump, "w")
        self.log_file = open(file_log, "w")
        
        fp_dump.write(data)
        fp_dump.close()
        
        # Start Import
        xml = ElementTree.fromstring(data)
          
        self.informer(self.name,"started :"+str(datetime.datetime.now().strftime("%d/%m/%Y %H:%M:%S"))+" \n",log=True)
        for k,v in self.COMMON_GAMES.iteritems():
            self.informer(self.name,"    Picked Game Parent :%s\n" %k,log=True)
            for component_name,_setting in v["components"].iteritems():
                self.informer(self.name,"        Picked Game Component :%s\n" %component_name,log=True)
                for remote_id,current_state in v['common_states']:
                    if component_name=="Megaplier" and remote_id not in ["TX"]:
                        #skip Megaplier for all states except (TX) 
                        continue
                    
                    found_state=xml.find(".//*[@stateprov_name='"+current_state+"']")
                    if found_state is not None:
                        self.informer(self.name,"            fill for state :%s\n" %current_state,log=True)
                        
                        #self.log_file.write("Found Game %s\n" % game_meta)
#                         print ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"
#                         print found_state.get("stateprov_name").strip()
#                         print found_state.get("stateprov_id").strip()
#                         print found_state.get("country").strip()
#                         print "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<"
                        division, _ = LotteryCountryDivision.objects.get_or_create(
                                                                                   name__iexact=found_state.get("stateprov_name").strip(),
                                                                                   remote_id__iexact=found_state.get("stateprov_id").strip(),
                                                                                   remote_country__iexact=found_state.get("country").strip()
                                                                                   )
                        division.save()
                        if _:self.informer(self.name,"            created division :%s\n" %division,color="cyan",log=True)
                        else:
                            self.informer(self.name,"            get division :%s\n" %division,log=True)
                                
                        found_component_game=found_state.find(".//*[@game_name='"+component_name+"']")
                        if found_component_game is not None:
                            self.informer(self.name,"            Is valid component for state ? :%s\n" %True,log=True)
                            component_meta = self._filter_game(comp_name=found_component_game.get("game_name"),parent_code=k)#get game component name
                            if component_meta:
                                self.informer(self.name,"            Has component meta ? :%s\n" %True,log=True)                          
                                
                                parent_game,_= LotteryGame.objects.get_or_create(
                                            name=component_meta["game_name"],
                                            code=component_meta["game_format"]
                                            )
                                
                                if _:self.informer(self.name,"            created new parent :%s\n" %parent_game,color="cyan",log=True)
                                else:
                                    self.informer(self.name,"            got component parent :%s\n" %parent_game,log=True)
                                
                                
#                                 if remote_id=="CA":
#                                     print "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
#                                     print "REMOTE ID:",found_component_game.get("game_id")
#                                     print "REMOTE ID:",component_meta.get("comp_name")
#                                     print "REMOTE ID:",component_meta.get("comp_format")
#                                     print "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
                                    
                                component,_ = LotteryGameComponent.objects.get_or_create(
                                                remote_id=found_component_game.get("game_id"),
                                                name=component_meta["comp_name"],
                                                format=component_meta["comp_format"]
                                                )
                                
                                if _:self.informer(self.name,"            created new component :%s\n" %component,color="cyan",log=True)
                                else:
                                    self.informer(self.name,"            got component :%s\n" %component,log=True)
                                
                                component.parent = parent_game
                                component.division.add(division)
                                component.format = component_meta["comp_format"]
                                component.identifier = component_meta["comp_identifier"]
                                component.save()
                                
                                handler = GameManager.get(component_meta["game_format"])#common handler for all types of component for single game 
                                self.informer(self.name,"            handler picked up :%s\n" %handler,log=True)
                                
                                # Load Jackpot
                                jackpot = found_component_game.find("jackpot")
                                jackpot_date = None
                                jackpot_prize = None
                                                
                                if jackpot is not None:
                                    jackpot_date = datetime.datetime.strptime(jackpot.get("date").strip(), "%m/%d/%Y").date()
                                    jackpot_prize = int(jackpot.text)
                                    self.informer(self.name,"            Found Jackpot tag with prize (%s) and attrib date (%s) \n" %(jackpot_prize,jackpot_date),log=True)
                                else:
                                    self.informer(self.name,"            Jackpot tag not found.(assume normal) \n",color="yellow",log=True)
                                
                                # Load Last Draw
                                last_draw_date = found_component_game.find("lastdraw_date")
                                last_draw_numbers = found_component_game.find("lastdraw_numbers")
                                self.informer(self.name,"            result available (%s) for last draw date (%s). \n" %(last_draw_numbers.text,last_draw_date.text),log=True)
                                
                                
                                if last_draw_date is not None and last_draw_numbers is not None:
                                    last_draw_date = datetime.datetime.strptime(last_draw_date.text, "%m/%d/%Y").date()
                                    
                                    if component_meta["game_name"] in ["All or Nothing"] and not jackpot:
                                        jackpot_prize = 250000
                                        jackpot_date = last_draw_date
                                        self.informer(self.name,"          handler picked up :<cl  Assume fix jackpot prize of (%s). \n" %(250000),color="yellow",log=True)
                                    
                                    _notification = False
                                    
                                    # Find Draw Object
                                    
                                    try:
                                        draw, created = \
                                        LotteryDraw.objects.get_or_create(component=component,
                                                                         date=last_draw_date,
                                                                         division_id=division.id)
                                    except Exception as e:
                                        self.informer(self.name,"            exception in retrieving draw for last draw date (%s) ,exception :%s \n"%(last_draw_date,e),color="red",log=True)
                                    
                                    if created:self.informer(self.name,"            created LotteryDraw id=(%s) for last draw date (%s)\n"%(draw.id,last_draw_date),color="yellow",log=True)
                                    else:
                                        self.informer(self.name,"            draw already exist LotteryDraw id=(%s) of last draw date (%s)\n"%(draw.id,last_draw_date),log=True)
                                    
                                    
                                    if not draw.result:
                                        _notification = True
                                        self.informer(self.name,"            prepared for draw result available notification for date (%s) \n"%(last_draw_date),log=True)
                                    draw.result = handler.decode(last_draw_numbers.text,format=component_meta["comp_format"])
                                    
                                    #daily derby racetime has came through jackpot xml not from winning xml
                                    # filling race time for component has name=="Daily Derby" and (if future daily derby has more than one component)identifier=="Daily Derby"  
                                    if component_name=="Daily Derby" and component_meta.get("comp_identifier")=="Daily Derby":
                                        try:
                                            draw.race_time = handler.decode_race_time(last_draw_numbers.text, format=component_meta["comp_format"])
                                            draw.save()
                                        except Exception as e:
                                            self.informer(self.name,"            ( Daily Derby ) race time not filled for draw id = (%s) \n"%(draw.id),color="red",log=True)
                                    if component_meta["game_name"] == "Powerball":
                                        if len(literal_eval(draw.result)) != 7:
                                            raise ValueError("Unexpected Result Value for powerball: %s" % draw.result)
                                        else:
                                            draw.powerplay = literal_eval(draw.result)[-1]
                                            
                                    #If the draw object was created OR the draw became official, 
                                    #fire a notification for Draw Results
                                    if created or not draw.official:
                                        _notification = True
                                     
                                    draw.official = True
                                     
                                    #if jackpot is old update old draw jackpot
                                    if jackpot_prize is not None and jackpot_date == last_draw_date:
                                        draw.jackpot = jackpot_prize
                                        self.informer(self.name,"            found old jackpot of last draw date (%s) \n"%(last_draw_date),color="yellow",log=True)
                                     
                                    draw.save()#valid data hasn't store yet. 
                                    
                                    #search for powerplay and create it
                                    if component_meta["game_name"] == "Powerball" and len(ast.literal_eval(draw.result)) == 7 and remote_id=="TX":
                                        #For last_draw_date
                 
                                        powerplay = ast.literal_eval(draw.result)[6:7]
                                        self.informer(self.name,"            found powerplay :%s\n"%(powerplay),log=True)
                                        
                                        # first create a fake powerplay game component
                                        
                                        powerplay_component, _ = LotteryGameComponent.objects.get_or_create(
                                                        remote_id=found_component_game.get("game_id").strip(),#powerplay has same remote_id as powerball
                                                        name="Powerplay"
                                                        )
                                        powerplay_component.parent = parent_game
                                        powerplay_component.division.add(division)
                                        powerplay_component.format = "Powerplay"
                                        powerplay_component.identifier = "Powerplay"
                                        powerplay_component.save()
                                        
                                        #creating draw object for Powerplay
                                        powerplay_draw, powerplay_created = LotteryDraw.objects.get_or_create(
                                                                component=powerplay_component,
                                                                date=last_draw_date,
                                                                division_id=division.id,
                                                                result=powerplay
                                                                )
                                        if powerplay_created:
                                            self.informer(self.name,"            powerplay draw (id=%s) created for existing last draw date :%s\n"%(powerplay_draw.id,last_draw_date),log=True)
                                        
                                            
                                        #If the draw object was created OR the draw became official, 
                                        #fire a notification for Draw Results
                                        if powerplay_created or not powerplay_draw.official:
                                            _notification = True
                                            self.informer(self.name,"            (powerplay)prepared for draw result available notification for date (%s) \n"%last_draw_date,log=True)
                 
                                        powerplay_draw.official = True
                                        powerplay_draw.save()
                                        self.informer(self.name,"            powerplay draw (id=%s) updated for existing last draw date :%s\n"%(powerplay_draw.id,last_draw_date),log=True)
                                        
                                        
                                        
                                        #Finally Storing the draw result without powerplay
                                        draw.result = str(ast.literal_eval(draw.result)[0:6])
                                        draw.save()
                                        self.informer(self.name,"            powerball draw result saved (draw id=%s) \n"%draw.id,log=True)
                                    
                                    #set other states result for powerball to 6 length
                                    if component_meta["game_name"] == "Powerball" and len(ast.literal_eval(draw.result)) == 7:
                                        draw.result = str(ast.literal_eval(draw.result)[0:6])
                                        draw.save()
                                    if _notification:
                                        try:
                                            self.informer(self.name,"            started sending draw result notification for old draw (id=%s) \n"%draw.id,log=True)
                                            notify_draw_result_all.apply_async(countdown=0,
                                                                           kwargs={"draw_id": draw.pk})
                                            
                                        except Exception as e:
                                            self.informer(self.name,"            Exception in sending draw result notification for old draw (id=%s)  :%s\n"%(draw.id,e),color="red",log=True)
                                 
                                
                                
                                # Load Current Draw
                                next_draw_date = found_component_game.find("nextdraw_date")
                                if next_draw_date is not None:
                                    self.informer(self.name,"            Next draw available for date :%s \n"%next_draw_date.text,log=True) 
                                else:
                                    self.informer(self.name,"            Next draw tag missing \n",color="yellow",log=True)
        
                                
                                 
                                if next_draw_date is not None:
                                    next_draw_date = datetime.datetime.strptime(next_draw_date.text, "%m/%d/%Y").date()
                                    _notification = False
                                     
                                    # Find Draw Object
                                    draw, created = LotteryDraw.objects.get_or_create(component=component,
                                                                                     date=next_draw_date,
                                                                                     division_id=division.id)
                                     
                                    
                                    if created:self.informer(self.name,"            created LotteryDraw id=(%s) of next draw date (%s) \n"%(draw.id,next_draw_date),log=True)
                                    else:
                                        self.informer(self.name,"            draw already exist LotteryDraw id=(%s) for next draw date (%s) \n"%(draw.id,next_draw_date),log=True)
                                        
                                    #If the draw object was created OR the draw became official, 
                                    #fire a notification for Draw Results
                                    if created or not draw.official:
                                        _notification = True
                                                    
                                    draw.official = True
                                     
                                    self.informer(self.name,"            prepared for frenzy notification for next draw date (%s) \n"%next_draw_date,log=True)
                                    
                                                                         
                                    if jackpot_date == next_draw_date:
                                        draw.jackpot = jackpot_prize
                                        self.informer(self.name,"            next draw jackpot available :%s\n"%jackpot_prize,log=True)
                                    else:
                                        self.informer(self.name,"            next draw jackpot not filled \n",color="yellow",log=True)
                                        
                                    
                                    if draw.jackpot and _notification:
                                        try:
                                            self.informer(self.name,"            started sending frenzy notification for new draw (id=%s) \n"%draw.id,log=True)
#                                             notify_frenzy_all.apply_async(countdown=300,
#                                                                           kwargs={"draw_id": draw.pk})
                                        except Exception as e:
                                            self.informer(self.name,"            Exception in sending frenzy notification for new draw (id=%s) :%s\n"%(draw.id,e),color="red",log=True)
                                    
                                    
                                    draw.save()
                                    self.informer(self.name,"            next draw saved \n",log=True)
                                    
                                self.informer(self.name,"\n                --------------------------------------------\n",log=True)
                            else:
                                self.informer(self.name,"           (auto skip) Has component meta ? :%s\n" %False,log=True,color="red")
                        else:
                            self.informer(self.name,"           (auto skip) Is valid component for state ? :%s\n" %False,log=True,color="red")
                    else:
                        self.informer(self.name,"            (auto skip) state not found :%s\n" %current_state,log=True,color="red")
                        self.informer(self.name,"\n            --------------------------------------------\n",log=True)
                        #print colored(e,'red','on_white',attrs=['bold'])
            self.informer(self.name,"\n        --------------------------------------------\n",log=True)
        self.informer(self.name,"\n------------------------DONE--------------------\n",log=True)
                    
            
        # Close Logger
        self.log_file.close()