Advertisement
Smugger

SteamCMD Get App Info Return as JSON

Nov 15th, 2019
958
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 4.95 KB | None | 0 0
  1. #!/bin/bash
  2. # Change this to the user you want to run steam as
  3. runAsUser="sdtd";
  4. # Change this to the location of steamcmd
  5. steamCMDshLocation="/home/sdtd/steamcmd/steamcmd.sh";
  6. # You will need to know the location of appinfo.vdf this file in order to delete it, or steam will not get an updated info, it will use cached
  7. # Refer to the current set location to know where it may be (./Steam/appcache/appinfo.vdf)
  8. steamAPPINFOvdfLocation="/home/sdtd/Steam/appcache/appinfo.vdf";
  9. rm ${steamAPPINFOvdfLocation};
  10.  
  11. # Get the app info from steam
  12. infoOutput=$(sudo -H -u ${runAsUser} bash -c "${steamCMDshLocation} +login anonymous +app_info_print 294420 +quit");
  13. # get the total lines
  14. tl=$(echo "${infoOutput}" | wc -l | cut -d " " -f 1);
  15. # Find the line that starts with "294420"
  16. sl=$(echo "${infoOutput}" | grep -n -m 1 \"294420\" | cut -d ":" -f 1);
  17. # Move only those lines to the variable we need to use
  18. sJSON=$(echo "${infoOutput}" | sed -n "${sl},${tl}p");
  19.  
  20. # <UPDATE>
  21. # We need to make some sort of error checking...
  22. # </UPDATE>
  23.  
  24. # Create an array that we need to use to affect lines
  25. b=();
  26.  
  27. # Set the IFS so we can do this by line
  28. IFS=$'\n';
  29.  
  30. # Add all the lines to the Array
  31. for c in $sJSON; do
  32.     b+=("$c");
  33. done;
  34.  
  35. # Get the total number of lines
  36. al=${#b[@]};
  37.  
  38. # Set the number of the last line in the array
  39. let ll=$al-1;
  40.  
  41. # Work through the array
  42. for (( i=0; i<${#b[@]}; i++ )); do
  43.    
  44.     # <UPDATE>
  45.     #
  46.     # Make sure it is not the last line ( Last line will be } anyway and we don't need to do anything with it )
  47.     # Although I did have some extra data at the end before, I need to make sure to clear that out, so before all this even starts, we need to check for the last } and cut out everything before it
  48.     #
  49.     # </UPDATE>
  50.    
  51.     if [ "$i" != "$ll" ]; then
  52.    
  53.         # Check for }
  54.         checkReg=`echo ${b[i]} | grep "[\}]"`;
  55.        
  56.         if [ "$checkReg" ]; then
  57.            
  58.             # Check if the next line starts with "
  59.             checkReg=`echo ${b[i+1]} | grep "\""`;
  60.            
  61.             if [ "$checkReg" ]; then
  62.                
  63.                 # Add , if the next line starts with ,
  64.                 b[$i]="${b[i]},";
  65.            
  66.             fi;
  67.            
  68.             # We are done with this line, move to next line
  69.             continue;
  70.        
  71.         fi;
  72.        
  73.         # If we did not pass the last check, then start another check
  74.         # Here we are checking to see if the line has "<something>" in it
  75.         checkReg=`echo ${b[i]} | grep -E "\"[a-zA-Z0-9\s\t \_\-\(\)\.]+\""`;
  76.        
  77.         if [ "$checkReg" ]; then
  78.            
  79.             # Find out if there is " space(s)/tabs "
  80.             checkReg=`echo ${b[i]} | grep -E "\"[[:space:]]+\""`;
  81.            
  82.            
  83.             if [ "$checkReg" ]; then
  84.                 # If We did find the above check then we need to remove the space and add : in that space
  85.                 b[$i]=`echo "${b[i]}" | sed -E "s/\"[\s\t ]+\"/\":\"/"`;
  86.                 # Now check if the next line has }
  87.                 checkReg=`echo ${b[i+1]} | grep -E "[\}]"`;
  88.                
  89.                 if [ ! "$checkReg" ]; then
  90.                    
  91.                     # If the next line was not } then we need to add a , at the end
  92.                     b[$i]="${b[i]},";
  93.                
  94.                 fi;
  95.            
  96.             else
  97.                
  98.                 # If there was not a space, then we need to
  99.                 b[$i]="${b[i]}:";
  100.            
  101.             fi;
  102.        
  103.         fi;
  104.    
  105.     fi;
  106.  
  107. done;
  108. sJSON="";
  109. sJSON+="{";
  110. for (( i=0; i<${#b[@]}; i++ )); do
  111.     sJSON+="${b[i]}";
  112. done;
  113. sJSON+="}";
  114.  
  115. # Output the compiled JSON file / you can display it using jq for clean display or  you could keep the output be sJSON variable if you really wanted to not have to use jq
  116. echo "${sJSON}"
  117. # OR YOU CAN USE jq to return a pretty version of this / Just comment the above line and uncomment the below line, this would require you to have jq installed
  118. #echo "${sJSON}" | jq ".";
  119.  
  120. # Example Usage: ( All these Examples do require you to have jq install Ubuntu/Debian: sudo apt-get install jq )
  121. # getBranches.sh is the name of the script file I have set, just change this to what you name the script file as.
  122.  
  123. # 1. This will build a JSON that will seperate each version with build id, time updated, and description
  124. #sudo ./getBranches.sh | jq ".[].depots.branches | to_entries[] | {"version": .key, "id": .value.buildid, "updated": .value.timeupdated, "desc": .value.description}";
  125.  
  126. # 2. This Example shows how you can iterate over all the branches and display the info for them.
  127. #vout=$(sudo ./getBranches.sh); V=$(echo "${vout}" | jq -r ".[].depots.branches | to_entries[].key"); for a in $V; do echo "Key: $a"; echo "${vout}" | jq -r ".[].depots.branches.\"$a\""; done
  128.  
  129. # 3. Get a specific branch you know the name of and return the buildid
  130. #sudo ./getBranches.sh | jq ".[].depots.branches.\"alpha18.0\".buildid";
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement