0

I am trying to go back to March 29th 2018 and run through each day till today. Also save each day individually to .csv and change the name BOOKS_thedate.csv. I have been meaning to apply datetime another way and any help understanding this would be much appreciated.

import csv
import requests
import datetime
from pprint import pprint

outfile = open("BOOKS.csv","w",newline='')
writer = csv.writer(outfile)
writer.writerow(["book_ids","status","away_team_id","away_rot","ml_away","spread_away","spread_away_line","home_team_id",
                 "home_rot","ml_home","spread_home","spread_home_line","over","under","total","type_odds","insert","time",
                 "one_tm_id","one_tm","one_team","two_tm_id","two_tm","two_team"])

req2 = requests.get('https://api-prod.sprtactn.co/web/v1/scoreboard/mlb?bookIds=21,1,55&date=' + datetime.datetime.now().strftime('%Y%m%d') + '')
odd = req2.json()['games']

for info in odd:
    time = info['start_time']
    status = info['status']
    away_rot = info['away_rotation_number']
    home_rot = info['home_rotation_number']
    away_team_id = info['away_team_id']
    home_team_id = info['home_team_id']
    teams = info['teams']
    vegas = info['odds']
    one_team = teams[0]['full_name']
    one_tm = teams[0]['abbr']
    one_tm_id = teams[0]['id']
    two_team = teams[1]['full_name']
    two_tm = teams[1]['abbr']
    two_tm_id = teams[1]['id']
    for odds in vegas:
        ml_away = odds['ml_away']
        ml_home = odds['ml_home']
        type_odds = odds['type']
        insert = odds['inserted']
        book_ids = odds['book_id']
        spread_away = odds['spread_away']
        spread_home = odds['spread_home']
        spread_away_line = odds['spread_away_line']
        spread_home_line = odds['spread_home_line']
        over = odds['over']
        under = odds['under']
        total = odds['total']


        print(book_ids, status, away_team_id, away_rot, ml_away, spread_away, spread_away_line, 
              home_team_id, home_rot, ml_home, spread_home, spread_home_line,
              over, under, total, type_odds, insert, time, one_tm_id, one_tm, one_team, two_tm_id, two_tm, two_team)

        writer.writerow([book_ids, status, away_team_id, away_rot, ml_away, spread_away, spread_away_line, 
              home_team_id, home_rot, ml_home, spread_home, spread_home_line,
              over, under, total, type_odds, insert, time, one_tm_id, one_tm, one_team, two_tm_id, two_tm, two_team])

1 Answer 1

1

An easy way to do this is to use the Pendulum library (https://pendulum.eustace.io/docs/).

Calculate the start and end times, then the period based on these. Execute a for-loop that provides one datetime item for each interval within the period, in this case for every day.

Within the loop you can format the datetime to provide a string for use in the URL and for naming the csv, as shown.

>>> import pendulum
>>> start = pendulum.datetime(2018, 3, 29)
>>> end = pendulum.today()
>>> period = pendulum.period(start, end)
>>> for dt in period.range('days'):
...     dt.format('%Y%m%d')
...     break
... 
'20180329'
Sign up to request clarification or add additional context in comments.

6 Comments

What did you use to print the date like so. I made a string the_date = dt and it gives me 2018-03-29T00:00:00+00:00
Printing the date in the way that you did makes pendulum print dt in its default format. If you want it formatted then use dt.format(<format>). For instance, print(dt.format('%Y%m%d')) in the code above would have yielded 20180329 (ie, without the apostrophes).
>>> import pendulum >>> start = pendulum.datetime(2018, 3, 29) >>> end = pendulum.today() >>> period = pendulum.period(start, end) >>> for dt in period.range('days'): dt.format('%Y%m%d') break '%2018%0%4' This is what I receive after following it step by step..
Apologies! With version 2 the format function has changed, and I was using version 1. dt.format('YYYYMMDD') gives '20180329'.
There it is! I was using lower case m and d aswell which was printing the day. Thanks for the help Bill! Now I would pass the period str in the URL and put the_date str into the csv, right? Do I have to add in outfile.close() so it closes and moves to the next?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.