feat: xbox game bar support with exif handlin
This commit is contained in:
parent
9cf3092030
commit
8cf42422a8
|
@ -0,0 +1,39 @@
|
|||
package exif
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
|
||||
"github.com/barasher/go-exiftool"
|
||||
)
|
||||
|
||||
func GetTags(path string) (map[string]string, error) {
|
||||
et, err := exiftool.NewExiftool()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error intializing exiftool: %v\n", err)
|
||||
}
|
||||
defer et.Close()
|
||||
|
||||
fileInfos := et.ExtractMetadata(path)
|
||||
|
||||
if len(fileInfos) == 0 {
|
||||
return nil, fmt.Errorf("no metadata found for %s", path)
|
||||
}
|
||||
|
||||
result := make(map[string]string, len(fileInfos[0].Fields))
|
||||
|
||||
for _, fileInfo := range fileInfos {
|
||||
if fileInfo.Err != nil {
|
||||
return nil, fmt.Errorf("error parsing file exif for %v: %v", fileInfo.File, fileInfo.Err)
|
||||
}
|
||||
|
||||
for k := range fileInfo.Fields {
|
||||
result[k], err = fileInfo.GetString(k)
|
||||
if err != nil {
|
||||
log.Printf("error getting tag %s: %s", k, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
|
@ -1,64 +0,0 @@
|
|||
package xbox_game_bar
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
exiftool "github.com/barasher/go-exiftool"
|
||||
"github.com/rwcarlsen/goexif/exif"
|
||||
)
|
||||
|
||||
func getExifTagsWithOld(path string) error {
|
||||
fileDescriptor, errFileDescriptor := os.Open(path)
|
||||
if errFileDescriptor != nil {
|
||||
return fmt.Errorf("Couldn't open file %s: %s", path, errFileDescriptor)
|
||||
}
|
||||
|
||||
exifData, errExifData := exif.Decode(fileDescriptor)
|
||||
if errExifData != nil {
|
||||
return fmt.Errorf("Decoding EXIF data from %s: %s", path, errExifData)
|
||||
}
|
||||
|
||||
defer fileDescriptor.Close()
|
||||
|
||||
t, _ := exifData.MarshalJSON()
|
||||
log.Println(t)
|
||||
|
||||
titleTag := "Title"
|
||||
if strings.Contains(path, ".png") {
|
||||
titleTag = "Microsoft Game DVR Title"
|
||||
}
|
||||
title, err := exifData.Get(exif.FieldName(titleTag))
|
||||
if err != nil {
|
||||
return fmt.Errorf("Error getting tag %s from %s: %s", titleTag, path, errExifData)
|
||||
}
|
||||
|
||||
log.Println(title)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func getExifTags(path string) error {
|
||||
et, err := exiftool.NewExiftool()
|
||||
if err != nil {
|
||||
fmt.Printf("Error when intializing: %v\n", err)
|
||||
return nil
|
||||
}
|
||||
defer et.Close()
|
||||
|
||||
fileInfos := et.ExtractMetadata(path)
|
||||
|
||||
for _, fileInfo := range fileInfos {
|
||||
if fileInfo.Err != nil {
|
||||
fmt.Printf("Error concerning %v: %v\n", fileInfo.File, fileInfo.Err)
|
||||
continue
|
||||
}
|
||||
|
||||
log.Println(fileInfo.GetString("MicrosoftGameDVRTitle"))
|
||||
log.Println(fileInfo.GetString("Title"))
|
||||
|
||||
}
|
||||
return nil
|
||||
}
|
|
@ -3,17 +3,18 @@ package xbox_game_bar
|
|||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/fmartingr/games-screenshot-manager/internal/exif"
|
||||
"github.com/fmartingr/games-screenshot-manager/internal/models"
|
||||
"github.com/fmartingr/games-screenshot-manager/pkg/helpers"
|
||||
"github.com/gosimple/slug"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
const Name = "xbox-game-bar"
|
||||
const platformName = "Xbox Game Bar"
|
||||
const platformName = "PC"
|
||||
|
||||
type XboxGameBarProvider struct {
|
||||
logger *logrus.Entry
|
||||
|
@ -23,25 +24,50 @@ func (p *XboxGameBarProvider) FindGames(options models.ProviderOptions) ([]*mode
|
|||
var userGames []*models.Game
|
||||
|
||||
path := helpers.ExpandUser(options.InputPath)
|
||||
log.Print(path)
|
||||
|
||||
files, err := ioutil.ReadDir(path)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error reading from path %s: %s", options.InputPath, err)
|
||||
}
|
||||
|
||||
games := make(map[string]*models.Game)
|
||||
|
||||
for _, file := range files {
|
||||
fullPath := filepath.Join(path, file.Name())
|
||||
log.Println(fullPath)
|
||||
|
||||
if strings.Contains(file.Name(), ".png") || strings.Contains(file.Name(), ".mp4") {
|
||||
if err := getExifTags(fullPath); err != nil {
|
||||
tags, err := exif.GetTags(fullPath)
|
||||
if err != nil {
|
||||
p.logger.Errorf("err: %s", err)
|
||||
continue
|
||||
}
|
||||
|
||||
// game.Screenshots = append(game.Screenshots, models.NewScreenshotWithoutDestination(path+"/"+file.Name()))
|
||||
titleTag := "MicrosoftGameDVRTitle"
|
||||
if strings.Contains(file.Name(), ".mp4") {
|
||||
titleTag = "Title"
|
||||
}
|
||||
|
||||
gameName := tags[titleTag]
|
||||
|
||||
game, exists := games[tags[titleTag]]
|
||||
if !exists {
|
||||
game = &models.Game{
|
||||
ID: slug.Make(gameName),
|
||||
Name: gameName,
|
||||
Platform: platformName,
|
||||
Provider: Name,
|
||||
}
|
||||
games[tags[titleTag]] = game
|
||||
}
|
||||
|
||||
game.Screenshots = append(game.Screenshots, models.NewScreenshotWithoutDestination(path+"/"+file.Name()))
|
||||
}
|
||||
}
|
||||
|
||||
for _, g := range games {
|
||||
userGames = append(userGames, g)
|
||||
}
|
||||
|
||||
return userGames, nil
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue