Last active
August 15, 2023 08:09
-
-
Save tatocaster/5d4cf0f320ab0f22d94027c77296fc37 to your computer and use it in GitHub Desktop.
Download Nintendo Game data for my account
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"encoding/json" | |
"errors" | |
"fmt" | |
"io/ioutil" | |
"os" | |
"regexp" | |
"os/exec" | |
"path/filepath" | |
"strings" | |
) | |
type DeviceInfo struct { | |
Items []struct { | |
DeviceID string `json:"deviceId"` | |
} `json:"items"` | |
} | |
type UserStats struct { | |
DeviceID string `json:"deviceId"` | |
PlayedApps []struct { | |
ApplicationID string `json:"applicationId"` | |
Title string `json:"title"` | |
} `json:"playedApps"` | |
DevicePlayers []struct { | |
Nickname string `json:"nickname"` | |
} `json:"devicePlayers"` | |
} | |
func runCommand(command string, arg ...string) (string, error) { | |
cmd := exec.Command(command, arg...) | |
// Run the command and get its output | |
output, err := cmd.Output() | |
if err != nil { | |
fmt.Println("Error executing command:", err) | |
return "", errors.New("Error executing command") | |
} | |
result := string(output) | |
// Print the output | |
fmt.Println(result) | |
return result, nil | |
} | |
func getDeviceId() (string, error) { | |
jsonData, commandError := runCommand("nxapi", "pctl", "devices", "--json") | |
if commandError != nil { | |
return "", errors.New("cannot get devices, error in API") | |
} | |
var data DeviceInfo | |
if err := json.Unmarshal([]byte(jsonData), &data); err != nil { | |
fmt.Println("Error parsing JSON:", err) | |
return "", errors.New("Error parsing JSON") | |
} | |
// Assuming there is at least one item in the "items" array | |
deviceID := data.Items[0].DeviceID | |
fmt.Println("Device ID:", deviceID) | |
return deviceID, nil | |
} | |
func readFile(name string) (string, error) { | |
file, err := os.Open(name) | |
if err != nil { | |
fmt.Println(err) | |
return "", errors.New(fmt.Sprintf("cannot open file %s", name)) | |
} | |
defer file.Close() | |
// Read the contents of the file into a byte slice. | |
contents, err := ioutil.ReadFile(file.Name()) | |
if err != nil { | |
fmt.Println(err) | |
return "", errors.New("cannot read contents") | |
} | |
// Print the contents of the file. | |
return string(contents), nil | |
} | |
func getJSONFileList(folderPath string) ([]string, error) { | |
var fileList []string | |
err := filepath.Walk(folderPath, func(path string, info os.FileInfo, err error) error { | |
if err != nil { | |
return err | |
} | |
if !info.IsDir() && filepath.Ext(path) == ".json" && strings.Contains(path, "-monthly-") { | |
fileList = append(fileList, path) | |
} | |
return nil | |
}) | |
return fileList, err | |
} | |
func main() { | |
// deviceId, deviceErr := getDeviceId() | |
// if (deviceErr) != nil { | |
// fmt.Println(deviceErr) | |
// return | |
// } | |
// // download summaries | |
// runCommand("nxapi", "pctl", "dump-summaries", "/workspaces/codespaces-blank/data", "--device", deviceId) | |
// filter files | |
files, err := getJSONFileList("data/") | |
if err != nil { | |
fmt.Println("Error:", err) | |
return | |
} | |
// Use a map to keep track of distinct games | |
distinctGames := make(map[string]bool) | |
// Filter playedApps for the user "tatocaster" | |
var playedAppsForTatocaster []struct { | |
ApplicationID string `json:"applicationId"` | |
Title string `json:"title"` | |
} | |
// Iterate over the files and filter the JSON data. | |
for _, file := range files { | |
// Read the JSON data from the file. | |
data, err := readFile(file) | |
if err != nil { | |
fmt.Println(err) | |
return | |
} | |
var userStats UserStats | |
err = json.Unmarshal([]byte(data), &userStats) | |
if err != nil { | |
fmt.Printf("Error parsing JSON in file '%s': %v\n", file, err) | |
continue | |
} | |
for _, app := range userStats.PlayedApps { | |
if userStats.DevicePlayers[0].Nickname == "tatocaster" { | |
if _, ok := distinctGames[app.ApplicationID]; !ok { | |
distinctGames[app.ApplicationID] = true | |
playedAppsForTatocaster = append(playedAppsForTatocaster, app) | |
} | |
} | |
} | |
} | |
var allowedLettersInTitle = regexp.MustCompile(`[^a-zA-Z0-9 ]+`) | |
// Print the results | |
for _, app := range playedAppsForTatocaster { | |
fmt.Println(allowedLettersInTitle.ReplaceAllString(app.Title, "")) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment