Update requests.get within my python script

Question:

I am trying to build a sports betting ticker that streams the game match up with the current odds("line").
I am retrieving the data from a JSON via requests. I want to update the data at a fixed rate (every 2-3 mins) so that my odds are current. Here is the code I have pieced together. I have no coding training so pretty much used google-fu and stackoverflow for this.

Side note: I want to change the text color of each match up so it is easier to differentiate between games (red,green,yellow,red,etc..).

import requests
from PIL import Image, ImageDraw, ImageFont
import os

res = requests.get("myurl")

data = res.json()

games = []
for game in data['games']:
    for info in data['games'][game]:
        v1 = data['games'][game]['awayTeam'] + ' vs '
        v2 = data['games'][game]['homeTeam'] + ' '
        v3 = data['games'][game]['gameSpreadHomeHandicap'] + '    '
    games.append(v1 + v2 + v3)

with open('yourfile.txt', 'w') as file:
    file.write(' '.join(games))

with open('yourfile.txt', 'r') as file:
    output = file.readline()

im = Image.new("RGB", (8600, 16), "black")
draw = ImageDraw.Draw(im)
font = ImageFont.truetype(r'/usr/share/fonts/truetype/msttcorefonts/Arial.ttf', 13)
draw.text((0, 0), output, (0, 255, 0), font=font)

im.save("ticker.ppm")

os.system("sudo ./demo -D 1 --led-rows=16 --led-chain=3 ticker.ppm")

Thank you for your help.

Asked By: cc2644

||

Answers:

Move your code into a function e.g. stream_games(url), then call it from inside a while-loop, with a time.sleep() delay, like How can I make a time delay in Python?:

import time

while True:
    stream_games(url)
    time.sleep(180) # strictly this takes (180s + runtime of stream_games), not exactly 180s, but should be close enough.

Instead of passing your intermediate data around by writing to file yourfile.txt, use a list, array, json, pandas dataframe or whatever.

Answered By: smci
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.