diff --git a/.gitignore b/.gitignore index d6cbda8..205bd65 100755 --- a/.gitignore +++ b/.gitignore @@ -4,7 +4,7 @@ dump.rdb *.csv # Git .git/* -# Python cache, cron output, lock files and pre-change sheet backups made on the server +# cache, cron logs and the excel backups/locks __pycache__/ cron.log* excel_lotto_records/.*.lock diff --git a/cash25.py b/cash25.py index 9967e70..e50d0f1 100755 --- a/cash25.py +++ b/cash25.py @@ -8,7 +8,7 @@ from collections import Counter import csv def get_data(): - # Old records from the excel sheets, wvlottery.com took these down but they go back to the early 90s on some games + # previous records which have since been removed from wvlottery.com's database df_old = pd.read_excel('./excel_lotto_records/cash25.xlsx') salvaged = df_old[['Date', 'Numbers']] @@ -16,7 +16,7 @@ def get_data(): header = { "User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.75 Safari/537.36", } - # Get web data, the new site pulls past draws from json 500 at a time + # Get web data, 500 draws a page web = {'Date': [], 'Numbers': []} page = 0 while True: @@ -31,7 +31,7 @@ def get_data(): dfs = [pd.DataFrame(web)] pd.set_option('display.max_rows', None) # Specifies no max rows, otherwise only shows 10 records - # Adds any draw not in the sheet yet, so the excel record keeps growing + # adds new draws to the excel so the records keep growing df = records.merge_save('./excel_lotto_records/cash25.xlsx', dfs[0], fallback=salvaged) df2 = df[['Date', 'Numbers']] date = list(df2['Date']) @@ -41,14 +41,13 @@ def get_data(): date, nums = get_data() # Formatting -# Site numbers come split by – and the excel records by - so it splits on both +# site uses – and the excel uses - hyphenfree = [] for x in nums: hyphenfree.append(x.replace('–',', ').replace('-',', ')) splitlist = ", ".join(hyphenfree) sep = splitlist.split(", ") -# Only count numbers that can still be called, the ball ranges have changed over the years -# so the old records have some numbers that dont exist in the game anymore +# ball range changed over the years so drop numbers that arent in the game anymore sep = [x for x in sep if x.isdigit() and 1 <= int(x) <= 25] for n in range(0, 25): @@ -57,9 +56,7 @@ for n in range(0, 25): frequency = [v[-1] for v in most_common] sorted_nums = sorted(likely_nums, key=lambda x: (len(x), x)) -# Chance = 1 / every possible ticket, 25 choose 6 = 177100 -# every ticket has the same odds, the forecast just goes with the numbers that get called the most -Chance = frac(1, comb(25, 6)) +Chance = frac(1, comb(25, 6)) # Chance = 1 / all possible tickets i.e. 177100 Forecast = str(" - ".join(sorted_nums)) #print(f"Likely numbers are . . . {Forecast} \n" diff --git a/cashpop.py b/cashpop.py index 8fabe81..ce8e857 100755 --- a/cashpop.py +++ b/cashpop.py @@ -11,7 +11,7 @@ def get_data(): header = { "User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.75 Safari/537.36", } - # Get web data, cash pop draws every 15 min so only use the last ~3000 draws (about a month) + # Get web data, cash pop is drawn every 15 min so ~3000 draws is about a month web = {'Date': [], 'Numbers': []} ids, times = [], [] page = 0 @@ -27,7 +27,7 @@ def get_data(): break page += 1 df2 = pd.DataFrame(web) - # The site only keeps a year of cash pop draws, keep our own record of every draw + # site only keeps a year of cash pop, keep our own record records.merge_save('./excel_lotto_records/cash_pop.xlsx', df2, ids=ids, times=times) date = list(df2['Date']) nums = list(df2['Numbers'].astype('str')) @@ -41,7 +41,7 @@ for x in nums: hyphenfree.append(x.replace('–',', ')) splitlist = ", ".join(hyphenfree) sep = splitlist.split(", ") -# Only keep 1 - 15, just in case anything weird comes back from the site +# only 1 - 15 sep = [x for x in sep if x.isdigit() and 1 <= int(x) <= 15] for n in range(0, 15): @@ -51,7 +51,7 @@ for n in range(0, 15): Forecast = likely_nums[0] Backups = str(" - ".join(likely_nums[1:])) -Chance = frac(1, 15) # Only one number is drawn out of 15 so every pick has the same chance +Chance = frac(1, 15) # one number out of 15 #print(f"Likely number is . . . {Forecast} Backups: {Backups}\n" #f"With percent chance of winning being {Chance}") diff --git a/daily3.py b/daily3.py index f747040..39e66ca 100755 --- a/daily3.py +++ b/daily3.py @@ -7,7 +7,7 @@ from collections import Counter import csv def get_data(): - # Old records from the excel sheets, wvlottery.com took these down but they go back to the early 90s on some games + # previous records which have since been removed from wvlottery.com's database df_old = pd.read_excel('./excel_lotto_records/daily3.xlsx') salvaged = df_old[['Date', 'Numbers']] @@ -15,7 +15,7 @@ def get_data(): header = { "User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.75 Safari/537.36", } - # Get web data, the new site pulls past draws from json 500 at a time + # Get web data, 500 draws a page web = {'Date': [], 'Numbers': []} page = 0 while True: @@ -30,7 +30,7 @@ def get_data(): dfs = [pd.DataFrame(web)] pd.set_option('display.max_rows', None) # Specifies no max rows, otherwise only shows 10 records - # Adds any draw not in the sheet yet, so the excel record keeps growing + # adds new draws to the excel so the records keep growing df = records.merge_save('./excel_lotto_records/daily3.xlsx', dfs[0], fallback=salvaged) df2 = df[['Date', 'Numbers']] date = list(df2['Date']) @@ -40,13 +40,13 @@ def get_data(): date, nums = get_data() # Formatting -# Site numbers come split by – and the excel records by - so it splits on both +# site uses – and the excel uses - hyphenfree = [] for x in nums: hyphenfree.append(x.replace('–',', ').replace('-',', ')) splitlist = ", ".join(hyphenfree) sep = splitlist.split(", ") -# Only keep actual digits, gets rid of any blanks from the excel records +# gets rid of blanks from the excel sep = [x for x in sep if x.isdigit() and int(x) <= 9] for n in range(0, 10): @@ -55,9 +55,7 @@ for n in range(0, 10): frequency = [v[-1] for v in most_common] sorted_nums = sorted(likely_nums, key=lambda x: (len(x), x)) -# Chance = playing the forecast as a box (any order), 3 different digits can come up 6 ways out of 1000 = 3/500 -# playing it straight (exact order) is 1/1000 -Chance = frac(6, 1000) +Chance = frac(6, 1000) # box play, 6 ways out of 1000 (straight is 1/1000) Forecast = str(" - ".join(sorted_nums)) #print(f"Likely numbers are . . . {Forecast} \n" diff --git a/daily4.py b/daily4.py index 72cf602..dfd2926 100755 --- a/daily4.py +++ b/daily4.py @@ -7,7 +7,7 @@ from collections import Counter import csv def get_data(): - # Old records from the excel sheets, wvlottery.com took these down but they go back to the early 90s on some games + # previous records which have since been removed from wvlottery.com's database df_old = pd.read_excel('./excel_lotto_records/daily4.xlsx') salvaged = df_old[['Date', 'Numbers']] @@ -15,7 +15,7 @@ def get_data(): header = { "User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.75 Safari/537.36", } - # Get web data, the new site pulls past draws from json 500 at a time + # Get web data, 500 draws a page web = {'Date': [], 'Numbers': []} page = 0 while True: @@ -30,7 +30,7 @@ def get_data(): dfs = [pd.DataFrame(web)] pd.set_option('display.max_rows', None) # Specifies no max rows, otherwise only shows 10 records - # Adds any draw not in the sheet yet, so the excel record keeps growing + # adds new draws to the excel so the records keep growing df = records.merge_save('./excel_lotto_records/daily4.xlsx', dfs[0], fallback=salvaged) df2 = df[['Date', 'Numbers']] date = list(df2['Date']) @@ -40,13 +40,13 @@ def get_data(): date, nums = get_data() # Formatting -# Site numbers come split by – and the excel records by - so it splits on both +# site uses – and the excel uses - hyphenfree = [] for x in nums: hyphenfree.append(x.replace('–',', ').replace('-',', ')) splitlist = ", ".join(hyphenfree) sep = splitlist.split(", ") -# Only keep actual digits, gets rid of any blanks from the excel records +# gets rid of blanks from the excel sep = [x for x in sep if x.isdigit() and int(x) <= 9] for n in range(0, 9): @@ -55,9 +55,7 @@ for n in range(0, 9): frequency = [v[-1] for v in most_common] sorted_nums = sorted(likely_nums, key=lambda x: (len(x), x)) -# Chance = playing the forecast as a box (any order), 4 different digits can come up 24 ways out of 10000 = 3/1250 -# playing it straight (exact order) is 1/10000 -Chance = frac(24, 10000) +Chance = frac(24, 10000) # box play, 24 ways out of 10000 (straight is 1/10000) Forecast = str(" - ".join(sorted_nums)) #print(f"Likely numbers are . . . {Forecast} \n" diff --git a/excel_lotto_records/cash25.xlsx b/excel_lotto_records/cash25.xlsx index 2a324b4..f63f7ce 100644 Binary files a/excel_lotto_records/cash25.xlsx and b/excel_lotto_records/cash25.xlsx differ diff --git a/excel_lotto_records/cash_pop.xlsx b/excel_lotto_records/cash_pop.xlsx index f307aa7..be56cae 100644 Binary files a/excel_lotto_records/cash_pop.xlsx and b/excel_lotto_records/cash_pop.xlsx differ diff --git a/excel_lotto_records/daily3.xlsx b/excel_lotto_records/daily3.xlsx index cece31e..ab50a6c 100644 Binary files a/excel_lotto_records/daily3.xlsx and b/excel_lotto_records/daily3.xlsx differ diff --git a/excel_lotto_records/daily4.xlsx b/excel_lotto_records/daily4.xlsx index 014679b..a803cf5 100644 Binary files a/excel_lotto_records/daily4.xlsx and b/excel_lotto_records/daily4.xlsx differ diff --git a/excel_lotto_records/lotto.xlsx b/excel_lotto_records/lotto.xlsx index e23cb22..8d27229 100644 Binary files a/excel_lotto_records/lotto.xlsx and b/excel_lotto_records/lotto.xlsx differ diff --git a/excel_lotto_records/lotto_america.xlsx b/excel_lotto_records/lotto_america.xlsx index 5710a43..786feb4 100644 Binary files a/excel_lotto_records/lotto_america.xlsx and b/excel_lotto_records/lotto_america.xlsx differ diff --git a/excel_lotto_records/lotto_megamil.xlsx b/excel_lotto_records/lotto_megamil.xlsx index 98ea52c..733e0e4 100644 Binary files a/excel_lotto_records/lotto_megamil.xlsx and b/excel_lotto_records/lotto_megamil.xlsx differ diff --git a/lotto_america.py b/lotto_america.py index c7c8d37..89f627b 100755 --- a/lotto_america.py +++ b/lotto_america.py @@ -8,7 +8,7 @@ from collections import Counter import csv def get_data(): - # Old records from the excel sheets, wvlottery.com took these down but they go back to the early 90s on some games + # previous records which have since been removed from wvlottery.com's database df_old = pd.read_excel('./excel_lotto_records/lotto_america.xlsx') salvaged = df_old[['Date', 'Numbers', 'SB']] @@ -16,7 +16,7 @@ def get_data(): header = { "User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.75 Safari/537.36", } - # Get web data, the new site pulls past draws from json 500 at a time + # Get web data, 500 draws a page web = {'Date': [], 'Numbers': [], 'SB': []} page = 0 while True: @@ -32,7 +32,7 @@ def get_data(): dfs = [pd.DataFrame(web)] pd.set_option('display.max_rows', None) # Specifies no max rows, otherwise only shows 10 records - # Adds any draw not in the sheet yet, so the excel record keeps growing + # adds new draws to the excel so the records keep growing df = records.merge_save('./excel_lotto_records/lotto_america.xlsx', dfs[0], fallback=salvaged) df2 = df[['Date', 'Numbers', 'SB']] date = list(df2['Date']) @@ -43,14 +43,13 @@ def get_data(): date, nums, SBs = get_data() # Formatting -# Site numbers come split by – and the excel records by - so it splits on both +# site uses – and the excel uses - hyphenfree = [] for x in nums: hyphenfree.append(x.replace('–',', ').replace('-',', ')) splitlist = ", ".join(hyphenfree) sep = splitlist.split(", ") -# Only count numbers that can still be called, the ball ranges have changed over the years -# so the old records have some numbers that dont exist in the game anymore +# ball range changed over the years so drop numbers that arent in the game anymore sep = [x for x in sep if x.isdigit() and 1 <= int(x) <= 52] SBs = [x for x in SBs if 1 <= x <= 10] @@ -65,9 +64,7 @@ for n in range(0, 10): frequency_sb = [v[-1] for v in most_common_sb] sorted_nums = sorted(likely_nums, key=lambda x: (len(x), x)) -# Chance = 1 / every possible ticket, 52 choose 5 white balls times 10 star balls = 25989600 -# every ticket has the same odds, the forecast just goes with the numbers that get called the most -Chance = frac(1, comb(52, 5) * 10) +Chance = frac(1, comb(52, 5) * 10) # Chance = 1 / all possible tickets i.e. 25989600 Forecast = str(" - ".join(sorted_nums)) #print(f"Likely numbers are . . . {Forecast} SB: {SB}\n" diff --git a/lottopy_pb.py b/lottopy_pb.py index 058f1c5..cfe9286 100755 --- a/lottopy_pb.py +++ b/lottopy_pb.py @@ -8,7 +8,7 @@ from collections import Counter import csv def get_data(): - # Old records from the excel sheets, wvlottery.com took these down but they go back to the early 90s on some games + # previous records which have since been removed from wvlottery.com's database df_old = pd.read_excel('./excel_lotto_records/lotto.xlsx') salvaged = df_old[['Date', 'Numbers', 'PB']] @@ -16,7 +16,7 @@ def get_data(): header = { "User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.75 Safari/537.36", } - # Get web data, the new site pulls past draws from json 500 at a time + # Get web data, 500 draws a page web = {'Date': [], 'Numbers': [], 'PB': []} page = 0 while True: @@ -32,7 +32,7 @@ def get_data(): dfs = [pd.DataFrame(web)] pd.set_option('display.max_rows', None) # Specifies no max rows, otherwise only shows 10 records - # Adds any draw not in the sheet yet, so the excel record keeps growing + # adds new draws to the excel so the records keep growing df = records.merge_save('./excel_lotto_records/lotto.xlsx', dfs[0], fallback=salvaged) df2 = df[['Date', 'Numbers', 'PB']] date = list(df2['Date']) @@ -43,14 +43,13 @@ def get_data(): date, nums, PBs = get_data() # Formatting -# Site numbers come split by – and the excel records by - so it splits on both +# site uses – and the excel uses - hyphenfree = [] for x in nums: hyphenfree.append(x.replace('–',', ').replace('-',', ')) splitlist = ", ".join(hyphenfree) sep = splitlist.split(", ") -# Only count numbers that can still be called, the ball ranges have changed over the years -# so the old records have some numbers that dont exist in the game anymore +# ball range changed over the years so drop numbers that arent in the game anymore sep = [x for x in sep if x.isdigit() and 1 <= int(x) <= 69] PBs = [x for x in PBs if 1 <= x <= 26] @@ -65,9 +64,7 @@ for n in range(0, 26): frequency_pb = [v[-1] for v in most_common_pb] sorted_nums = sorted(likely_nums, key=lambda x: (len(x), x)) -# Chance = 1 / every possible ticket, 69 choose 5 white balls times 26 powerballs = 292201338 -# every ticket has the same odds, the forecast just goes with the numbers that get called the most -Chance = frac(1, comb(69, 5) * 26) +Chance = frac(1, comb(69, 5) * 26) # Chance = 1 / all possible tickets i.e. 292201338 Forecast = str(" - ".join(sorted_nums)) #print(f"Likely numbers are . . . {Forecast} PB: {PB}\n" diff --git a/megamil.py b/megamil.py index 0ed358e..156f25e 100755 --- a/megamil.py +++ b/megamil.py @@ -9,7 +9,7 @@ import csv def get_data(): - # Old records from the excel sheets, wvlottery.com took these down but they go back to the early 90s on some games + # previous records which have since been removed from wvlottery.com's database df_old = pd.read_excel('./excel_lotto_records/lotto_megamil.xlsx') salvaged = df_old[['Date', 'Numbers', 'MB']] @@ -17,7 +17,7 @@ def get_data(): header = { "User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.75 Safari/537.36", } - # Get web data, the new site pulls past draws from json 500 at a time + # Get web data, 500 draws a page web = {'Date': [], 'Numbers': [], 'MB': []} page = 0 while True: @@ -33,7 +33,7 @@ def get_data(): dfs = [pd.DataFrame(web)] pd.set_option('display.max_rows', None) # Specifies no max rows, otherwise only shows 10 records - # Adds any draw not in the sheet yet, so the excel record keeps growing + # adds new draws to the excel so the records keep growing df = records.merge_save('./excel_lotto_records/lotto_megamil.xlsx', dfs[0], fallback=salvaged) df2 = df[['Date', 'Numbers', 'MB']] date = list(df2['Date']) @@ -44,14 +44,13 @@ def get_data(): date, nums, MBs = get_data() # Formatting -# Site numbers come split by – and the excel records by - so it splits on both +# site uses – and the excel uses - hyphenfree = [] for x in nums: hyphenfree.append(x.replace('–',', ').replace('-',', ')) splitlist = ", ".join(hyphenfree) sep = splitlist.split(", ") -# Only count numbers that can still be called, the ball ranges have changed over the years -# so the old records have some numbers that dont exist in the game anymore +# ball range changed over the years so drop numbers that arent in the game anymore sep = [x for x in sep if x.isdigit() and 1 <= int(x) <= 70] MBs = [x for x in MBs if 1 <= x <= 24] @@ -66,9 +65,7 @@ for n in range(0, 25): frequency_mb = [v[-1] for v in most_common_mb] sorted_nums = sorted(likely_nums, key=lambda x: (len(x), x)) -# Chance = 1 / every possible ticket, 70 choose 5 white balls times 24 mega balls = 290472336 -# every ticket has the same odds, the forecast just goes with the numbers that get called the most -Chance = frac(1, comb(70, 5) * 24) +Chance = frac(1, comb(70, 5) * 24) # Chance = 1 / all possible tickets i.e. 290472336 Forecast = str(" - ".join(sorted_nums)) #print(f"Likely numbers are . . . {Forecast} MB: {MB}\n" diff --git a/records.py b/records.py index 0584413..00c6750 100644 --- a/records.py +++ b/records.py @@ -1,6 +1,7 @@ -# WVLottopy records: keeps the excel history files growing as draws happen -# wvlottery.com only keeps ~3 years of results (Cash Pop one year), so every run appends any draw that is -# not in the sheet yet. Nothing is ever removed or rewritten, existing rows stay exactly as they are. +# WVLottopy: Matteo DiBiagio +# wvlottery.com only keeps about 3 years of results (cash pop only 1) so this adds every new draw +# to the excel sheets each run, that way the records keep growing instead of getting lost again. +# Nothing already in the sheets gets changed or removed. import fcntl import os import shutil @@ -10,8 +11,8 @@ import pandas as pd def draw_day(x): - # the site's drawingDate is the UTC date, so late evening draws (powerball, mega millions, lotto america at ~11pm ET) - # come out a day late. The real draw day is drawDate converted to Eastern time. + # drawingDate from the site is in UTC so the late draws (powerball, megamillions, lotto america) + # show up a day late, converting drawDate to eastern time gives the real day try: from datetime import datetime from zoneinfo import ZoneInfo @@ -22,7 +23,7 @@ def draw_day(x): def _fmt_date(d): - # site gives 2026-10-09, the sheets use 10/09/2026 + # site dates are 2026-10-09, the sheets use 10/09/2026 s = str(d)[:10] if len(s) == 10 and s[4] == '-': return f'{s[5:7]}/{s[8:10]}/{s[0:4]}' @@ -30,18 +31,18 @@ def _fmt_date(d): def _fmt_nums(n): - # site numbers come split by an en dash, the sheets use a hyphen + # site splits numbers with – and the sheets use - return str(n).replace('–', '-').strip() def _newer_first(df): - # some sheets are oldest first, some newest first + # some of the sheets are newest first and some are oldest first d = pd.to_datetime(df['Date'], errors='coerce').dropna() return len(d) > 1 and d.iloc[0] > d.iloc[-1] def merge_save(path, web_df, fallback=None, ids=None, times=None): - # recording must never break the forecast: if anything goes wrong the old behaviour (site + sheet) is used + # if saving fails for whatever reason just use the site + old sheet so the numbers still come out try: return _merge_save(path, web_df, ids, times) except Exception as e: @@ -52,9 +53,8 @@ def merge_save(path, web_df, fallback=None, ids=None, times=None): def _merge_save(path, web_df, ids=None, times=None): - """Append draws from the site (web_df: Date, Numbers[, bonus col]) that the sheet at path does not have yet. - ids: optional list of site draw ids, same length as web_df. Returns the whole sheet as Date/Numbers/[bonus] - so callers can keep counting from one source. If the site gave nothing the sheet is left alone.""" + # web_df is Date, Numbers and the bonus ball column if the game has one + # ids is the site's draw id for each row (cash pop needs it), returns the whole sheet bonus = [c for c in web_df.columns if c not in ('Date', 'Numbers')] if os.path.exists(path): old = pd.read_excel(path) @@ -74,9 +74,9 @@ def _merge_save(path, web_df, ids=None, times=None): def key(r): return (str(r['Date']), _fmt_nums(r['Numbers'])) + tuple(str(int(r[c])) if str(r[c]) not in ('nan', 'None', '') else '' for c in bonus) + # rows with a draw id get matched by the id, date + numbers is only for the old rows without one + # (cash pop draws 1 number out of 15 like 90 times a day so the same date + number happens a lot) have_ids = set(old['DrawID'].dropna().astype(str)) if 'DrawID' in old.columns else set() - # Rows that carry a draw id are matched by id only. The Date+Numbers match is just for old rows without an id: - # cash pop draws one number out of 15 about 90 times a day, so other draws on the same day share Date+Numbers. no_id = old['DrawID'].isna() if 'DrawID' in old.columns else pd.Series(True, index=old.index) have = Counter(key(r) for _, r in old[no_id].iterrows()) keep = [] @@ -94,6 +94,7 @@ def _merge_save(path, web_df, ids=None, times=None): lock = open(os.path.join(os.path.dirname(path) or '.', '.' + os.path.basename(path) + '.lock'), 'w') fcntl.flock(lock, fcntl.LOCK_EX) try: + # one time backup of the sheet before it ever gets added to backup = path + '.pre-recording' if os.path.exists(path) and not os.path.exists(backup): shutil.copy2(path, backup) @@ -112,5 +113,4 @@ def _merge_save(path, web_df, ids=None, times=None): print(f'records: {os.path.basename(path)} +{len(new)} new draws') else: full = old - out = full[['Date', 'Numbers'] + bonus].copy() - return out + return full[['Date', 'Numbers'] + bonus].copy()