1

I'm using request to grab a json file (here's one for instance: http://gd2.mlb.com/components/game/mlb/year_2015/month_06/day_10/grid.json) and trying to read the data. I'm interesting in looping through games and look at the score for each game. The hierarchy goes a little something like this:

data
    games
        game
            home_score
            away_score

I have the following code together that grabs the file for a given date

 "Download the grid json file for the given YEAR, MONTH and DAY."                                                                          
  (interactive)                                                                                                                             
  (let ((file-url (format "http://gd2.mlb.com/components/game/mlb/%s/grid.json" (mlb/format-date year month day)))                          
        (file-tmp (concat temporary-file-directory (format "mlb-%04d-%02d-%02d.json" year month day))))                                     
    (request file-url                                                                                                                       
             :parser (lambda ()                                                                                                             
                       (let ((json-object-type 'alist))                                                                                     
                         (json-read)))                                                                                                      
             :success (function*                                                                                                            
                       (lambda (&key data &allow-other-keys)                                                                                
                         (let* (                                                                                                            
                                (game (elt (assoc-default 'data data) 0))                                                                   
                                (a (assoc-default 'game game))                                                                              
                                )                                                                                                           
                           (message "%s" a)                                                                                                 
                           )                                                                                                                

                         ))                                                                                                                 
             :status-code                                                                                                                   
             '((400 . (lambda (&rest _) (message "Got 400.")))                                                                              
               (418 . (lambda (&rest _) (message "Got 418."))))                                                                             
             :complete                                                                                                                      
             )                                                                                                                              
    )                                                                                                                                       
  )

Along with this quick function to format the date to be used in the URL:

(defun mlb/format-date (year month day)                                                                                                     
  "Formats the given YEAR, MONTH and DAY as a path on mlb.com."                                                                             
  (let* ((a (format "year_%04d/month_%02d/day_%02d" year month day)))                                                                       
    a)                                                                                                                                      
  )

When evaluating this for a given date, it'll output something like this in the message buffer (truncated):

[((away_code . phi) (tbd_flag . N) (group . MLB) (calendar_event_id . 14-414537-2015-06-10) (home_team_id . 113) (event_time . 12:35 PM) (a\
way_file_code . phi) (home_file_code . cin) (venue . Great American Ball Park) (away_name_abbrev . PHI) (game_pk . 414537) (inning . 9) (aw\
ay_score . 2) (home_code . cin) (status . Final) (home_score . 5) (away_team_id . 143) (gameday_sw . P) (venue_id . 2602) (ind . F) (home_t\
eam_name . Reds) (top_inning . Y) (media_state . media_archive) (home_name_abbrev . CIN) (id . 2015/06/10/phimlb-cinmlb-1) (away_team_name \
. Phillies) (double_header_sw . N) (game_nbr . 1) (game_media (homebase (media . [((type . mlbtv_away) (display . CSP-HD) (state . MEDIA_AR\
CHIVE) (playback_scenario . HTTP_CLOUD_WIRED) (free . NO) (combined_media_state . MEDIA_ARCHIVE) (blackout . ) (id . 135111383)) ((type . m\
lbtv_away) (display . CSP-HD) (state . MEDIA_ARCHIVE) (playback_scenario . HTTP_CLOUD_WIRED_ADS) (free . NO) (combined_media_state . MEDIA_\
ARCHIVE) (blackout . ) (id . 135111383)) ((type . mlbtv_away) (display . CSP-HD) (state . MEDIA_ARCHIVE) (playback_scenario . HTTP_CLOUD_WI\
RED_IRDETO) (free . NO) (combined_media_state . MEDIA_ARCHIVE) (blackout . ) (id . 135111383)) ((type . mlbtv_away) (display . CSP-HD) (sta\
te . MEDIA_ARCHIVE) (playback_scenario . HTTP_CLOUD_WIRED_WEB) (free . NO) (combined_media_state . MEDIA_ARCHIVE) (blackout . ) (id . 13511\
1383)) ((type . mlbtv_away) (display . CSP-HD) (state . MEDIA_ARCHIVE) (playback_scenario . FMS_CLOUD) (free . NO) (combined_media_state . \
MEDIA_ARCHIVE) (blackout . ) (id . 135111383))

I'm looking for the best way to loop through this mess and extra the fields I'm interested in at this point. Could anyone provide any tips?

Thanks!

  • I was able to get the fields I wanted with this: (cl-map 'list (lambda (alist) (cdr (assoc 'day alist))) vect) – mast Dec 1 '15 at 0:19
  • Did you look at the let-alist package? It lets you access nested alists with a dotted path notation. I can write a more detailed answer later, if you're interested. – lunaryorn Dec 1 '15 at 17:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.