cemior

Garmin json to csv

Aug 23rd, 2021 (edited)
1,376
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.42 KB | None | 0 0
  1. import os
  2. import csv
  3. import json
  4. import requests
  5.  
  6. # Opening JSON file
  7. f = open('yoga.json',)
  8.  
  9. # returns JSON object as a dictionary
  10. data = json.load(f)
  11.  
  12. #Close file
  13. f.close()
  14.  
  15. #Create output CVS file
  16. mf = open("yoga.csv", "a")
  17.  
  18. #write headers row
  19. mf.write("NAME_GARMIN"+";"+"CATEGORY_GARMIN"+";"+"Name"+";"+"Detailed"+";"+"Body parts"+";"+"Difficulty"+";"+"Equipment"+";"+"Focus"+";"+"Description"+";"+"Image1"+";"+"Image2"+";"+"URL"+";"+"\n")
  20.  
  21. # Iterating through the json list
  22. for category in data['categories']:
  23.    
  24.     for exercise in data['categories'][category]['exercises']:
  25.        
  26.         #set default values (when there is no detail for exercice)
  27.         NAME_GARMIN = exercise
  28.         CATEGORY_GARMIN = category
  29.         Name = exercise.replace('_',' ').title()
  30.         Detailed = "0"
  31.         Body_parts = ""
  32.         Difficulty = ""
  33.         Equipment = ""
  34.         Focus = ""
  35.         Description = ""
  36.         Image1 = ""
  37.         Image2 = ""
  38.         URL = ""
  39.  
  40.         #URL based on known path and exercice name
  41.         URLjson = 'https://connect.garmin.com/web-data/exercises/en-US/' + category+"/"+exercise + '.json'
  42.         page = requests.get(URLjson)
  43.  
  44.         #if page exists (200), then there are details for the exercice
  45.         if page.status_code == 200:
  46.  
  47.             exdata = json.loads(page.text)
  48.  
  49.             Detailed = "1"
  50.             Body_parts = exdata['bodyParts']
  51.             Difficulty = exdata['difficulty']
  52.             Equipment = exdata['equipment']
  53.             Focus = exdata['focuses']
  54.             Description = exdata['description']
  55.            
  56.             #try if there are images availables (I try just 2, but when there are, there are usually more)
  57.             try:
  58.                 Image1 = exdata['videos'][0]['thumbnail']
  59.             except:
  60.                 Image1 = ""
  61.             try:
  62.                 Image2 = exdata['videos'][1]['thumbnail']
  63.             except:
  64.                 Image2 = ""
  65.  
  66.             URL = "https://connect.garmin.com/modern/exercises/"+category+"/"+exercise
  67.  
  68.         #print the name of exercice (to see what's happening while the script is running)
  69.         print(exercise)
  70.  
  71.         #write the row data with exercice
  72.         mf.write(NAME_GARMIN+";"+CATEGORY_GARMIN+";"+Name+";"+Detailed+";"+Body_parts+";"+Difficulty+";"+Equipment+";"+Focus+";"+Description+";"+Image1+";"+Image2+";"+URL+";"+"\n")
  73.  
  74. #close output file
  75. mf.close()
Add Comment
Please, Sign In to add comment