/
externalidextractor.go
56 lines (48 loc) · 2.07 KB
/
externalidextractor.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package igdbapi
import (
"fmt"
"regexp"
)
type ExternalIds struct {
Official *string // official website, "homepage"
SteamId *string
GogSlug *string
EnglishWikipediaSlug *string
RedditSlug *string
GooglePlayAppId *string
AppleAppStoreAppId *string
}
type idExtractor func(url string, ids *ExternalIds) error
// keyed by IGDB's website "category" (= specific website)
var extractorByCategory = map[int]idExtractor{
// <anything>
WebsiteOfficial: func(url string, ids *ExternalIds) error {
ids.Official = &url
return nil
},
// https://store.steampowered.com/app/270910
WebsiteSteam: regex(`steampowered.com/app/(\d+)`, func(key string, ids *ExternalIds) { ids.SteamId = &key }),
// https://www.gog.com/game/worms_world_party_remastered
WebsiteGog: regex(`gog.com/game/([^/]+)`, func(key string, ids *ExternalIds) { ids.GogSlug = &key }),
// https://en.wikipedia.org/wiki/Battle_City_(video_game)
// https://wikipedia.org/wiki/Sonic_Colors
WebsiteWikipedia: regex(`(?:en.)?wikipedia.org/wiki/(.+)`, func(key string, ids *ExternalIds) { ids.EnglishWikipediaSlug = &key }),
// https://www.reddit.com/r/dukenukem/
WebsiteReddit: regex(`reddit.com/r/([^/]+)`, func(key string, ids *ExternalIds) { ids.RedditSlug = &key }),
// https://play.google.com/store/apps/details?id=com.frogmind.badland&hl=en
WebsiteAndroid: regex(`\?id=([^&]+)`, func(key string, ids *ExternalIds) { ids.GooglePlayAppId = &key }),
// https://itunes.apple.com/us/app/badland/id535176909?mt=8&uo=4
WebsiteIphone: regex(`/id(\d+)`, func(key string, ids *ExternalIds) { ids.AppleAppStoreAppId = &key }),
WebsiteIpad: regex(`/id(\d+)`, func(key string, ids *ExternalIds) { ids.AppleAppStoreAppId = &key }),
}
func regex(expression string, assign func(string, *ExternalIds)) idExtractor {
idRegex := regexp.MustCompile(expression)
return func(url string, ids *ExternalIds) error {
matches := idRegex.FindStringSubmatch(url)
if matches == nil {
return fmt.Errorf("unable to parse ID from URL: %s", url)
}
assign(matches[1], ids)
return nil
}
}