Skip to content

Commit

Permalink
IsMobile
Browse files Browse the repository at this point in the history
  • Loading branch information
alaingilbert committed Apr 1, 2020
1 parent d778159 commit 08918a3
Show file tree
Hide file tree
Showing 4 changed files with 1,363 additions and 0 deletions.
13 changes: 13 additions & 0 deletions extractor_v71.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package ogame

import (
"bytes"
"fmt"
"strconv"
"time"

Expand Down Expand Up @@ -160,3 +161,15 @@ func (e ExtractorV71) ExtractBuffActivation(pageHTML []byte) (string, []Item, er
func (e ExtractorV71) ExtractBuffActivationFromDoc(doc *goquery.Document) (string, []Item, error) {
return extractBuffActivationFromDocV71(doc)
}

// ExtractIsMobile ...
func (e ExtractorV71) ExtractIsMobile(pageHTML []byte) bool {
doc, _ := goquery.NewDocumentFromReader(bytes.NewReader(pageHTML))
fmt.Println(pageHTML)
return e.ExtractIsMobileFromDoc(doc)
}

// ExtractIsMobileFromDoc ...
func (e ExtractorV71) ExtractIsMobileFromDoc(doc *goquery.Document) bool {
return extractIsMobileFromDocV71(doc)
}
13 changes: 13 additions & 0 deletions extracts_v71.go
Original file line number Diff line number Diff line change
Expand Up @@ -822,3 +822,16 @@ func extractBuffActivationFromDocV71(doc *goquery.Document) (token string, items
}
return
}

func extractIsMobileFromDocV71(doc *goquery.Document) bool {
r := regexp.MustCompile(`var isMobile = (true|false);`)
scripts := doc.Find("script")
for i := 0; i < scripts.Size(); i++ {
scriptText := scripts.Eq(i).Text()
m := r.FindStringSubmatch(scriptText)
if len(m) == 2 {
return m[1] == "true"
}
}
return false
}
10 changes: 10 additions & 0 deletions ogame_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2887,3 +2887,13 @@ func TestExtractOGameSession(t *testing.T) {
session = NewExtractorV6().ExtractOGameSession(pageHTMLBytes)
assert.Equal(t, "c1626ce8228ac5986e3808a7d42d4afc764c1b68", session)
}

func TestExtractIsMobile(t *testing.T) {
pageHTMLBytes, _ := ioutil.ReadFile("samples/v7.1/en/movement.html")
isMobile := NewExtractorV71().ExtractIsMobile(pageHTMLBytes)
assert.False(t, isMobile)

pageHTMLBytes, _ = ioutil.ReadFile("samples/v7.2/en/movement_mobile.html")
isMobile = NewExtractorV71().ExtractIsMobile(pageHTMLBytes)
assert.True(t, isMobile)
}
Loading

0 comments on commit 08918a3

Please sign in to comment.