Created
October 26, 2021 16:47
-
-
Save sebres/ea6c906a9dc2d477f7b6bb12e6a5488f to your computer and use it in GitHub Desktop.
parseUAG.tcl -- example how to parse user agents (in tcl)
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
proc parseUAG {uag} { | |
array set a {cltype "" clver "" os "" plat "" safari 0 ffox 0 chrome-compat 0 webkit-compat 0} | |
if {![regexp {^(\w+)/(\d+(?:\.\d+){0,4})\s+\((?:(\w+);(?:\s*U;)?\s*)?(\w+)\s*(\d+(?:\.\d+){0,4})} $uag _ a(cltype) a(clver) a(os) a(plat)]} { | |
regexp {^(\w+)/(\d+(?:\.\d+){0,4})\M} $uag _ a(cltype) a(clver) | |
if {![regexp {\((\w+);(?:(?:\s*U;)?\s*)?(\w+)\s*(\d+(?:\.\d+){0,4})} $uag _ a(os) a(plat)]} { | |
regexp {\((\w+);\s*(?:CPU\s+(\w+)\s+)?(?:OS\s+(\S+)\s+)(?:like\s+([^)]+))\)} $uag _ a(plat) a(cpu) a(osver) a(osdesc) | |
} | |
} | |
regexp {\mChrome/(\d+(?:\.\d+){0,4})} $uag {} a(chrome-compat) | |
regexp {\mAppleWebKit/(\d+(?:\.\d+){0,4})} $uag {} a(webkit-compat) | |
regexp {\m(?:Mobile)?Safari/(\d+(?:\.\d+){0,4})} $uag {} a(safari) | |
regexp {\mFirefox/(\d+(?:\.\d+){0,4})} $uag {} a(ffox) | |
if {[regexp {\(i?Mac(?:(?:Book)?Pro)?(\d+(?:[.,]\d+)?)\)$} $uag a(macver)]} { | |
if {$a(os) eq ""} { set a(os) MacOS } | |
} | |
if {[regexp {Darwin/(\d+(?:\.\d+){0,4})} $uag {} a(darwin)]} { | |
if {$a(os) eq ""} { set a(os) iOS } | |
} | |
if {$a(os) eq ""} { | |
switch -- $a(plat) "Android" {set a(os) Linux} "iPhone" {set a(os) iOS} | |
} | |
array get a | |
} | |
foreach uag { | |
{Dalvik/2.1.0 (Linux; U; Android 5.1.1; Navori QL Stix 3500 Build/LMY49F)} | |
{Mozilla/5.0 (Linux; Android 5.1.1; Navori QL Stix 3500 Build/LMY49F; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/67.0.3396.87 Safari/537.36} | |
{Mozilla/5.0 (Android 9; Mobile; rv:68.0) Gecko/68.0 Firefox/68.0} | |
{Mozilla/5.0 (Linux; Android 6.0; vivo 1713 Build/MRA58K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.124 Mobile Safari/537.36} | |
{Mozilla/5.0 (Linux; Android 11; SM-A102U) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.72 Mobile Safari/537.36} | |
{MobileSafari/604.1 CFNetwork/894 Darwin/17.4.0} | |
{Safari/13606.2.104.1.2 CFNetwork/902.3.1 Darwin/17.7.0 (x86_64)} | |
{Safari/6533.19.4 CFNetwork/454.11.5 Darwin/10.5.0 (i386) (iMac11,3)} | |
{Safari/6533.19.4 CFNetwork/454.11.5 Darwin/10.6.0 (i386) (MacPro3,1)} | |
{Safari/6531.21.10 CFNetwork/454.5 Darwin/10.2.0 (i386) (MacBookPro3,1)} | |
{Mozilla/5.0 (iPhone; CPU iPhone OS 12_2 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148} | |
{WeatherReport/1.0.2 CFNetwork/485.13.9 Darwin/11.0.0} | |
{Mozilla/5.0 (iPhone; CPU iPhone OS 13_4_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.1 Mobile/15E148 Safari/604.1} | |
} { | |
set a [parseUAG $uag] | |
puts [format "%.40s...\t%s\t %s\t %s" $uag [dict get $a plat] [dict get $a os] $a] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment