Esempio n. 1
0
class Fetcher():
    db = db_init()

    def get_zip_files_by_url(self, z_url):
        #z_url = "http://www.bclc.com/DownloadableNumbers/CSV/KenoCurrentYear.zip"
        #z_url = "http://www.bclc.com/DownloadableNumbers/CSV/KenoPastYears.zip"

        headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64)'}

        requests.packages.urllib3.disable_warnings(InsecureRequestWarning)

        content = requests.get(z_url, verify=False, headers=headers)
        zipfiles = zipfile.ZipFile(io.BytesIO(content.content))
        return zipfiles

    def read_zipped_csv(self, z_url=None, filename=None):
        zipfiles = self.get_zip_files_by_url(z_url)
        df = pd.read_csv(zipfiles.open(filename))
        results = pd.DataFrame()
        results['draw_id'] = df['DRAW NUMBER'].astype('int64')
        #results['data_time'] = pd.datetime(df['DRAW DATE'], format='%Y-%m-%d %H:%M:%S', errors='ignore');
        results['date_time'] = df['DRAW DATE'].astype('datetime64[ns]')
        i = 1
        while (i <= 20):
            results['r' + str(i)] = df['NUMBER DRAWN ' +
                                       str(i)].astype('int64')
            i += 1
        results['mega'] = df['BONUS MULTIPLIER'].astype('float')
        results.set_index('draw_id', inplace=True)
        results.index = results.index.astype('int64')
        #print(results[results.index >2312060].head())
        return results

    def upload_dataframe(self, data_frame, table_name='results'):
        # query = (' SELECT MAX(`draw_id`) FROM {}').format(table_name)
        # df = pd.read_sql(query, con=self.db.db_engine)
        # print(df)
        data_frame.to_sql(name=table_name,
                          con=self.db.db_engine,
                          if_exists='append',
                          index=True)

    def fetch_online_csv(self):
        cur_year_zip = "http://www.bclc.com/DownloadableNumbers/CSV/KenoCurrentYear.zip"
        prev_years_zip = "http://www.bclc.com/DownloadableNumbers/CSV/KenoPastYears.zip"

        cur_file_name = 'KenoCurrentYear.csv'
        prev_file_name = 'KenoYear2019.csv'

        results = self.read_zipped_csv(cur_year_zip, cur_file_name)
        #results = self.read_zipped_csv(prev_years_zip, prev_file_name);

        #print(results[results.index >2312063].head())

        self.upload_dataframe(results, 'results')

        return results[results.index > 2312063]

    def fetch_csv_results(self):
        data_folder = '../results/'
        csvFileList = []
        i = 2002
        while (i < 2019):
            csvFileList.append("YR" + str(i) + "AKENO.csv")
            csvFileList.append("YR" + str(i) + "BKENO.csv")
            i += 1

        for csvFileName in csvFileList:
            path = data_folder + csvFileName
            self.fetch_csv_file(path)

    def fetch_csv_file(self, csv_path):
        with io.open(csv_path, 'r', encoding='utf8') as csvFile:
            #for tweet in f.readlines():
            #    tweet = tweet.strip()
            #         with open(csv_path, newline='') as csvFile:
            #         #with open('../results/YR1995BKENO.csv','r') as csvFile:
            #             csvreader = csv.reader(csvFile, delimiter=' ', quotechar='|')
            #
            header = csvFile.readline()
            for rows in csvFile.readlines():
                row = rows.strip()
                columns = row.split(',')
                r = iresult.iResult()
                r.draw_id = int(columns[1])
                r.date_time = Utils.parseDateTime(columns[2],
                                                  '=\"%d/%m/%Y %H:%M:%S\"')
                r.numFromArray([
                    int(columns[4]),
                    int(columns[5]),
                    int(columns[6]),
                    int(columns[7]),
                    int(columns[8]),
                    int(columns[9]),
                    int(columns[10]),
                    int(columns[11]),
                    int(columns[12]),
                    int(columns[13]),
                    int(columns[14]),
                    int(columns[15]),
                    int(columns[16]),
                    int(columns[17]),
                    int(columns[18]),
                    int(columns[19]),
                    int(columns[20]),
                    int(columns[21]),
                    int(columns[22]),
                    int(columns[23])
                ])
                r.mega = columns[3]
                if (columns[3] == ''): r.mega = 0
                else: r.mega = float(columns[3])
                r.db_save()

    def fetch_result(self, DrawID):
        url = "http://www.californialottery.com/sitecore/content/LotteryHome/play/draw-games/hot-spot/draw-detail?draw="
        url_str = url + str(DrawID)

        urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
        http = urllib3.PoolManager()
        response = http.request('GET', url_str)
        doc = BeautifulSoup(response.data, 'html.parser')
        drw = doc.find("h3", {"class": "hotspot-callout-text"})
        draw_id = drw.get_text().strip()

        uls = doc.find("ul", {"id": "hotSpotDrawResults"})
        lis = uls.find_all("li")
        dttm = doc.find("div", {
            "class": "hotspot-details-datetime"
        }).find_all("p")
        pick_array = []
        pick_mega = 0
        for li in lis:
            li_cls = li.get('class')
            if (len(li_cls) > 1):
                pick_array.append(li.get_text())
                if (len(li_cls) > 2):
                    pick_mega = li.get_text()
                    print("bonus", li.get_text())

        date = dttm[0].get_text().split(":")[1]
        wk_day = date.split(",")[0].strip()
        month = date.split(",")[1].strip().split(" ")[0].strip()
        day = date.split(",")[1].strip().split(" ")[1].strip()
        year = date.split(",")[2].strip().strip()
        time_hr = dttm[1].get_text().split(":")[1].strip()
        time_min = dttm[1].get_text().split(":")[2].split(" ")[0].strip()
        am_pm = dttm[1].get_text().split(":")[2].split(" ")[1].strip()

        str_dttm = day + '-' + month + '-' + year + ' ' + time_hr + ':' + time_min + ' ' + am_pm
        draw_date_time = Utils.parseDateTime(str_dttm)
        pick = ipick.iPick(pick_array)
        print("date", str_dttm, pick.toString())
        print(drw.get_text().strip())
        r = iresult.iResult(draw_id, draw_date_time, pick, pick_mega)
        return r
        #dttm = dttm_p.select("p")get;


#         lis = uls.select("li");
#         swin = "spot win";
#         swbonus = "spot win bonus";

#         print("parsed draw ID: " + drw.text());
#             draw_array[0]=drw.text(); System.out.println(drw.text());
#             draw_array[1]=dttm.select("p").text();
#
#             //Date draw_date = utils.getDateTimeFromHTML(dttm.select("p").text());
#
#             //System.out.println(dttm.select("p").text());
#             String mega = null;
#             int i = 2;
#             for (Element li:lis){
#                 if (li.className().equals(swin)){
#                     //System.out.println(li.text());
#                     draw_array[i]=li.text();
#                     i++;
#                 } else if (li.className().equals(swbonus)){
#                     //System.out.println(li.text());
#                     draw_array[i]=li.text();
#                     mega = li.text();
#                     i++;
#                 }
#
#             }
#             draw_array[i] = mega;
#
#         } catch (IOException e) {
#             // TODO Auto-generated catch block
#             e.printStackTrace();
#         }

#         return draw_array;

    def fetch_results(self, start_id, end_id):
        draw_id = start_id
        while (draw_id < end_id):
            res = self.fetch_result(draw_id)
            res.db_save()
            draw_id += 1

    def sync_results(self):
        res = iresult.iResult()
        last_draw_id = res.getLastDrawID()
        next_draw_id = last_draw_id + 1
        print(last_draw_id)
        while (True):
            new_res = self.fetch_result(next_draw_id)
            new_res.db_save()
            next_draw_id += 1
Esempio n. 2
0
class iDraw(Base, DBBase):
    __tablename__ = 'draws'
    __table_args__ = {'extend_existing': True}
    db = db_init()

    draw_id = Column(Integer, primary_key=True, autoincrement=False)
    n1 = Column(Integer)
    n2 = Column(Integer)
    n3 = Column(Integer)
    n4 = Column(Integer)
    n5 = Column(Integer)
    n6 = Column(Integer)
    n7 = Column(Integer)
    n8 = Column(Integer)
    n9 = Column(Integer)
    n10 = Column(Integer)
    n11 = Column(Integer)
    n12 = Column(Integer)
    n13 = Column(Integer)
    n14 = Column(Integer)
    n15 = Column(Integer)
    n16 = Column(Integer)
    n17 = Column(Integer)
    n18 = Column(Integer)
    n19 = Column(Integer)
    n20 = Column(Integer)
    n21 = Column(Integer)
    n22 = Column(Integer)
    n23 = Column(Integer)
    n24 = Column(Integer)
    n25 = Column(Integer)
    n26 = Column(Integer)
    n27 = Column(Integer)
    n28 = Column(Integer)
    n29 = Column(Integer)
    n30 = Column(Integer)
    n31 = Column(Integer)
    n32 = Column(Integer)
    n33 = Column(Integer)
    n34 = Column(Integer)
    n35 = Column(Integer)
    n36 = Column(Integer)
    n37 = Column(Integer)
    n38 = Column(Integer)
    n39 = Column(Integer)
    n40 = Column(Integer)
    n41 = Column(Integer)
    n42 = Column(Integer)
    n43 = Column(Integer)
    n44 = Column(Integer)
    n45 = Column(Integer)
    n46 = Column(Integer)
    n47 = Column(Integer)
    n48 = Column(Integer)
    n49 = Column(Integer)
    n50 = Column(Integer)
    n51 = Column(Integer)
    n52 = Column(Integer)
    n53 = Column(Integer)
    n54 = Column(Integer)
    n55 = Column(Integer)
    n56 = Column(Integer)
    n57 = Column(Integer)
    n58 = Column(Integer)
    n59 = Column(Integer)
    n60 = Column(Integer)
    n61 = Column(Integer)
    n62 = Column(Integer)
    n63 = Column(Integer)
    n64 = Column(Integer)
    n65 = Column(Integer)
    n66 = Column(Integer)
    n67 = Column(Integer)
    n68 = Column(Integer)
    n69 = Column(Integer)
    n70 = Column(Integer)
    n71 = Column(Integer)
    n72 = Column(Integer)
    n73 = Column(Integer)
    n74 = Column(Integer)
    n75 = Column(Integer)
    n76 = Column(Integer)
    n77 = Column(Integer)
    n78 = Column(Integer)
    n79 = Column(Integer)
    n80 = Column(Integer)
    mega = Column(Integer)
    d_bin0140 = Column(BigInteger)
    d_bin4180 = Column(BigInteger)

    drawDict = {0: 0}

    def __init__(self, DrawID=0):
        self.reset()
        self.draw_id = DrawID
        if (self.draw_id > 0): self.setup()

        super().setupDBBase(iDraw, iDraw.draw_id, self.draw_id)

    def reset(self):
        for i in range(81):
            self.drawDict[i] = 0
            if i > 0:
                setattr(self, "n" + str(i), 0)
        self.mega = 0
        self.d_bin0140 = 0
        self.d_bin4180 = 0

    def derive(self):
        if (self.draw_id > 0):
            self.reset()

            #remove bin setting as it is resource intensive. We will use SQL
            #b0140 = ibin.iBin();
            #b4180 = ibin.iBin();

            r = iresult.iResult(self.draw_id)
            r.setup()
            #setup from db
            self.mega = r.mega
            for x in r.pick.getArray():
                setattr(self, "n" + str(x), 1)
                self.drawDict[x] = 1

                #if x > 40:
            #         posn = x - 40;
            #         b4180.set_bit(posn);
            #     else: b0140.set_bit(x)

            # self.d_bin0140 = b0140.get_bin(); #770947678208
            # self.d_bin4180 = b4180.get_bin();  #8606711840

    def setBin(self):
        i = 1
        b0140 = ibin.iBin()
        b4180 = ibin.iBin()
        while (i <= 80):
            dr = getattr(self, "n" + str(i))
            if dr == 1:
                if i > 40:
                    b4180.set_bit(i)
                else:
                    b0140.set_bit(i)
            i += 1
        self.d_bin0140 = b0140.get_bin()
        #833831981626 for 2277311
        self.d_bin4180 = b4180.get_bin()
        #206435647488 for 2277311

        #update db

        d = self.db.session.query(iDraw).filter(
            iDraw.draw_id == self.draw_id).update(
                {
                    iDraw.d_bin0140: self.d_bin0140,
                    iDraw.d_bin4180: self.d_bin4180
                },
                synchronize_session='fetch')
        self.db.session.commit()

    def setBinBySQL(self):
        #single draw_id
        # sql_statement = text(""" UPDATE `draws` SET `d_bin0140`=CONV(CONCAT(`n1`, `n2`, `n3`, `n4`, `n5`, `n6`, `n7`, `n8`, `n9`, `n10`, `n11`, `n12`, `n13`, `n14`, `n15`, `n16`, `n17`, `n18`, `n19`, `n20`, `n21`, `n22`, `n23`, `n24`, `n25`, `n26`, `n27`, `n28`, `n29`, `n30`, `n31`, `n32`, `n33`, `n34`, `n35`, `n36`, `n37`, `n38`, `n39`, `n40`),2,10), `d_bin4180`=CONV(CONCAT(`n41`, `n42`, `n43`, `n44`, `n45`, `n46`, `n47`, `n48`, `n49`, `n50`, `n51`, `n52`, `n53`, `n54`, `n55`, `n56`, `n57`, `n58`, `n59`, `n60`, `n61`, `n62`, `n63`, `n64`, `n65`, `n66`, `n67`, `n68`, `n69`, `n70`, `n71`, `n72`, `n73`, `n74`, `n75`, `n76`, `n77`, `n78`, `n79`, `n80`),2,10) WHERE `draw_id` = :draw_id """);
        # self.db.session.execute(sql_statement,  { "draw_id": self.draw_id } )

        #multiple draw_ids with dbin value null or 0
        sql_statement = text(
            """ UPDATE `draws` SET `d_bin0140`=CONV(CONCAT(`n1`, `n2`, `n3`, `n4`, `n5`, `n6`, `n7`, `n8`, `n9`, `n10`, `n11`, `n12`, `n13`, `n14`, `n15`, `n16`, `n17`, `n18`, `n19`, `n20`, `n21`, `n22`, `n23`, `n24`, `n25`, `n26`, `n27`, `n28`, `n29`, `n30`, `n31`, `n32`, `n33`, `n34`, `n35`, `n36`, `n37`, `n38`, `n39`, `n40`),2,10), `d_bin4180`=CONV(CONCAT(`n41`, `n42`, `n43`, `n44`, `n45`, `n46`, `n47`, `n48`, `n49`, `n50`, `n51`, `n52`, `n53`, `n54`, `n55`, `n56`, `n57`, `n58`, `n59`, `n60`, `n61`, `n62`, `n63`, `n64`, `n65`, `n66`, `n67`, `n68`, `n69`, `n70`, `n71`, `n72`, `n73`, `n74`, `n75`, `n76`, `n77`, `n78`, `n79`, `n80`),2,10) WHERE `d_bin0140` IS NULL or `d_bin0140` = 0 or `d_bin4180` IS NULL or `d_bin4180` = 0 """
        )
        self.db.session.execute(sql_statement)

    def setup(self):
        d = self.db.session.query(iDraw).filter(
            iDraw.draw_id == self.draw_id).first()

        if d is not None:

            for i in self.drawDict.keys():
                if (i > 0):
                    setattr(self, "n" + str(i), getattr(d, "n" + str(i)))
                    self.drawDict[i] = getattr(self, "n" + str(i))
            self.mega = d.mega
            self.d_bin0140 = d.d_bin0140
            self.d_bin4180 = d.d_bin4180

    def toString(self):
        return str(self.get_dict()) + "[" + str(self.mega) + "]"

    def get_dict(self):
        dict = {}
        dict[self.draw_id] = np.array([
            self.n1, self.n2, self.n3, self.n4, self.n5, self.n6, self.n7,
            self.n8, self.n9, self.n10, self.n11, self.n12, self.n13, self.n14,
            self.n15, self.n16, self.n17, self.n18, self.n19, self.n20,
            self.n21, self.n22, self.n23, self.n24, self.n25, self.n26,
            self.n27, self.n28, self.n29, self.n30, self.n31, self.n32,
            self.n33, self.n34, self.n35, self.n36, self.n37, self.n38,
            self.n39, self.n40, self.n41, self.n42, self.n43, self.n44,
            self.n45, self.n46, self.n47, self.n48, self.n49, self.n50,
            self.n51, self.n52, self.n53, self.n54, self.n55, self.n56,
            self.n57, self.n58, self.n59, self.n60, self.n61, self.n62,
            self.n63, self.n64, self.n65, self.n66, self.n67, self.n68,
            self.n69, self.n70, self.n71, self.n72, self.n73, self.n74,
            self.n75, self.n76, self.n77, self.n78, self.n79, self.n80,
            self.d_bin0140, self.d_bin4180
        ])
        return dict

    def __get_dict__(self):
        dic = {}
        dic[self.draw_id] = {}

        i = 1
        while (i <= 80):
            dic[self.draw_id][i] = getattr(self, "n" + str(i))
            i += 1

        dic[self.draw_id]['d_bin0140'] = self.d_bin0140
        dic[self.draw_id]['d_bin4180'] = self.d_bin4180

        return dic
Esempio n. 3
0
class iResult(Base, DBBase):
    __tablename__ = 'results'
    __table_args__ = {'extend_existing': True}
    db = db_init()

    draw_id = Column(Integer, primary_key=True)
    date_time = Column(DateTime)
    r1 = Column(Integer)
    r2 = Column(Integer)
    r3 = Column(Integer)
    r4 = Column(Integer)
    r5 = Column(Integer)
    r6 = Column(Integer)
    r7 = Column(Integer)
    r8 = Column(Integer)
    r9 = Column(Integer)
    r10 = Column(Integer)
    r11 = Column(Integer)
    r12 = Column(Integer)
    r13 = Column(Integer)
    r14 = Column(Integer)
    r15 = Column(Integer)
    r16 = Column(Integer)
    r17 = Column(Integer)
    r18 = Column(Integer)
    r19 = Column(Integer)
    r20 = Column(Integer)
    mega = Column(Integer)

    pick = ''

    def __init__(self,
                 DrawID=0,
                 DrawDateTime='',
                 PickArray=[0 for x in range(20)],
                 Mega=0):
        self.draw_id = DrawID
        self.date_time = DrawDateTime
        self.setPick(ipick.iPick(PickArray))
        #self.numFromArray(PickArray)
        self.mega = Mega

        super().setupDBBase(iResult, iResult.draw_id, self.draw_id)

    def setup(self):
        r = self.db.session.query(iResult).filter(
            iResult.draw_id == self.draw_id).first()
        if (r is not None):
            self.date_time = r.date_time
            self.r1 = r.r1
            self.r2 = r.r2
            self.r3 = r.r3
            self.r4 = r.r4
            self.r5 = r.r5
            self.r6 = r.r6
            self.r7 = r.r7
            self.r8 = r.r8
            self.r9 = r.r9
            self.r10 = r.r10
            self.r11 = r.r11
            self.r12 = r.r12
            self.r13 = r.r13
            self.r14 = r.r14
            self.r15 = r.r15
            self.r16 = r.r16
            self.r17 = r.r17
            self.r18 = r.r18
            self.r19 = r.r19
            self.r20 = r.r20
            self.mega = r.mega
            self.pick = ipick.iPick(self.numToArray())

    def getPick(self):
        return self.pick

    def setPick(self, pick):
        self.pick = pick
        self.numFromArray(self.pick.getArray())

    def numFromArray(self, array20=[0 for x in range(20)]):
        self.r1 = array20[0]
        self.r2 = array20[1]
        self.r3 = array20[2]
        self.r4 = array20[3]
        self.r5 = array20[4]
        self.r6 = array20[5]
        self.r7 = array20[6]
        self.r8 = array20[7]
        self.r9 = array20[8]
        self.r10 = array20[9]
        self.r11 = array20[10]
        self.r12 = array20[11]
        self.r13 = array20[12]
        self.r14 = array20[13]
        self.r15 = array20[14]
        self.r16 = array20[15]
        self.r17 = array20[16]
        self.r18 = array20[17]
        self.r19 = array20[18]
        self.r20 = array20[19]
        #self.setPick(ipick.iPick(array20))

    def numToArray(self):
        numArray = [
            self.r1, self.r2, self.r3, self.r4, self.r5, self.r6, self.r7,
            self.r8, self.r9, self.r10, self.r11, self.r12, self.r13, self.r14,
            self.r15, self.r16, self.r17, self.r18, self.r19, self.r20
        ]
        return numArray

    def get_dict(self):
        dict = {}
        dict[self.draw_id] = np.array([
            self.r1, self.r2, self.r3, self.r4, self.r5, self.r6, self.r7,
            self.r8, self.r9, self.r10, self.r11, self.r12, self.r13, self.r14,
            self.r15, self.r16, self.r17, self.r18, self.r19, self.r20
        ])
        return dict

    def toString(self):
        return str(self.draw_id) + "[" + str(
            self.date_time) + "]" + self.pick.toString() + "[" + str(
                self.mega) + "]"

    def getLastDrawID(self):
        #return self.db.session.query(func.max(iResult.draw_id)).scalar();
        return self.db.session.query(iResult).order_by(
            iResult.draw_id.desc()).first().draw_id
Esempio n. 4
0
class iDepth(Base, DBBase):
    __tablename__ = 'depths'
    __table_args__ = {'extend_existing': True}
    db = db_init()

    draw_id = Column(Integer, primary_key=True, autoincrement=False)
    n1 = Column(Integer)
    n2 = Column(Integer)
    n3 = Column(Integer)
    n4 = Column(Integer)
    n5 = Column(Integer)
    n6 = Column(Integer)
    n7 = Column(Integer)
    n8 = Column(Integer)
    n9 = Column(Integer)
    n10 = Column(Integer)
    n11 = Column(Integer)
    n12 = Column(Integer)
    n13 = Column(Integer)
    n14 = Column(Integer)
    n15 = Column(Integer)
    n16 = Column(Integer)
    n17 = Column(Integer)
    n18 = Column(Integer)
    n19 = Column(Integer)
    n20 = Column(Integer)
    n21 = Column(Integer)
    n22 = Column(Integer)
    n23 = Column(Integer)
    n24 = Column(Integer)
    n25 = Column(Integer)
    n26 = Column(Integer)
    n27 = Column(Integer)
    n28 = Column(Integer)
    n29 = Column(Integer)
    n30 = Column(Integer)
    n31 = Column(Integer)
    n32 = Column(Integer)
    n33 = Column(Integer)
    n34 = Column(Integer)
    n35 = Column(Integer)
    n36 = Column(Integer)
    n37 = Column(Integer)
    n38 = Column(Integer)
    n39 = Column(Integer)
    n40 = Column(Integer)
    n41 = Column(Integer)
    n42 = Column(Integer)
    n43 = Column(Integer)
    n44 = Column(Integer)
    n45 = Column(Integer)
    n46 = Column(Integer)
    n47 = Column(Integer)
    n48 = Column(Integer)
    n49 = Column(Integer)
    n50 = Column(Integer)
    n51 = Column(Integer)
    n52 = Column(Integer)
    n53 = Column(Integer)
    n54 = Column(Integer)
    n55 = Column(Integer)
    n56 = Column(Integer)
    n57 = Column(Integer)
    n58 = Column(Integer)
    n59 = Column(Integer)
    n60 = Column(Integer)
    n61 = Column(Integer)
    n62 = Column(Integer)
    n63 = Column(Integer)
    n64 = Column(Integer)
    n65 = Column(Integer)
    n66 = Column(Integer)
    n67 = Column(Integer)
    n68 = Column(Integer)
    n69 = Column(Integer)
    n70 = Column(Integer)
    n71 = Column(Integer)
    n72 = Column(Integer)
    n73 = Column(Integer)
    n74 = Column(Integer)
    n75 = Column(Integer)
    n76 = Column(Integer)
    n77 = Column(Integer)
    n78 = Column(Integer)
    n79 = Column(Integer)
    n80 = Column(Integer)
    mega = Column(Integer)

    depthDict = {0: 0}

    def __init__(self, DrawID=0):
        self.draw_id = DrawID
        self.reset()

        super().setupDBBase(iDepth, iDepth.draw_id, self.draw_id)

    def reset(self):
        for i in range(81):
            self.depthDict[i] = 0
            if i > 0:
                setattr(self, "n" + str(i), 0)
        self.mega = 0

    def derive(self):
        if (self.draw_id > 0):
            cur_id = self.draw_id
            prev_id = self.draw_id - 1
            prev_depth = self.db.session.query(iDepth).filter(
                iDepth.draw_id == prev_id).first()
            cur_draw = idraw.iDraw(cur_id)
            for i in self.depthDict.keys():
                if i > 0:
                    if getattr(cur_draw, "n" + str(i)) == 1:
                        setattr(self, "n" + str(i), 0)
                    else:
                        setattr(self, "n" + str(i),
                                getattr(prev_depth, "n" + str(i)) + 1)

    def setup(self):
        d = self.db.session.query(iDepth).filter(
            iDepth.draw_id == self.draw_id).first()
        if d is not None:
            self.mega = d.mega
            for i in self.depthDict.keys():
                if i > 0:
                    setattr(self, "n" + str(i), getattr(d, "n" + str(i)))

    def get_dict(self):
        dict = {}
        dict[self.draw_id] = np.array([
            self.n1, self.n2, self.n3, self.n4, self.n5, self.n6, self.n7,
            self.n8, self.n9, self.n10, self.n11, self.n12, self.n13, self.n14,
            self.n15, self.n16, self.n17, self.n18, self.n19, self.n20,
            self.n21, self.n22, self.n23, self.n24, self.n25, self.n26,
            self.n27, self.n28, self.n29, self.n30, self.n31, self.n32,
            self.n33, self.n34, self.n35, self.n36, self.n37, self.n38,
            self.n39, self.n40, self.n41, self.n42, self.n43, self.n44,
            self.n45, self.n46, self.n47, self.n48, self.n49, self.n50,
            self.n51, self.n52, self.n53, self.n54, self.n55, self.n56,
            self.n57, self.n58, self.n59, self.n60, self.n61, self.n62,
            self.n63, self.n64, self.n65, self.n66, self.n67, self.n68,
            self.n69, self.n70, self.n71, self.n72, self.n73, self.n74,
            self.n75, self.n76, self.n77, self.n78, self.n79, self.n80
        ])
        return dict

    def toString(self):
        return str(self.get_dict()) + "[" + str(self.mega) + "]"

    def __get_dict__(self):
        dic = {}
        dic[self.draw_id] = {}

        i = 1
        while (i <= 80):
            dic[self.draw_id][i] = getattr(self, "n" + str(i))
            i += 1

        return dic
Esempio n. 5
0
class Keno:
    db = db_init()

    def get_last_draw_id(self):
        res = iresult.iResult()
        return res.getLastDrawID()

    def derive(self, from_id, to_id):
        result_id = from_id
        while (result_id <= to_id):
            new_draw = idraw.iDraw(result_id)
            new_draw.derive()
            new_draw.db_save()
            #new_draw.toString()

            new_depth = idepth.iDepth(result_id)
            new_depth.derive()
            new_depth.db_save()
            #new_depth.toString()
            result_id += 1

    def sync(self):
        last_draw_id = self.get_last_draw_id()
        #last_draw_id=307657;
        next_draw_id = last_draw_id + 1
        f = Fetcher()
        while (True):
            print("Syncing " + str(next_draw_id))
            new_res = f.fetch_result(next_draw_id)
            print(new_res.toString())
            new_res.db_save()

            new_draw = idraw.iDraw(next_draw_id)
            new_draw.derive()
            new_draw.db_save()
            #new_draw.toString()

            new_depth = idepth.iDepth(next_draw_id)
            new_depth.derive()
            new_depth.db_save()
            #new_depth.toString()

            next_draw_id += 1

    def get_all_results(self):
        master_dict = {}
        results = self.db.session.query(iresult.iResult).order_by(
            iresult.iResult.draw_id.asc()).all()
        for result in results:
            master_dict.update(result.get_dict())

        return master_dict

    def get_all_draws(self):
        master_dict = {}
        draws = self.db.session.query(idraw.iDraw).order_by(
            idraw.iDraw.draw_id.asc()).all()
        for draw in draws:
            master_dict.update(draw.get_dict())

        return master_dict

    def get_all_depths(self):
        master_dict = {}
        depths = self.db.session.query(idepth.iDepth).order_by(
            idepth.iDepth.draw_id.asc()).all()
        for depth in depths:
            master_dict.update(depth.get_dict())

        return master_dict

    def validate_db(self):
        val_obs = {
            'missing': [],
            'invalid': []
        }

        draw_ids_results = []
        first_draw = 1
        last_draw = 345678

        val_obs['missing']['results'] = sorted(
            set(range(first_draw, last_draw)).difference(draw_ids_results))

        master_results_d = self.get_all_results()
        master_draws_d = self.get_all_draws()
        master_depths_d = self.get_all_depths()

        master_draws_a = []
        for key, value in master_draws_d.items():
            master_draws_a.append(value)

        master_draws = np.matrix(master_draws_a)
        print(master_draws.shape)

        np.savez('master_draws', master_draws)
        #         np.savez('master_draws', master_draws.values())
        #         np.savez('master_depths', master_depths.values())
        #
        #         first_draw_id = self.db.session.query(iresult.iResult).order_by(iresult.iResult.draw_id.asc()).first().draw_id;
        #         last_draw_id = self.db.session.query(iresult.iResult).order_by(iresult.iResult.draw_id.asc()).first().draw_id;
        #         next_draw_id = first_draw_id + 1;
        #
        #         while (next_draw_id < last_draw_id):
        #             print(master_results[next_draw_id])
        #             print(master_draws[next_draw_id])
        #             print(master_depths[next_draw_id])
        #
        #             next_draw_id +=1;
        #
        #         next_draw_id = first_draw_id + 1;
        #         while (next_draw_id < last_draw_id):
        #             if next_draw_id not in master_results:  val_obs['missing']['results']= next_draw_id;
        #             if next_draw_id not in master_draws:  val_obs['missing']['draws']= next_draw_id;
        #             if next_draw_id in master_depths:  val_obs['missing']['depths']= next_draw_id;
        #
        #             next_draw_id +=1;
        #

        return val_obs

    def save_master_matrix(self):
        data_folder = 'strategies/data/keno/'
        master_results_d = self.get_all_results()
        master_draws_d = self.get_all_draws()
        master_depths_d = self.get_all_depths()

        master_results_a = []
        master_draws_a = []
        master_depths_a = []
        master_ids_a = []

        for key, value in master_draws_d.items():
            master_ids_a.append(np.array([key]))
            master_draws_a.append(value)
            master_results_a.append(master_results_d.get(key))

        for key, value in master_depths_d.items():
            master_depths_a.append(value)

        master_ids = np.matrix(master_ids_a)
        master_results = np.matrix(master_results_a)
        master_draws = np.matrix(master_draws_a).astype(float)
        master_depths = np.matrix(master_depths_a).astype(float)

        np.savez(data_folder + 'data_245M',
                 ids=master_ids,
                 results=master_results,
                 draws=master_draws,
                 depths=master_depths)
        np.savez(data_folder + 'master_ids_245M', ids=master_ids)
        np.savez(data_folder + 'master_results_245M', results=master_results)
        np.savez(data_folder + 'master_draws_245M',
                 draws=master_draws.astype(float))
        np.savez(data_folder + 'master_depths_245M',
                 depths=master_depths.astype(float))

        np.savez(data_folder + 'train_2M',
                 ids=master_ids[0:2000000],
                 train=master_depths[0:2000000],
                 target=master_draws[1:2000001])
        np.savez(data_folder + 'val_2M',
                 ids=master_ids[2000000:2001000],
                 val=master_depths[2000000:2001000],
                 target=master_draws[2000001:2001001])
        np.savez(data_folder + 'test_2M',
                 ids=master_ids[2001000:2002000],
                 test=master_depths[2001000:2002000],
                 target=master_draws[2001001:2002001])

        np.savez(data_folder + 'train_1M',
                 ids=master_ids[0:1000000],
                 train=master_depths[0:1000000],
                 target=master_draws[1:1000001])
        np.savez(data_folder + 'val_1M',
                 ids=master_ids[1000000:1001000],
                 val=master_depths[1000000:1001000],
                 target=master_draws[1000001:1001001])
        np.savez(data_folder + 'test_1M',
                 ids=master_ids[1001000:1002000],
                 test=master_depths[1001000:1002000],
                 target=master_draws[1001001:1002001])

        np.savez(data_folder + 'train_1-2M',
                 ids=master_ids[1000000:2000000],
                 train=master_depths[1000000:2000000],
                 target=master_draws[1000001:20010001])
        np.savez(data_folder + 'val_1-2M',
                 ids=master_ids[2000000:2001000],
                 val=master_depths[2000000:2001000],
                 target=master_draws[2000001:2001001])
        np.savez(data_folder + 'test_1-2M',
                 ids=master_ids[2001000:2002000],
                 test=master_depths[2001000:2002000],
                 target=master_draws[2001001:2002001])

        np.savez(data_folder + 'train_2-245M',
                 ids=master_ids[2000000:2245000],
                 train=master_depths[2000000:2245000],
                 target=master_draws[2000000:2245000])
        np.savez(data_folder + 'val_2-245M',
                 ids=master_ids[2245000:2246000],
                 val=master_depths[2245000:2246000],
                 target=master_draws[2245001:2246001])
        np.savez(data_folder + 'test_2-245M',
                 ids=master_ids[2246000:2247000],
                 test=master_depths[2246000:2247000],
                 target=master_draws[2246001:2247001])