コード例 #1
0
        public UserAgentDetectorItem getInfo(string userAgent)
        {
            DeviceDetector.SetVersionTruncation(VersionTruncation.VERSION_TRUNCATION_NONE);
            DeviceDetector deviceDetector = new DeviceDetector(userAgent);

            deviceDetector.Parse();
            UserAgentDetectorItem userAgentItem        = new UserAgentDetectorItem();
            DeviceDetectorResult  deviceDetectorResult = ((DeviceDetectorResult)DeviceDetector.GetInfoFromUserAgent(userAgent).Match);

            ClientMatchResult client = deviceDetector.GetClient().Match;

            userAgentItem.client_summary = client.Name + " " + client.Version;

            userAgentItem.ua_family  = deviceDetectorResult.BrowserFamily;
            userAgentItem.ua_version = client.Version;

            userAgentItem.os_family  = deviceDetectorResult.Os.Name;
            userAgentItem.os_version = deviceDetectorResult.Os.Version;

            userAgentItem.ua_type  = deviceDetector.GetClient().Match.Type;
            userAgentItem.bot_info = deviceDetector.IsBot();

            userAgentItem.os_meta            = new Os_meta();
            userAgentItem.os_meta.name       = deviceDetectorResult.Os.Name;
            userAgentItem.os_meta.short_name = deviceDetectorResult.Os.ShortName;
            userAgentItem.os_meta.version    = deviceDetectorResult.Os.Version;
            userAgentItem.os_meta.platform   = deviceDetectorResult.Os.Platform;

            /* String[] info = client.ToString().Split("\n");
             * userAgentItem.ua_rendering_engine = info.Length > 4 ? info[4] : "";
             * userAgentItem.ua_rendering_engine_version = info.Length > 5 ? info[5] : "";*/

            userAgentItem.device            = new Device();
            userAgentItem.device.is_mobile  = deviceDetector.IsMobile();
            userAgentItem.device.is_tablet  = deviceDetector.IsTablet();
            userAgentItem.device.is_desktop = deviceDetector.IsDesktop();
            userAgentItem.device.brand      = deviceDetectorResult.DeviceBrand;
            userAgentItem.device.model      = deviceDetectorResult.DeviceModel;

            userAgentItem.client      = new Client();
            userAgentItem.client.bot  = deviceDetector.IsBot();
            userAgentItem.client.user = !deviceDetector.IsBot();
            return(userAgentItem);
        }
コード例 #2
0
        /// <summary>
        /// Parses a useragent and returns the detected data
        ///
        /// ATTENTION: Use that method only for testing or very small applications
        /// To get fast results from DeviceDetector you need to make your own implementation,
        /// that should use one of the caching mechanisms. See README.md for more information.
        ///
        /// </summary>
        /// <param name="ua">UserAgent to parse</param>
        /// <returns></returns>
        public static ParseResult <DeviceDetectorResult> GetInfoFromUserAgent(string ua)
        {
            var result         = new ParseResult <DeviceDetectorResult>();
            var deviceDetector = new DeviceDetector(ua);

            deviceDetector.Parse();

            var match = new DeviceDetectorResult {
                UserAgent = deviceDetector.userAgent
            };

            if (deviceDetector.IsBot())
            {
                match.Bot = deviceDetector.bot.Match;
            }

            match.Os          = deviceDetector.os.Match;
            match.Client      = deviceDetector.client.Match;
            match.DeviceType  = deviceDetector.GetDeviceName();
            match.DeviceBrand = deviceDetector.brand;
            match.DeviceModel = deviceDetector.model;

            if (deviceDetector.os.Success)
            {
                OperatingSystemParser.GetOsFamily(deviceDetector.os.Match.ShortName, out var osFamily);
                match.OsFamily = osFamily;
            }

            if (!(deviceDetector.client.Match is BrowserMatchResult browserMatch))
            {
                return(result.Add(match));
            }

            BrowserParser.GetBrowserFamily(browserMatch.ShortName, out var browserFamily);
            match.BrowserFamily = browserFamily;
            return(result.Add(match));
        }