Guest User

json.sh scripttt

a guest
Oct 30th, 2025
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 5.16 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. # Paths and settings
  4. games_dir="$HOME/Games/Heroic"
  5. json_file="$HOME/.var/app/com.heroicgameslauncher.hgl/config/heroic/sideload_apps/library.json"
  6. temp_json="/tmp/library_temp.json"
  7. API_KEY="ENTER YOUR STEAMGRIDDB API KEY HERE"
  8.  
  9. # Ensure the JSON file exists
  10. if [[ ! -f "$json_file" ]]; then
  11.     echo "library.json not found, creating a new one..."
  12.     mkdir -p "$(dirname "$json_file")"
  13.     echo '{"games": []}' > "$json_file"
  14. fi
  15.  
  16. # Extract existing folder_names from JSON
  17. existing_entries=$(jq -r '.games[].folder_name' "$json_file")
  18.  
  19. cp "$json_file" "$temp_json"
  20.  
  21. # Function: longest common substring
  22. longest_common_substring_length() {
  23.     local s1="$1"
  24.     local s2="$2"
  25.     local max=0
  26.     local len1=${#s1}
  27.     local len2=${#s2}
  28.     for ((i=0; i<len1; i++)); do
  29.         for ((j=0; j<len2; j++)); do
  30.             local lcs=0
  31.             while [[ $((i+lcs)) -lt $len1 && $((j+lcs)) -lt $len2 && "${s1:i+lcs:1}" == "${s2:j+lcs:1}" ]]; do
  32.                 ((lcs++))
  33.             done
  34.             (( lcs > max )) && max=$lcs
  35.         done
  36.     done
  37.     echo $max
  38. }
  39.  
  40. # Function: prettify folder name
  41. make_nice_title() {
  42.     local original="$1"
  43.     local cleaned=$(echo "$original" | sed -E 's/steamrip\.com//Ig')
  44.     cleaned="${cleaned//-/ }"
  45.     cleaned=$(echo "$cleaned" | tr -s ' ')
  46.     cleaned=$(echo "$cleaned" | sed 's/^[[:space:]]*//;s/[[:space:]]*$//')
  47.     echo "$cleaned"
  48. }
  49.  
  50. # Function: fetch SteamGridDB art URL
  51. fetch_steamgriddb_image() {
  52.     local game_name="$1"
  53.     local encoded_game_name
  54.     encoded_game_name=$(python3 -c "import urllib.parse; print(urllib.parse.quote('''$game_name'''))")
  55.  
  56.     # Search by name
  57.     local game_search_response
  58.     game_search_response=$(curl -s -H "Authorization: Bearer $API_KEY" \
  59.         "https://www.steamgriddb.com/api/v2/search/autocomplete/$encoded_game_name")
  60.  
  61.     local game_id
  62.     game_id=$(echo "$game_search_response" | jq -r '.data[0].id // empty')
  63.  
  64.     if [ -z "$game_id" ]; then
  65.         echo ""
  66.         return
  67.     fi
  68.  
  69.     # Fetch top rated grid
  70.     local grids_response
  71.     grids_response=$(curl -s -H "Authorization: Bearer $API_KEY" \
  72.         "https://www.steamgriddb.com/api/v2/grids/game/$game_id?order_by=score&order_direction=desc")
  73.  
  74.     local grid_url
  75.     grid_url=$(echo "$grids_response" | jq -r '.data[0].url // empty')
  76.  
  77.     echo "$grid_url"
  78. }
  79.  
  80. # Process games directory
  81. for folder_path in "$games_dir"/*/; do
  82.     folder_path="${folder_path%/}"
  83.     folder_name_only=$(basename "$folder_path")
  84.  
  85.     # Skip 'prefixes' folder explicitly
  86.     if [[ "$folder_name_only" == "prefixes" ]]; then
  87.         echo "Skipping prefixes folder."
  88.         continue
  89.     fi
  90.  
  91.     # Skip if this folder path already exists in JSON
  92.     if echo "$existing_entries" | grep -Fxq "$folder_path"; then
  93.         echo "Skipping already entered entry: $folder_path"
  94.         continue
  95.     fi
  96.  
  97.     echo "Processing new folder: $folder_path"
  98.  
  99.     mapfile -t exe_files < <(find "$folder_path" -type f -iname '*.exe')
  100.  
  101.     if [[ ${#exe_files[@]} -eq 0 ]]; then
  102.         echo "No .exe files found in $folder_path, skipping."
  103.         continue
  104.     fi
  105.  
  106.     best_exe=""
  107.     best_score=0
  108.     for exe_file in "${exe_files[@]}"; do
  109.         exe_base=$(basename "$exe_file" .exe)
  110.         exe_lower=$(echo "$exe_base" | tr '[:upper:]' '[:lower:]')
  111.         score=$(longest_common_substring_length "$(echo "$folder_name_only" | tr '[:upper:]' '[:lower:]')" "$exe_lower")
  112.         if (( score > best_score )); then
  113.             best_score=$score
  114.             best_exe="$exe_file"
  115.         fi
  116.     done
  117.  
  118.     if [[ -z "$best_exe" ]]; then
  119.         echo "Could not determine suitable .exe for $folder_path, skipping."
  120.         continue
  121.     fi
  122.  
  123.     echo "Selected executable: $best_exe"
  124.  
  125.     app_name=$(uuidgen)
  126.     nice_title=$(make_nice_title "$folder_name_only")
  127.  
  128.     echo "Fetching SteamGridDB image for '$nice_title'..."
  129.     cover_url=$(fetch_steamgriddb_image "$nice_title")
  130.  
  131.     if [ -n "$cover_url" ]; then
  132.         echo "Fetched cover art: $cover_url"
  133.     else
  134.         echo "No cover art found for '$nice_title'"
  135.     fi
  136.  
  137.     new_game=$(jq -n \
  138.       --arg runner "sideload" \
  139.       --arg app_name "$app_name" \
  140.       --arg title "$nice_title" \
  141.       --arg exe "$best_exe" \
  142.       --arg folder "$folder_path" \
  143.       --argjson is_dlc false \
  144.       --arg platform "Windows" \
  145.       --argjson null_val null \
  146.       --arg art_url "$cover_url" \
  147.       '{
  148.        runner: $runner,
  149.        app_name: $app_name,
  150.        title: $title,
  151.        install: {
  152.          executable: $exe,
  153.          platform: $platform,
  154.          is_dlc: $is_dlc
  155.        },
  156.        folder_name: $folder,
  157.        art_cover: ($art_url | select(length > 0) // $null_val),
  158.        art_square: ($art_url | select(length > 0) // $null_val),
  159.        is_installed: true,
  160.        canRunOffline: true,
  161.        browserUrl: "",
  162.        customUserAgent: "",
  163.        launchFullScreen: false
  164.      }'
  165.     )
  166.  
  167.     jq ".games += [$new_game]" "$temp_json" > "${temp_json}.new" && mv "${temp_json}.new" "$temp_json"
  168. done
  169.  
  170. mv "$temp_json" "$json_file"
  171.  
  172. echo "Library JSON update complete with SteamGridDB covers. 'prefixes' folder skipped."
  173.  
Add Comment
Please, Sign In to add comment