Skip to content

Commit

Permalink
Adding maintenance mode detection (#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
tobiasehlert committed Jan 7, 2022
1 parent 813b57e commit c02fc2e
Show file tree
Hide file tree
Showing 13 changed files with 109 additions and 23 deletions.
9 changes: 8 additions & 1 deletion src/TibiaCharactersCharacterV3.go
Expand Up @@ -2,6 +2,7 @@ package main

import (
"log"
"net/http"
"regexp"
"strings"

Expand Down Expand Up @@ -151,7 +152,13 @@ func TibiaCharactersCharacterV3(c *gin.Context) {

// Getting data with TibiadataHTMLDataCollectorV3
TibiadataRequest.URL = "https://www.tibia.com/community/?subtopic=characters&name=" + TibiadataQueryEscapeStringV3(character)
BoxContentHTML := TibiadataHTMLDataCollectorV3(TibiadataRequest)
BoxContentHTML, err := TibiadataHTMLDataCollectorV3(TibiadataRequest)

// return error (e.g.1 for maintenance mode)
if err != nil {
TibiaDataAPIHandleOtherResponse(c, http.StatusServiceUnavailable, "TibiaCharactersCharacterV3", gin.H{"error": err.Error()})
return
}

// Loading HTML data into ReaderHTML for goquery with NewReader
ReaderHTML, err := goquery.NewDocumentFromReader(strings.NewReader(BoxContentHTML))
Expand Down
9 changes: 8 additions & 1 deletion src/TibiaCreaturesCreatureV3.go
Expand Up @@ -2,6 +2,7 @@ package main

import (
"log"
"net/http"
"regexp"
"strings"

Expand Down Expand Up @@ -50,7 +51,13 @@ func TibiaCreaturesCreatureV3(c *gin.Context) {

// Getting data with TibiadataHTMLDataCollectorV3
TibiadataRequest.URL = "https://www.tibia.com/library/?subtopic=creatures&race=" + TibiadataQueryEscapeStringV3(race)
BoxContentHTML := TibiadataHTMLDataCollectorV3(TibiadataRequest)
BoxContentHTML, err := TibiadataHTMLDataCollectorV3(TibiadataRequest)

// return error (e.g.1 for maintenance mode)
if err != nil {
TibiaDataAPIHandleOtherResponse(c, http.StatusServiceUnavailable, "TibiaCreaturesCreatureV3", gin.H{"error": err.Error()})
return
}

// Loading HTML data into ReaderHTML for goquery with NewReader
ReaderHTML, err := goquery.NewDocumentFromReader(strings.NewReader(BoxContentHTML))
Expand Down
9 changes: 8 additions & 1 deletion src/TibiaCreaturesOverviewV3.go
Expand Up @@ -2,6 +2,7 @@ package main

import (
"log"
"net/http"
"regexp"
"strings"

Expand Down Expand Up @@ -35,7 +36,13 @@ func TibiaCreaturesOverviewV3(c *gin.Context) {

// Getting data with TibiadataHTMLDataCollectorV3
TibiadataRequest.URL = "https://www.tibia.com/library/?subtopic=creatures"
BoxContentHTML := TibiadataHTMLDataCollectorV3(TibiadataRequest)
BoxContentHTML, err := TibiadataHTMLDataCollectorV3(TibiadataRequest)

// return error (e.g.1 for maintenance mode)
if err != nil {
TibiaDataAPIHandleOtherResponse(c, http.StatusServiceUnavailable, "TibiaCreaturesOverviewV3", gin.H{"error": err.Error()})
return
}

// Loading HTML data into ReaderHTML for goquery with NewReader
ReaderHTML, err := goquery.NewDocumentFromReader(strings.NewReader(BoxContentHTML))
Expand Down
9 changes: 8 additions & 1 deletion src/TibiaFansitesV3.go
Expand Up @@ -2,6 +2,7 @@ package main

import (
"log"
"net/http"
"regexp"
"strings"

Expand Down Expand Up @@ -59,7 +60,13 @@ func TibiaFansitesV3(c *gin.Context) {

// Getting data with TibiadataHTMLDataCollectorV3
TibiadataRequest.URL = "https://www.tibia.com/community/?subtopic=fansites"
BoxContentHTML := TibiadataHTMLDataCollectorV3(TibiadataRequest)
BoxContentHTML, err := TibiadataHTMLDataCollectorV3(TibiadataRequest)

// return error (e.g.1 for maintenance mode)
if err != nil {
TibiaDataAPIHandleOtherResponse(c, http.StatusServiceUnavailable, "TibiaFansitesV3", gin.H{"error": err.Error()})
return
}

// Loading HTML data into ReaderHTML for goquery with NewReader
ReaderHTML, err := goquery.NewDocumentFromReader(strings.NewReader(BoxContentHTML))
Expand Down
13 changes: 12 additions & 1 deletion src/TibiaGuildsGuildV3.go
Expand Up @@ -2,6 +2,7 @@ package main

import (
"log"
"net/http"
"regexp"
"strings"

Expand Down Expand Up @@ -88,7 +89,13 @@ func TibiaGuildsGuildV3(c *gin.Context) {

// Getting data with TibiadataHTMLDataCollectorV3
TibiadataRequest.URL = "https://www.tibia.com/community/?subtopic=guilds&page=view&GuildName=" + TibiadataQueryEscapeStringV3(guild)
BoxContentHTML := TibiadataHTMLDataCollectorV3(TibiadataRequest)
BoxContentHTML, err := TibiadataHTMLDataCollectorV3(TibiadataRequest)

// return error (e.g.1 for maintenance mode)
if err != nil {
TibiaDataAPIHandleOtherResponse(c, http.StatusServiceUnavailable, "TibiaGuildsGuildV3", gin.H{"error": err.Error()})
return
}

// Loading HTML data into ReaderHTML for goquery with NewReader
ReaderHTML, err := goquery.NewDocumentFromReader(strings.NewReader(BoxContentHTML))
Expand All @@ -114,13 +121,17 @@ func TibiaGuildsGuildV3(c *gin.Context) {
var GuildDescriptionFinished bool
for _, line := range strings.Split(strings.TrimSuffix(InnerTableContainerTMPB, "\n"), "\n") {

log.Println(line)

// Guild information
if !GuildDescriptionFinished {
// First line is the description..
GuildDescription += strings.ReplaceAll(line+"\n", "<br/><br/>\n", "")
log.Println(GuildDescription)

// Abort loop and continue wiht next section
if strings.Contains(line, "<br/><br/>") {
guild = GuildDescription
GuildDescription = TibiaDataSanitizeEscapedString(GuildDescription)
GuildDescriptionFinished = true
}
Expand Down
9 changes: 8 additions & 1 deletion src/TibiaGuildsOverviewV3.go
Expand Up @@ -2,6 +2,7 @@ package main

import (
"log"
"net/http"
"strings"

"github.com/PuerkitoBio/goquery"
Expand Down Expand Up @@ -44,7 +45,13 @@ func TibiaGuildsOverviewV3(c *gin.Context) {

// Getting data with TibiadataHTMLDataCollectorV3
TibiadataRequest.URL = "https://www.tibia.com/community/?subtopic=guilds&world=" + TibiadataQueryEscapeStringV3(world)
BoxContentHTML := TibiadataHTMLDataCollectorV3(TibiadataRequest)
BoxContentHTML, err := TibiadataHTMLDataCollectorV3(TibiadataRequest)

// return error (e.g.1 for maintenance mode)
if err != nil {
TibiaDataAPIHandleOtherResponse(c, http.StatusServiceUnavailable, "TibiaGuildsOverviewV3", gin.H{"error": err.Error()})
return
}

// Loading HTML data into ReaderHTML for goquery with NewReader
ReaderHTML, err := goquery.NewDocumentFromReader(strings.NewReader(BoxContentHTML))
Expand Down
9 changes: 8 additions & 1 deletion src/TibiaHighscoresV3.go
Expand Up @@ -2,6 +2,7 @@ package main

import (
"log"
"net/http"
"regexp"
"strings"

Expand Down Expand Up @@ -111,7 +112,13 @@ func TibiaHighscoresV3(c *gin.Context) {

// Getting data with TibiadataHTMLDataCollectorV3
TibiadataRequest.URL = "https://www.tibia.com/community/?subtopic=highscores&world=" + TibiadataQueryEscapeStringV3(world) + "&category=" + TibiadataQueryEscapeStringV3(categoryid) + "&profession=" + TibiadataQueryEscapeStringV3(vocationid) + "&currentpage=400000000000000"
BoxContentHTML := TibiadataHTMLDataCollectorV3(TibiadataRequest)
BoxContentHTML, err := TibiadataHTMLDataCollectorV3(TibiadataRequest)

// return error (e.g.1 for maintenance mode)
if err != nil {
TibiaDataAPIHandleOtherResponse(c, http.StatusServiceUnavailable, "TibiaHighscoresV3", gin.H{"error": err.Error()})
return
}

// Loading HTML data into ReaderHTML for goquery with NewReader
ReaderHTML, err := goquery.NewDocumentFromReader(strings.NewReader(BoxContentHTML))
Expand Down
9 changes: 8 additions & 1 deletion src/TibiaKillstatisticsV3.go
Expand Up @@ -2,6 +2,7 @@ package main

import (
"log"
"net/http"
"regexp"
"strings"

Expand Down Expand Up @@ -51,7 +52,13 @@ func TibiaKillstatisticsV3(c *gin.Context) {

// Getting data with TibiadataHTMLDataCollectorV3
TibiadataRequest.URL = "https://www.tibia.com/community/?subtopic=killstatistics&world=" + TibiadataQueryEscapeStringV3(world)
BoxContentHTML := TibiadataHTMLDataCollectorV3(TibiadataRequest)
BoxContentHTML, err := TibiadataHTMLDataCollectorV3(TibiadataRequest)

// return error (e.g.1 for maintenance mode)
if err != nil {
TibiaDataAPIHandleOtherResponse(c, http.StatusServiceUnavailable, "TibiaKillstatisticsV3", gin.H{"error": err.Error()})
return
}

// Loading HTML data into ReaderHTML for goquery with NewReader
ReaderHTML, err := goquery.NewDocumentFromReader(strings.NewReader(BoxContentHTML))
Expand Down
9 changes: 8 additions & 1 deletion src/TibiaSpellsOverviewV3.go
Expand Up @@ -2,6 +2,7 @@ package main

import (
"log"
"net/http"
"regexp"
"strings"

Expand Down Expand Up @@ -60,7 +61,13 @@ func TibiaSpellsOverviewV3(c *gin.Context) {

// Getting data with TibiadataHTMLDataCollectorV3
TibiadataRequest.URL = "https://www.tibia.com/library/?subtopic=spells&vocation=" + TibiadataQueryEscapeStringV3(vocationName)
BoxContentHTML := TibiadataHTMLDataCollectorV3(TibiadataRequest)
BoxContentHTML, err := TibiadataHTMLDataCollectorV3(TibiadataRequest)

// return error (e.g.1 for maintenance mode)
if err != nil {
TibiaDataAPIHandleOtherResponse(c, http.StatusServiceUnavailable, "TibiaSpellsOverviewV3", gin.H{"error": err.Error()})
return
}

// Loading HTML data into ReaderHTML for goquery with NewReader
ReaderHTML, err := goquery.NewDocumentFromReader(strings.NewReader(BoxContentHTML))
Expand Down
9 changes: 8 additions & 1 deletion src/TibiaSpellsSpellV3.go
Expand Up @@ -2,6 +2,7 @@ package main

import (
"log"
"net/http"
"regexp"
"strings"

Expand Down Expand Up @@ -76,7 +77,13 @@ func TibiaSpellsSpellV3(c *gin.Context) {

// Getting data with TibiadataHTMLDataCollectorV3
TibiadataRequest.URL = "https://www.tibia.com/library/?subtopic=spells&spell=" + TibiadataQueryEscapeStringV3(spell)
BoxContentHTML := TibiadataHTMLDataCollectorV3(TibiadataRequest)
BoxContentHTML, err := TibiadataHTMLDataCollectorV3(TibiadataRequest)

// return error (e.g.1 for maintenance mode)
if err != nil {
TibiaDataAPIHandleOtherResponse(c, http.StatusServiceUnavailable, "TibiaSpellsSpellV3", gin.H{"error": err.Error()})
return
}

// Loading HTML data into ReaderHTML for goquery with NewReader
ReaderHTML, err := goquery.NewDocumentFromReader(strings.NewReader(BoxContentHTML))
Expand Down
9 changes: 8 additions & 1 deletion src/TibiaWorldsOverviewV3.go
Expand Up @@ -2,6 +2,7 @@ package main

import (
"log"
"net/http"
"regexp"
"strings"

Expand Down Expand Up @@ -45,7 +46,13 @@ func TibiaWorldsOverviewV3(c *gin.Context) {

// Getting data with TibiadataHTMLDataCollectorV3
TibiadataRequest.URL = "https://www.tibia.com/community/?subtopic=worlds"
BoxContentHTML := TibiadataHTMLDataCollectorV3(TibiadataRequest)
BoxContentHTML, err := TibiadataHTMLDataCollectorV3(TibiadataRequest)

// return error (e.g.1 for maintenance mode)
if err != nil {
TibiaDataAPIHandleOtherResponse(c, http.StatusServiceUnavailable, "TibiaWorldsOverviewV3", gin.H{"error": err.Error()})
return
}

// Loading HTML data into ReaderHTML for goquery with NewReader
ReaderHTML, err := goquery.NewDocumentFromReader(strings.NewReader(BoxContentHTML))
Expand Down
9 changes: 8 additions & 1 deletion src/TibiaWorldsWorldV3.go
Expand Up @@ -2,6 +2,7 @@ package main

import (
"log"
"net/http"
"regexp"
"strings"

Expand Down Expand Up @@ -59,7 +60,13 @@ func TibiaWorldsWorldV3(c *gin.Context) {

// Getting data with TibiadataHTMLDataCollectorV3
TibiadataRequest.URL = "https://www.tibia.com/community/?subtopic=worlds&world=" + TibiadataQueryEscapeStringV3(world)
BoxContentHTML := TibiadataHTMLDataCollectorV3(TibiadataRequest)
BoxContentHTML, err := TibiadataHTMLDataCollectorV3(TibiadataRequest)

// return error (e.g.1 for maintenance mode)
if err != nil {
TibiaDataAPIHandleOtherResponse(c, http.StatusServiceUnavailable, "TibiaWorldsWorldV3", gin.H{"error": err.Error()})
return
}

// Loading HTML data into ReaderHTML for goquery with NewReader
ReaderHTML, err := goquery.NewDocumentFromReader(strings.NewReader(BoxContentHTML))
Expand Down
20 changes: 9 additions & 11 deletions src/webserver.go
Expand Up @@ -3,6 +3,7 @@ package main
import (
"bytes"
"encoding/json"
"errors"
"html"
"io"
"log"
Expand Down Expand Up @@ -194,6 +195,7 @@ func TibiaDataAPIHandleErrorResponse(c *gin.Context, s1 string, s2 string, s3 st
// return error response
c.JSON(http.StatusOK, gin.H{"error": s2})
}
*/

// TibiaDataAPIHandleOtherResponse func - handling of responses..
func TibiaDataAPIHandleOtherResponse(c *gin.Context, httpCode int, s string, j interface{}) {
Expand All @@ -204,7 +206,6 @@ func TibiaDataAPIHandleOtherResponse(c *gin.Context, httpCode int, s string, j i
// return successful response (with specific status code)
c.JSON(httpCode, j)
}
*/

// TibiaDataAPIHandleSuccessResponse func - handling of responses..
func TibiaDataAPIHandleSuccessResponse(c *gin.Context, s string, j interface{}) {
Expand Down Expand Up @@ -249,7 +250,7 @@ func TibiadataUserAgentGenerator(version int) string {
}

// TibiadataHTMLDataCollectorV3 func
func TibiadataHTMLDataCollectorV3(TibiadataRequest TibiadataRequestStruct) string {
func TibiadataHTMLDataCollectorV3(TibiadataRequest TibiadataRequestStruct) (string, error) {

// Setting up resty client
client := resty.New()
Expand Down Expand Up @@ -301,16 +302,13 @@ func TibiadataHTMLDataCollectorV3(TibiadataRequest TibiadataRequestStruct) strin
}

if err != nil {
log.Printf("[error] TibiadataHTMLDataCollectorV3 (URL: %s) in resp1: %s", TibiadataRequest.URL, err)
}

// Checking if status is something else than 200
if res.StatusCode() != 200 {
log.Printf("[warni] TibiadataHTMLDataCollectorV3 (URL: %s) status code: %s", TibiadataRequest.URL, res.Status())
log.Printf("[error] TibiadataHTMLDataCollectorV3 (Status: %s, URL: %s) in resp1: %s", res.Status(), TibiadataRequest.URL, err)

// Check if page is in maintenance mode
if res.StatusCode() == 302 {
log.Printf("[info] TibiadataHTMLDataCollectorV3 (URL: %s): Page tibia.com returns 302, probably maintenance mode enabled?", TibiadataRequest.URL)
location, _ := res.RawResponse.Location()
if location.Host == "maintenance.tibia.com" {
log.Println("[info] TibiadataHTMLDataCollectorV3: Maintenance mode detected!")
return "", errors.New("tibia.com is in maintenance mode")
}
}

Expand All @@ -333,7 +331,7 @@ func TibiadataHTMLDataCollectorV3(TibiadataRequest TibiadataRequestStruct) strin
}

// Return of extracted html to functions..
return data
return data, nil
}

// TibiadataRequestTraceLogger func - prints out trace information to log
Expand Down

0 comments on commit c02fc2e

Please sign in to comment.