-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from WURFL/feature/examples
Feature/examples
- Loading branch information
Showing
5 changed files
with
271 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
wurfl "github.com/WURFL/golang-wurfl" | ||
) | ||
|
||
func main() { | ||
|
||
// Replace this with your own WURFL Snapshot URL | ||
wurflUpdaterURL := "https://data.scientiamobile.com/xxxxx/wurfl.zip" | ||
|
||
fmt.Println("Downloading WURFL file ...") | ||
|
||
if err := wurfl.Download(wurflUpdaterURL, "."); err != nil { | ||
fmt.Printf("Error downloading WURFL file: %v\n", err) | ||
os.Exit(1) | ||
} | ||
|
||
fmt.Println("WURFL file downloaded successfully") | ||
|
||
wengine, err := wurfl.Create("./wurfl.zip", nil, nil, -1, wurfl.WurflCacheProviderLru, "100000") | ||
|
||
if err != nil { | ||
fmt.Printf("Error creating WURFL engine: %v", err) | ||
os.Exit(1) | ||
} | ||
defer wengine.Destroy() | ||
|
||
fmt.Println("Engine loaded, version : ", wengine.GetAPIVersion(), "wurfl info ", wengine.GetInfo()) | ||
|
||
// start the updater : will keep the wurfl.zip file updated to the last version | ||
// if err := wengine.SetUpdaterDataURL(wurflUpdaterURL); err != nil { | ||
// fmt.Printf("Error setting updater data URL: %v\n", err) | ||
// } | ||
// wengine.SetUpdaterDataFrequency(wurfl.WurflUpdaterFrequencyDaily) | ||
// wengine.UpdaterStart() | ||
|
||
c := wengine.GetAllCaps() | ||
fmt.Println("Capabilities available = ", c) | ||
|
||
ua := "Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Mobile Safari/537.36" | ||
|
||
ihmap := make(map[string]string) | ||
ihmap["User-Agent"] = ua | ||
ihmap["Accept-Encoding"] = "gzip, deflate, br, zstd" | ||
ihmap["Sec-CH-UA-Platform"] = "Android" | ||
ihmap["Sec-CH-UA-Platform-Version"] = `"13.0.0"` | ||
ihmap["Sec-CH-UA"] = `"Not/A)Brand";v="8", "Chromium";v="126", "Google Chrome";v="126"` | ||
ihmap["Sec-CH-UA-Mobile"] = "?1" | ||
ihmap["Sec-Ch-Ua-Model"] = `"SM-S135DL"` | ||
ihmap["Sec-Ch-Ua-Full-Version"] = `"126.0.6478.71"` | ||
ihmap["Sec-CH-UA-Full-Version-List"] = `"Not/A)Brand";v="8.0.0.0", "Chromium";v="126.0.6478.71", "Google Chrome";v="126.0.6478.71"` | ||
|
||
device, err := wengine.LookupWithImportantHeaderMap(ihmap) | ||
if err != nil { | ||
fmt.Printf("Error in lookup: %v", err) | ||
os.Exit(1) | ||
} | ||
defer device.Destroy() | ||
|
||
// obtain the wurfl_id for this device, a unique device identifier | ||
deviceid, err := device.GetDeviceID() | ||
if err != nil { | ||
fmt.Printf("Error in GetDeviceID: %v", err) | ||
} | ||
fmt.Println(deviceid) | ||
|
||
// static capabilities are stored in the wurfl.zip | ||
cap, _ := device.GetStaticCap("model_name") | ||
fmt.Printf("model_name = %s\n", cap) | ||
|
||
// virtual capabilities are computed on the fly | ||
vcap, _ := device.GetVirtualCap("complete_device_name") | ||
fmt.Printf("complete_device_name = %s\n", vcap) | ||
|
||
caps := []string{ | ||
"marketing_name", | ||
"brand_name", | ||
"device_os", | ||
} | ||
// get a list of static caps, returns a map | ||
caplist, err := device.GetStaticCaps(caps) | ||
if err != nil { | ||
fmt.Printf("Error in GetDeviceID: %v", err) | ||
} | ||
fmt.Printf("marketing_name = %s\n", caplist["marketing_name"]) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"net/http" | ||
"os" | ||
|
||
wurfl "github.com/WURFL/golang-wurfl" | ||
) | ||
|
||
func main() { | ||
|
||
// Replace this with your own WURFL Snapshot URL | ||
wurflUpdaterURL := "https://data.scientiamobile.com/xxxxx/wurfl.zip" | ||
|
||
fmt.Println("Downloading WURFL file ...") | ||
|
||
if err := wurfl.Download(wurflUpdaterURL, "."); err != nil { | ||
fmt.Printf("Error downloading WURFL file: %v\n", err) | ||
os.Exit(1) | ||
} | ||
|
||
fmt.Println("WURFL file downloaded successfully") | ||
|
||
wengine, err := wurfl.Create("./wurfl.zip", nil, nil, -1, wurfl.WurflCacheProviderLru, "100000") | ||
|
||
if err != nil { | ||
fmt.Printf("Error creating WURFL engine: %v\n", err) | ||
os.Exit(1) | ||
} | ||
|
||
fmt.Println("Engine loaded, version : ", wengine.GetAPIVersion(), "wurfl info ", wengine.GetInfo()) | ||
|
||
// start the updater : will keep the wurfl.zip file updated to the last version | ||
|
||
if err := wengine.SetUpdaterDataURL(wurflUpdaterURL); err != nil { | ||
fmt.Printf("Error setting updater data URL: %v\n", err) | ||
} | ||
wengine.SetUpdaterDataFrequency(wurfl.WurflUpdaterFrequencyDaily) | ||
wengine.UpdaterStart() | ||
|
||
// start server and process reqs | ||
|
||
fmt.Printf("Starting server on port 8080\n") | ||
|
||
// I need to inject the wurfl enginee into the handler | ||
// so that it can be used to lookup the device | ||
// for each request | ||
|
||
http.HandleFunc("/detect", makeHandler(wengine)) | ||
|
||
log.Fatal(http.ListenAndServe(":8080", nil)) | ||
|
||
wengine.Destroy() | ||
} | ||
|
||
// makeHandler creates a http.HandlerFunc injecting the wurfl engine | ||
func makeHandler(wengine *wurfl.Wurfl) http.HandlerFunc { | ||
return func(w http.ResponseWriter, r *http.Request) { | ||
|
||
device, err := wengine.LookupRequest(r) | ||
if err != nil { | ||
fmt.Fprintf(w, "Error in lookup: %v", err) | ||
} | ||
|
||
ado, _ := device.GetVirtualCap("advertised_device_os") | ||
isbot, _ := device.GetVirtualCap("is_robot") | ||
ab, _ := device.GetVirtualCap("advertised_browser") | ||
device.Destroy() | ||
|
||
fmt.Fprintf(w, "is_robot = %s\n", isbot) | ||
fmt.Fprintf(w, "advertised_device_os = %s\n", ado) | ||
fmt.Fprintf(w, "advertised_browser = %s\n", ab) | ||
// close response write and send out the response | ||
w.WriteHeader(http.StatusOK) | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
wurfl "github.com/WURFL/golang-wurfl" | ||
) | ||
|
||
func main() { | ||
|
||
// Replace this with your own WURFL Snapshot URL | ||
wurflUpdaterURL := "https://data.scientiamobile.com/xxxxx/wurfl.zip" | ||
|
||
fmt.Println("Downloading WURFL file ...") | ||
|
||
if err := wurfl.Download(wurflUpdaterURL, "."); err != nil { | ||
fmt.Printf("Error downloading WURFL file: %v\n", err) | ||
os.Exit(1) | ||
} | ||
|
||
fmt.Println("WURFL file downloaded successfully") | ||
|
||
wengine, err := wurfl.Create("./wurfl.zip", nil, nil, -1, wurfl.WurflCacheProviderLru, "100000") | ||
if err != nil { | ||
fmt.Printf("Error creating WURFL engine: %v\n", err) | ||
os.Exit(1) | ||
} | ||
defer wengine.Destroy() | ||
|
||
fmt.Println("Engine loaded, version : ", wengine.GetAPIVersion(), "wurfl info ", wengine.GetInfo()) | ||
|
||
// start the updater : will keep the wurfl.zip file updated to the last version | ||
// if err := wengine.SetUpdaterDataURL(wurflUpdaterURL); err != nil { | ||
// fmt.Printf("Error setting updater data URL: %v\n", err) | ||
// } | ||
// wengine.SetUpdaterDataFrequency(wurfl.WurflUpdaterFrequencyDaily) | ||
// wengine.UpdaterStart() | ||
|
||
c := wengine.GetAllCaps() | ||
fmt.Println("Capabilities available = ", c) | ||
|
||
ua := "Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Mobile Safari/537.36" | ||
|
||
device, err := wengine.LookupUserAgent(ua) | ||
if err != nil { | ||
fmt.Printf("Error in lookup: %v", err) | ||
os.Exit(1) | ||
} | ||
|
||
// obtain the wurfl_id for this device, a unique device identifier | ||
deviceid, err := device.GetDeviceID() | ||
if err != nil { | ||
fmt.Printf("Error in GetDeviceID: %v", err) | ||
os.Exit(1) | ||
} | ||
fmt.Println(deviceid) | ||
|
||
// static capabilities are stored in the wurfl.zip | ||
cap, _ := device.GetStaticCap("model_name") | ||
fmt.Printf("model_name = %s\n", cap) | ||
|
||
// virtual capabilities are computed on the fly | ||
vcap, _ := device.GetVirtualCap("is_android") | ||
fmt.Printf("is_android = %s\n", vcap) | ||
|
||
if wengine.IsUserAgentFrozen(ua) { | ||
fmt.Printf("UA %s is frozen. Sec-Ch-Ua headers are necessary for correct device identification.\n", ua) | ||
} | ||
|
||
device.Destroy() | ||
} |
Oops, something went wrong.