Merge pull request #13 from owogawc/california

Add parsing for California
main
Taylor Hood 2 years ago committed by GitHub
commit 17b37ec549
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -0,0 +1,63 @@
import locale
import logging
import json
import operator
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)
grand_prize = sorted(game_["prizeTiers"], key=operator.itemgetter("value"))[-1]
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": num_tx_initial(game_),
"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))

@ -0,0 +1,19 @@
import json
import subprocess
import unittest
class TestCalifornia(unittest.TestCase):
def test_all(self):
result = subprocess.run(
["python3", "-m", "lottery_data_scraper.california"], capture_output=True
)
data = json.loads(result.stdout)
self.assertEqual(
data[0]["game_id"], "1405", "Expected the first game to be PAC-MAN, #1405."
)
self.assertEqual(
data[0]["num_tx_initial"],
37080000,
"Expected 37,080,000 tickets for PAC-MAN #1405.",
)
Loading…
Cancel
Save