Documentation ¶
Index ¶
- type URL
- func (u URL) ExtractArgs(url string) (args []any)
- func (u URL) Fill(args ...any) string
- func (u URL) GetRequest(args ...any) (url string, req *http.Request, err error)
- func (u URL) JSON(req *http.Request, args ...any) (jsonBody map[string]any, resp *http.Response, err error)
- func (u URL) Match(url string) bool
- func (u URL) Regex() *regexp.Regexp
- func (u URL) Request(method string, body io.Reader, args ...any) (url string, req *http.Request, err error)
- func (u URL) RetryJSON(req *http.Request, maxTries int, minDelay time.Duration, ...) error
- func (u URL) RetrySoup(req *http.Request, maxTries int, minDelay time.Duration, ...) error
- func (u URL) Soup(req *http.Request, args ...any) (doc *soup.Root, resp *http.Response, err error)
- func (u URL) Standardise(url string) string
- func (u URL) String() string
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type URL ¶
type URL string
URL represents a page on Steam. It is usually a format string has string interpolation applied to it before fetching. The protocol should be given as a string verb at the beginning of the URL.
func (URL) ExtractArgs ¶
ExtractArgs extracts the necessary arguments from the given URL to run the ScrapeURL.Soup, URL.JSON, and URL.Fill methods. This is useful when taking a URL matched by URL.Match and fetching the soup for that matched URL.
Example ¶
const ( SteamAppPage URL = "%s://store.steampowered.com/app/%d" ItchIOGamePage URL = "%s://%s.itch.io/%s" ) fmt.Println(SteamAppPage.ExtractArgs("http://store.steampowered.com/app/477160")) fmt.Println(SteamAppPage.ExtractArgs("https://store.steampowered.com/app/477160/Human_Fall_Flat/")) fmt.Println(ItchIOGamePage.ExtractArgs("https://hempuli.itch.io/baba-files-taxes")) fmt.Println(ItchIOGamePage.ExtractArgs("https://sokpop.itch.io/ballspell"))
Output: [477160] [477160] [hempuli baba-files-taxes] [sokpop ballspell]
func (URL) Fill ¶
Fill will apply string interpolation to the URL. The protocol does not need to be included as "https" is always prepended to the args.
func (URL) GetRequest ¶
GetRequest creates a new http.MethodGet http.Request for the given URL with the given arguments.
func (URL) JSON ¶
func (u URL) JSON(req *http.Request, args ...any) (jsonBody map[string]any, resp *http.Response, err error)
JSON makes a request to the URL and parses the response to JSON. As well as returning the parsed JSON as a map, it also returns the response to the original HTTP request made to the given URL. If a non-nil http.Request is provided then it will be used to fetch the JSON resource, otherwise default http.MethodGet http.Request will be constructed instead.
Example ¶
const SteamAppReviews URL = "%s://store.steampowered.com/appreviews/%d?json=1&cursor=%s&language=%s&day_range=9223372036854775807&num_per_page=%d&review_type=all&purchase_type=%s&filter=%s&start_date=%d&end_date=%d&date_range_type=%s" args := []any{477160, "*", "all", 20, "all", "all", -1, -1, "all"} fmt.Printf("Getting review stats for 477160 from %s:\n", SteamAppReviews.Fill(args...)) if j, _, err := SteamAppReviews.JSON(nil, args...); err != nil { fmt.Printf("Could not get reviews for %s, because %s", SteamAppReviews.Fill(args...), err.Error()) } else { var i int keys := make([]string, len(j["query_summary"].(map[string]any))) for key := range j["query_summary"].(map[string]any) { keys[i] = key i++ } sort.Strings(keys) fmt.Println(keys) }
Output: Getting review stats for 477160 from https://store.steampowered.com/appreviews/477160?json=1&cursor=*&language=all&day_range=9223372036854775807&num_per_page=20&review_type=all&purchase_type=all&filter=all&start_date=-1&end_date=-1&date_range_type=all: [num_reviews review_score review_score_desc total_negative total_positive total_reviews]
func (URL) Match ¶
Match the given URL with a URL to check if they are the same format.
Example ¶
const ( SteamAppPage URL = "%s://store.steampowered.com/app/%d" ItchIOGamePage URL = "%s://%s.itch.io/%s" ) fmt.Println(SteamAppPage.Match("http://store.steampowered.com/app/477160")) fmt.Println(SteamAppPage.Match("http://store.steampowered.com/app/477160/Human_Fall_Flat/")) fmt.Println(SteamAppPage.Match("https://store.steampowered.com/app/477160")) fmt.Println(SteamAppPage.Match("https://store.steampowered.com/app/Human_Fall_Flat/")) fmt.Println(ItchIOGamePage.Match("https://hempuli.itch.io/baba-files-taxes")) fmt.Println(ItchIOGamePage.Match("https://sokpop.itch.io/ballspell"))
Output: true true true false true true
func (URL) Regex ¶
Regex converts the URL to a regex by replacing the string interpolation verbs with their regex character set counterparts.
Example ¶
const ( SteamAppPage URL = "%s://store.steampowered.com/app/%d" ItchIOGamePage URL = "%s://%s.itch.io/%s" ) fmt.Println(SteamAppPage.Regex()) fmt.Println(ItchIOGamePage.Regex())
Output: https?://store.steampowered.com/app/(\d+) https?://([a-zA-Z0-9-._~]+).itch.io/([a-zA-Z0-9-._~]+)
func (URL) Request ¶
func (u URL) Request(method string, body io.Reader, args ...any) (url string, req *http.Request, err error)
Request creates a new http.Request for the given URL with the given arguments, method, and io.Reader.
func (URL) RetryJSON ¶
func (u URL) RetryJSON(req *http.Request, maxTries int, minDelay time.Duration, try func(jsonBody map[string]any, resp *http.Response) error, args ...any) error
RetryJSON will run JSON with the given args and try the given function. If the function returns an error then the function will be retried up to a total of the given number of maxTries. If minDelay is given, and is not 0, then before the function is retried it will sleep for (maxTries + 1 - currentTries) * minDelay. If a non-nil http.Request is provided then it will be used to fetch the JSON resource, otherwise default http.MethodGet http.Request will be constructed instead.
func (URL) RetrySoup ¶
func (u URL) RetrySoup(req *http.Request, maxTries int, minDelay time.Duration, try func(doc *soup.Root, resp *http.Response) error, args ...any) error
RetrySoup will run Soup with the given args and try the given function. If the function returns an error then the function will be retried up to a total of the given number of maxTries. If minDelay is given, and is not 0, then before the function is retried it will sleep for (maxTries + 1 - currentTries) * minDelay. If a non-nil http.Request is provided then it will be used to fetch the page for the Soup, otherwise a default http.MethodGet http.Request will be constructed instead.
func (URL) Soup ¶
Soup fetches the URL using the default HTTP client, then parses the returned HTML page into a soup.Root. It also returns the http.Response object returned by the http.Get request. A http.Request can be provided, but if nil is provided then a default http.MethodGet http.Request will be constructed instead.
Example ¶
const SteamAppPage URL = "%s://store.steampowered.com/app/%d" fmt.Printf("Getting name of app 477160 from %s:\n", SteamAppPage.Fill(477160)) if soup, _, err := SteamAppPage.Soup(nil, 477160); err != nil { fmt.Printf("Could not get soup for %s, because %s", SteamAppPage.Fill(477160), err.Error()) } else { fmt.Println(soup.Find("div", "id", "appHubAppName").Text()) }
Output: Getting name of app 477160 from https://store.steampowered.com/app/477160: Human: Fall Flat
func (URL) Standardise ¶
Standardise will first extract the args from the given URL then Fill the referred to URL with those args.