Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for Smart TV #55

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions ua.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type UserAgent struct {
Tablet bool
Desktop bool
Bot bool
TV bool
}

// Constants for browsers and operating systems for easier comparison
Expand Down Expand Up @@ -84,6 +85,10 @@ func Parse(userAgent string) UserAgent {
tokens := parse([]byte(userAgent))
ua.URL = tokens.url

// TV check
ua.TV = tokens.existsAny("SmartTV", "Smart TV", "SMART-TV", "Apple TV", "GoogleTV",
"PhilipsTV", "HbbTV")

// OS lookup
switch {
case tokens.exists(Android):
Expand Down Expand Up @@ -120,7 +125,7 @@ func Parse(userAgent string) UserAgent {
ua.OSVersion = tokens.findMacOSVersion()
ua.Desktop = true

case tokens.exists(Linux):
case tokens.existsAny(Linux, strings.ToUpper(Linux)):
ua.OS = Linux
ua.OSVersion = tokens.get(Linux)
ua.Desktop = true
Expand Down Expand Up @@ -161,7 +166,7 @@ func Parse(userAgent string) UserAgent {

case tokens.exists("Bytespider"):
ua.Name = "Bytespider"
ua.Mobile = tokens.exists("Mobile Safari")
ua.Mobile = tokens.exists(MobileSafari)
ua.Bot = true

case tokens.exists(Applebot):
Expand Down
10 changes: 10 additions & 0 deletions ua_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,12 @@ var testTable = [][]string{
//Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/600.2.5 (KHTML, like Gecko) Version/8.0.2 Safari/600.2.5 (Applebot/0.1; +http://www.apple.com/go/applebot)
//Mozilla/5.0 (Macintosh; Intel Mac OS Xt 10_11_4) AppleWebKit/537.36 (KHTML, like Gecko) QtWebEngine/5.6.0 Chrome/45.0.2454.101 Safari/537.36

{"Mozilla/5.0 (SmartHub; SMART-TV; U; Linux/SmartTV; Maple2012) AppleWebKit/534.7 (KHTML, like Gecko) SmartTV Safari/534.7", "SmartTV Safari", "534.7", "tv", ua.Linux},
{"Mozilla/5.0 (X11; U; Linux i686; en-US) AppleWebKit/533.4 (KHTML, like Gecko) Chrome/5.0.375.127 Large Screen Safari/533.4 GoogleTV/ 162671", ua.Chrome, "5.0.375.127", "tv", ua.Linux},
{"Mozilla/5.0 (Linux mips; U;HbbTV/1.1.1 (+RTSP;DMM;Dreambox;0.1a;1.0;) CE-HTML/1.0; en) AppleWebKit/535.19 no/Volksbox QtWebkit/2.2", "HbbTV", "1.1.1", "tv", ua.Linux},
{"Mozilla/5.0 (Linux; NetCast; U) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.79 Safari/537.36 SmartTV/10.0 Colt/2.0", "SmartTV", "10.0", "tv", ua.Linux},
{"Mozilla/5.0 (SMART-TV; LINUX; Tizen 5.0) AppleWebKit/537.36 (KHTML, like Gecko) Version/5.0 TV Safari/537.36", "TV Safari", "537.36", "tv", ua.Linux},
{"Mozilla/5.0 (SMART-TV; Linux; Tizen 4.0) AppleWebKit/537.36 (KHTML, like Gecko) SamsungBrowser/2.1 Chrome/56.0.2924.0 TV Safari/537.36", ua.SamsungBrowser, "2.1", "tv", ua.Android},
}

func TestParse(t *testing.T) {
Expand Down Expand Up @@ -204,6 +210,10 @@ func TestParse(t *testing.T) {
t.Error("\n", ua.String, "should be bot")
fmt.Printf("%+v", ua)
}
if test[3] == "tv" && !ua.TV {
t.Error("\n", ua.String, "should be tv")
fmt.Printf("%+v", ua)
}
}

if len(test) > 4 && test[4] != ua.OS {
Expand Down