Repos / gorts / 5e64ca62ae
commit 5e64ca62ae9f2fa512601268e6ea6a6df6651e1e
Author: Nhân <hi@imnhan.com>
Date:   Mon Jun 19 21:44:37 2023 +0700

    WIP players
    
    Got country name-to-code mappings. Now let's actually fetch them.

diff --git a/.gitignore b/.gitignore
index 58c4c79..8682b36 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,2 +1,4 @@
 /dist
 /web/state.json
+/players.csv
+/creds*
diff --git a/startgg/countries.go b/startgg/countries.go
new file mode 100644
index 0000000..5d1ed9e
--- /dev/null
+++ b/startgg/countries.go
@@ -0,0 +1,262 @@
+package startgg
+
+// Startgg only shows (non-standard) country names (ongoing problem for years,
+// probably will never be fixed), so we need this mapping to convert these
+// names to alpha-2 codes.
+// From https://gist.github.com/morleym/a39c43f6544a350c109c5f7b0b055155
+var nameToCode = map[string]string{
+	"Bangladesh":                         "bd",
+	"Belgium":                            "be",
+	"Burkina Faso":                       "bf",
+	"Bulgaria":                           "bg",
+	"Bosnia and Herzegovina":             "ba",
+	"Barbados":                           "bb",
+	"Wallis and Futuna":                  "wf",
+	"Saint Barthelemy":                   "bl",
+	"Bermuda":                            "bm",
+	"Brunei":                             "bn",
+	"Bolivia":                            "bo",
+	"Bahrain":                            "bh",
+	"Burundi":                            "bi",
+	"Benin":                              "bj",
+	"Bhutan":                             "bt",
+	"Jamaica":                            "jm",
+	"Bouvet Island":                      "bv",
+	"Botswana":                           "bw",
+	"Samoa":                              "ws",
+	"Bonaire, Saint Eustatius and Saba ": "bq",
+	"Brazil":                             "br",
+	"Bahamas":                            "bs",
+	"Jersey":                             "je",
+	"Belarus":                            "by",
+	"Belize":                             "bz",
+	"Russia":                             "ru",
+	"Rwanda":                             "rw",
+	"Serbia":                             "rs",
+	"East Timor":                         "tl",
+	"Reunion":                            "re",
+	"Turkmenistan":                       "tm",
+	"Tajikistan":                         "tj",
+	"Romania":                            "ro",
+	"Tokelau":                            "tk",
+	"Guinea-Bissau":                      "gw",
+	"Guam":                               "gu",
+	"Guatemala":                          "gt",
+	"South Georgia and the South Sandwich Islands": "gs",
+	"Greece":                               "gr",
+	"Equatorial Guinea":                    "gq",
+	"Guadeloupe":                           "gp",
+	"Japan":                                "jp",
+	"Guyana":                               "gy",
+	"Guernsey":                             "gg",
+	"French Guiana":                        "gf",
+	"Georgia":                              "ge",
+	"Grenada":                              "gd",
+	"United Kingdom":                       "gb",
+	"Gabon":                                "ga",
+	"El Salvador":                          "sv",
+	"Guinea":                               "gn",
+	"Gambia":                               "gm",
+	"Greenland":                            "gl",
+	"Gibraltar":                            "gi",
+	"Ghana":                                "gh",
+	"Oman":                                 "om",
+	"Tunisia":                              "tn",
+	"Jordan":                               "jo",
+	"Croatia":                              "hr",
+	"Haiti":                                "ht",
+	"Hungary":                              "hu",
+	"Hong Kong":                            "hk",
+	"Honduras":                             "hn",
+	"Heard Island and McDonald Islands":    "hm",
+	"Venezuela":                            "ve",
+	"Puerto Rico":                          "pr",
+	"Palestinian Territory":                "ps",
+	"Palau":                                "pw",
+	"Portugal":                             "pt",
+	"Svalbard and Jan Mayen":               "sj",
+	"Paraguay":                             "py",
+	"Iraq":                                 "iq",
+	"Panama":                               "pa",
+	"French Polynesia":                     "pf",
+	"Papua New Guinea":                     "pg",
+	"Peru":                                 "pe",
+	"Pakistan":                             "pk",
+	"Philippines":                          "ph",
+	"Pitcairn":                             "pn",
+	"Poland":                               "pl",
+	"Saint Pierre and Miquelon":            "pm",
+	"Zambia":                               "zm",
+	"Western Sahara":                       "eh",
+	"Estonia":                              "ee",
+	"Egypt":                                "eg",
+	"South Africa":                         "za",
+	"Ecuador":                              "ec",
+	"Italy":                                "it",
+	"Vietnam":                              "vn",
+	"Solomon Islands":                      "sb",
+	"Ethiopia":                             "et",
+	"Somalia":                              "so",
+	"Zimbabwe":                             "zw",
+	"Saudi Arabia":                         "sa",
+	"Spain":                                "es",
+	"Eritrea":                              "er",
+	"Montenegro":                           "me",
+	"Moldova":                              "md",
+	"Madagascar":                           "mg",
+	"Saint Martin":                         "mf",
+	"Morocco":                              "ma",
+	"Monaco":                               "mc",
+	"Uzbekistan":                           "uz",
+	"Myanmar":                              "mm",
+	"Mali":                                 "ml",
+	"Macao":                                "mo",
+	"Mongolia":                             "mn",
+	"Marshall Islands":                     "mh",
+	"Macedonia":                            "mk",
+	"Mauritius":                            "mu",
+	"Malta":                                "mt",
+	"Malawi":                               "mw",
+	"Maldives":                             "mv",
+	"Martinique":                           "mq",
+	"Northern Mariana Islands":             "mp",
+	"Montserrat":                           "ms",
+	"Mauritania":                           "mr",
+	"Isle of Man":                          "im",
+	"Uganda":                               "ug",
+	"Tanzania":                             "tz",
+	"Malaysia":                             "my",
+	"Mexico":                               "mx",
+	"Israel":                               "il",
+	"France":                               "fr",
+	"British Indian Ocean Territory":       "io",
+	"Saint Helena":                         "sh",
+	"Finland":                              "fi",
+	"Fiji":                                 "fj",
+	"Falkland Islands":                     "fk",
+	"Micronesia":                           "fm",
+	"Faroe Islands":                        "fo",
+	"Nicaragua":                            "ni",
+	"Netherlands":                          "nl",
+	"Norway":                               "no",
+	"Namibia":                              "na",
+	"Vanuatu":                              "vu",
+	"New Caledonia":                        "nc",
+	"Niger":                                "ne",
+	"Norfolk Island":                       "nf",
+	"Nigeria":                              "ng",
+	"New Zealand":                          "nz",
+	"Nepal":                                "np",
+	"Nauru":                                "nr",
+	"Niue":                                 "nu",
+	"Cook Islands":                         "ck",
+	"Kosovo":                               "xk",
+	"Ivory Coast":                          "ci",
+	"Switzerland":                          "ch",
+	"Colombia":                             "co",
+	"China":                                "cn",
+	"Cameroon":                             "cm",
+	"Chile":                                "cl",
+	"Cocos Islands":                        "cc",
+	"Canada":                               "ca",
+	"CA":                                   "ca",
+	"Republic of the Congo":                "cg",
+	"Central African Republic":             "cf",
+	"Democratic Republic of the Congo":     "cd",
+	"Czech Republic":                       "cz",
+	"Cyprus":                               "cy",
+	"Christmas Island":                     "cx",
+	"Costa Rica":                           "cr",
+	"Curacao":                              "cw",
+	"Cape Verde":                           "cv",
+	"Cuba":                                 "cu",
+	"Swaziland":                            "sz",
+	"Syria":                                "sy",
+	"Sint Maarten":                         "sx",
+	"Kyrgyzstan":                           "kg",
+	"Kenya":                                "ke",
+	"South Sudan":                          "ss",
+	"Suriname":                             "sr",
+	"Kiribati":                             "ki",
+	"Cambodia":                             "kh",
+	"Saint Kitts and Nevis":                "kn",
+	"Comoros":                              "km",
+	"Sao Tome and Principe":                "st",
+	"Slovakia":                             "sk",
+	"South Korea":                          "kr",
+	"Slovenia":                             "si",
+	"North Korea":                          "kp",
+	"Kuwait":                               "kw",
+	"Senegal":                              "sn",
+	"San Marino":                           "sm",
+	"Sierra Leone":                         "sl",
+	"Seychelles":                           "sc",
+	"Kazakhstan":                           "kz",
+	"Cayman Islands":                       "ky",
+	"Singapore":                            "sg",
+	"Sweden":                               "se",
+	"Sudan":                                "sd",
+	"Dominican Republic":                   "do",
+	"Dominica":                             "dm",
+	"Djibouti":                             "dj",
+	"Denmark":                              "dk",
+	"British Virgin Islands":               "vg",
+	"Germany":                              "de",
+	"Yemen":                                "ye",
+	"Algeria":                              "dz",
+	"United States":                        "us",
+	"US":                                   "us",
+	"Uruguay":                              "uy",
+	"Mayotte":                              "yt",
+	"United States Minor Outlying Islands": "um",
+	"Lebanon":                              "lb",
+	"Saint Lucia":                          "lc",
+	"Laos":                                 "la",
+	"Tuvalu":                               "tv",
+	"Taiwan":                               "tw",
+	"Trinidad and Tobago":                  "tt",
+	"Turkey":                               "tr",
+	"Sri Lanka":                            "lk",
+	"Liechtenstein":                        "li",
+	"Latvia":                               "lv",
+	"Tonga":                                "to",
+	"Lithuania":                            "lt",
+	"Luxembourg":                           "lu",
+	"Liberia":                              "lr",
+	"Lesotho":                              "ls",
+	"Thailand":                             "th",
+	"French Southern Territories":          "tf",
+	"Togo":                                 "tg",
+	"Chad":                                 "td",
+	"Turks and Caicos Islands":             "tc",
+	"Libya":                                "ly",
+	"Vatican":                              "va",
+	"Saint Vincent and the Grenadines":     "vc",
+	"United Arab Emirates":                 "ae",
+	"Andorra":                              "ad",
+	"Antigua and Barbuda":                  "ag",
+	"Afghanistan":                          "af",
+	"Anguilla":                             "ai",
+	"U.S. Virgin Islands":                  "vi",
+	"Iceland":                              "is",
+	"Iran":                                 "ir",
+	"Armenia":                              "am",
+	"Albania":                              "al",
+	"Angola":                               "ao",
+	"Antarctica":                           "aq",
+	"American Samoa":                       "as",
+	"Argentina":                            "ar",
+	"Australia":                            "au",
+	"Austria":                              "at",
+	"Aruba":                                "aw",
+	"India":                                "in",
+	"Aland Islands":                        "ax",
+	"Azerbaijan":                           "az",
+	"Ireland":                              "ie",
+	"Indonesia":                            "id",
+	"Ukraine":                              "ua",
+	"Qatar":                                "qa",
+	"Mozambique":                           "mz",
+	"Wales":                                "gb-wls",
+	"Scotland":                             "gb-sct",
+}
diff --git a/startgg/startgg.go b/startgg/startgg.go
new file mode 100644
index 0000000..6220586
--- /dev/null
+++ b/startgg/startgg.go
@@ -0,0 +1,80 @@
+package startgg
+
+import (
+	"bytes"
+	_ "embed"
+	"encoding/json"
+	"fmt"
+	"io/ioutil"
+	"net/http"
+)
+
+const STARTGG_URL = "https://api.start.gg/gql/alpha"
+
+type Player struct {
+	GamerTag string
+	Prefix   string
+}
+
+type GraphQL struct {
+	Query     string   `json:"query"`
+	Variables struct{} `json:"variables"`
+}
+
+func FetchPlayers(token string, tourneySlug string) ([]Player, error) {
+	query := `
+{
+  tournament(slug: "%s") {
+    participants(query: {page: %d, perPage: 500}) {
+      nodes {
+        entrants {
+          event {
+            slug
+            name
+          }
+          team {
+            name
+          }
+        }
+        gamerTag
+        prefix
+        user {
+          location {
+            country
+          }
+        }
+      }
+    }
+  }
+}
+`
+	body, err := json.Marshal(GraphQL{
+		Query:     fmt.Sprintf(query, tourneySlug, 1),
+		Variables: struct{}{},
+	})
+	if err != nil {
+		panic(err)
+	}
+
+	req, err := http.NewRequest("POST", STARTGG_URL, bytes.NewBuffer(body))
+	if err != nil {
+		panic(err)
+	}
+	req.Header.Add("User-Agent", "GORTS/0.2")
+	req.Header.Add("Content-Type", "application/json")
+	req.Header.Add("Authorization", "Bearer "+token)
+
+	resp, err := http.DefaultClient.Do(req)
+	if err != nil {
+		return nil, fmt.Errorf("fetch players: %w", err)
+	}
+	defer resp.Body.Close()
+
+	respdata, err := ioutil.ReadAll(resp.Body)
+	fmt.Println(">>>>", string(respdata))
+
+	//var res map[string]interface{}
+	//json.NewDecoder(resp.Body).Decode(&res)
+	//fmt.Println(res["json"])
+	return nil, nil
+}