From e20bad2d386492734bccb71c2761ff3e5b7a5cff Mon Sep 17 00:00:00 2001 From: Eric Ihli Date: Thu, 20 Apr 2023 22:36:31 -0700 Subject: [PATCH] Add parsing for California --- lottery_data_scraper/california.py | 59 ++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 lottery_data_scraper/california.py diff --git a/lottery_data_scraper/california.py b/lottery_data_scraper/california.py new file mode 100644 index 0000000..b38fd38 --- /dev/null +++ b/lottery_data_scraper/california.py @@ -0,0 +1,59 @@ +import locale +import logging +import json +import requests +import html2text + +from lottery_data_scraper.schemas import GameSchema +from lottery_data_scraper.util import fetch_html + +# Set local for currency conversion and formatting +# because California only gives prize values and our schema +# expects a string representation of the prize. +# https://docs.python.org/3/library/locale.html +locale.setlocale(locale.LC_ALL, 'en_US.utf8') + +logger = logging.getLogger(__name__) +h = html2text.HTML2Text() + +BASE_URL = "https://www.calottery.com" +SCRATCHER_URL = "https://www.calottery.com/api/games/scratchers" + +def num_tx_initial(game): + grand_prize = game["topPrizeTier"] + return grand_prize["odds"] * grand_prize["totalNumberOfPrizes"] + + +def fetch_games(): + response = json.loads(fetch_html(SCRATCHER_URL)) + games = [] + for game_ in response["games"]: + prizes = [] + for prize_ in game_["prizeTiers"]: + prize = { + "available": prize_["numberOfPrizesPending"], + "claimed": prize_["numberOfPrizesCashed"], + "value": prize_["value"], + "prize": locale.currency(prize_["value"], grouping=True)[:-3] # -3 to drop the cents + } + prizes.append(prize) + game = { + "game_id": game_["gameNumber"], + "name": game_["name"], + "desription": h.handle(game_["description"]), + "image_urls": [game_["unScratchedImage"], game_["scratchedImage"]], + "how_to_play": h.handle(game_["howToPlay"]), + "num_tx_initial": sum(prize["available"] + prize["claimed"] for prize in prizes), + "price": game_["price"], + "prizes": prizes, + "state": "tx", + "url": BASE_URL + game_["productPage"], + } + games.append(game) + return games + + +if __name__ == "__main__": + games = fetch_games() + schema = GameSchema(many=True) + print(schema.dumps(games))