Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- # Paths and settings
- games_dir="$HOME/Games/Heroic"
- json_file="$HOME/.var/app/com.heroicgameslauncher.hgl/config/heroic/sideload_apps/library.json"
- temp_json="/tmp/library_temp.json"
- API_KEY="ENTER YOUR STEAMGRIDDB API KEY HERE"
- # Ensure the JSON file exists
- if [[ ! -f "$json_file" ]]; then
- echo "library.json not found, creating a new one..."
- mkdir -p "$(dirname "$json_file")"
- echo '{"games": []}' > "$json_file"
- fi
- # Extract existing folder_names from JSON
- existing_entries=$(jq -r '.games[].folder_name' "$json_file")
- cp "$json_file" "$temp_json"
- # Function: longest common substring
- longest_common_substring_length() {
- local s1="$1"
- local s2="$2"
- local max=0
- local len1=${#s1}
- local len2=${#s2}
- for ((i=0; i<len1; i++)); do
- for ((j=0; j<len2; j++)); do
- local lcs=0
- while [[ $((i+lcs)) -lt $len1 && $((j+lcs)) -lt $len2 && "${s1:i+lcs:1}" == "${s2:j+lcs:1}" ]]; do
- ((lcs++))
- done
- (( lcs > max )) && max=$lcs
- done
- done
- echo $max
- }
- # Function: prettify folder name
- make_nice_title() {
- local original="$1"
- local cleaned=$(echo "$original" | sed -E 's/steamrip\.com//Ig')
- cleaned="${cleaned//-/ }"
- cleaned=$(echo "$cleaned" | tr -s ' ')
- cleaned=$(echo "$cleaned" | sed 's/^[[:space:]]*//;s/[[:space:]]*$//')
- echo "$cleaned"
- }
- # Function: fetch SteamGridDB art URL
- fetch_steamgriddb_image() {
- local game_name="$1"
- local encoded_game_name
- encoded_game_name=$(python3 -c "import urllib.parse; print(urllib.parse.quote('''$game_name'''))")
- # Search by name
- local game_search_response
- game_search_response=$(curl -s -H "Authorization: Bearer $API_KEY" \
- "https://www.steamgriddb.com/api/v2/search/autocomplete/$encoded_game_name")
- local game_id
- game_id=$(echo "$game_search_response" | jq -r '.data[0].id // empty')
- if [ -z "$game_id" ]; then
- echo ""
- return
- fi
- # Fetch top rated grid
- local grids_response
- grids_response=$(curl -s -H "Authorization: Bearer $API_KEY" \
- "https://www.steamgriddb.com/api/v2/grids/game/$game_id?order_by=score&order_direction=desc")
- local grid_url
- grid_url=$(echo "$grids_response" | jq -r '.data[0].url // empty')
- echo "$grid_url"
- }
- # Process games directory
- for folder_path in "$games_dir"/*/; do
- folder_path="${folder_path%/}"
- folder_name_only=$(basename "$folder_path")
- # Skip 'prefixes' folder explicitly
- if [[ "$folder_name_only" == "prefixes" ]]; then
- echo "Skipping prefixes folder."
- continue
- fi
- # Skip if this folder path already exists in JSON
- if echo "$existing_entries" | grep -Fxq "$folder_path"; then
- echo "Skipping already entered entry: $folder_path"
- continue
- fi
- echo "Processing new folder: $folder_path"
- mapfile -t exe_files < <(find "$folder_path" -type f -iname '*.exe')
- if [[ ${#exe_files[@]} -eq 0 ]]; then
- echo "No .exe files found in $folder_path, skipping."
- continue
- fi
- best_exe=""
- best_score=0
- for exe_file in "${exe_files[@]}"; do
- exe_base=$(basename "$exe_file" .exe)
- exe_lower=$(echo "$exe_base" | tr '[:upper:]' '[:lower:]')
- score=$(longest_common_substring_length "$(echo "$folder_name_only" | tr '[:upper:]' '[:lower:]')" "$exe_lower")
- if (( score > best_score )); then
- best_score=$score
- best_exe="$exe_file"
- fi
- done
- if [[ -z "$best_exe" ]]; then
- echo "Could not determine suitable .exe for $folder_path, skipping."
- continue
- fi
- echo "Selected executable: $best_exe"
- app_name=$(uuidgen)
- nice_title=$(make_nice_title "$folder_name_only")
- echo "Fetching SteamGridDB image for '$nice_title'..."
- cover_url=$(fetch_steamgriddb_image "$nice_title")
- if [ -n "$cover_url" ]; then
- echo "Fetched cover art: $cover_url"
- else
- echo "No cover art found for '$nice_title'"
- fi
- new_game=$(jq -n \
- --arg runner "sideload" \
- --arg app_name "$app_name" \
- --arg title "$nice_title" \
- --arg exe "$best_exe" \
- --arg folder "$folder_path" \
- --argjson is_dlc false \
- --arg platform "Windows" \
- --argjson null_val null \
- --arg art_url "$cover_url" \
- '{
- runner: $runner,
- app_name: $app_name,
- title: $title,
- install: {
- executable: $exe,
- platform: $platform,
- is_dlc: $is_dlc
- },
- folder_name: $folder,
- art_cover: ($art_url | select(length > 0) // $null_val),
- art_square: ($art_url | select(length > 0) // $null_val),
- is_installed: true,
- canRunOffline: true,
- browserUrl: "",
- customUserAgent: "",
- launchFullScreen: false
- }'
- )
- jq ".games += [$new_game]" "$temp_json" > "${temp_json}.new" && mv "${temp_json}.new" "$temp_json"
- done
- mv "$temp_json" "$json_file"
- echo "Library JSON update complete with SteamGridDB covers. 'prefixes' folder skipped."
Add Comment
Please, Sign In to add comment