-
Notifications
You must be signed in to change notification settings - Fork 18
/
woothee.rb
170 lines (117 loc) · 4.5 KB
/
woothee.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# -*- coding: utf-8 -*-
require 'woothee/dataset'
require 'woothee/browser'
require 'woothee/os'
require 'woothee/mobilephone'
require 'woothee/crawler'
require 'woothee/appliance'
require 'woothee/misc'
require 'woothee/version'
module Woothee
def self.parse(useragent)
fill_result(exec_parse(useragent))
end
def self.is_crawler(useragent)
!!useragent && useragent.length > 0 and useragent != '-' and try_crawler(useragent, {})
end
def self.exec_parse(useragent)
result = {}
return result if useragent.nil? or useragent.length < 1 or useragent == '-'
if try_crawler(useragent, result)
return result
end
if try_browser(useragent, result)
if try_os(useragent, result)
return result
else
return result
end
end
if try_mobilephone(useragent, result)
return result
end
if try_appliance(useragent, result)
return result
end
if try_misc(useragent, result)
return result
end
# browser unknown. check os only
if try_os(useragent, result)
return result
end
if try_rare_cases(useragent, result)
return result
end
result
end
def self.try_crawler(useragent, result)
return true if Woothee::Crawler.challenge_google(useragent, result)
return true if Woothee::Crawler.challenge_crawlers(useragent, result)
false
end
def self.try_browser(useragent, result)
return true if Woothee::Browser.challenge_msie(useragent, result)
return true if Woothee::Browser.challenge_vivaldi(useragent, result)
return true if Woothee::Browser.challenge_yandexbrowser(useragent, result)
return true if Woothee::Browser.challenge_samsungbrowser(useragent, result)
return true if Woothee::Browser.challenge_safari_chrome(useragent, result)
return true if Woothee::Browser.challenge_firefox(useragent, result)
return true if Woothee::Browser.challenge_opera(useragent, result)
return true if Woothee::Browser.challenge_webview(useragent, result)
false
end
def self.try_os(useragent, result)
return true if Woothee::OS.challenge_windows(useragent, result)
# OSX PC and iOS devices (strict check)
return true if Woothee::OS.challenge_osx(useragent, result)
# Linux PC and Android
return true if Woothee::OS.challenge_linux(useragent, result)
# all useragents matches /(iPhone|iPad|iPod|Android|BlackBerry)/
return true if Woothee::OS.challenge_smartphone(useragent, result)
# mobile phones like KDDI-.*
return true if Woothee::OS.challenge_mobilephone(useragent, result)
# Nintendo DSi/Wii with Opera
return true if Woothee::OS.challenge_appliance(useragent, result)
# Win98, BSD, classic MacOS, ...
return true if Woothee::OS.challenge_misc(useragent, result)
false
end
def self.try_mobilephone(useragent, result)
return true if Woothee::MobilePhone.challenge_docomo(useragent, result)
return true if Woothee::MobilePhone.challenge_au(useragent, result)
return true if Woothee::MobilePhone.challenge_softbank(useragent, result)
return true if Woothee::MobilePhone.challenge_willcom(useragent, result)
return true if Woothee::MobilePhone.challenge_misc(useragent, result)
false
end
def self.try_appliance(useragent, result)
return true if Woothee::Appliance.challenge_playstation(useragent, result)
return true if Woothee::Appliance.challenge_nintendo(useragent, result)
return true if Woothee::Appliance.challenge_digitaltv(useragent, result)
false
end
def self.try_misc(useragent, result)
return true if Woothee::Misc.challenge_desktoptools(useragent, result)
false
end
def self.try_rare_cases(useragent, result)
return true if Woothee::Misc.challenge_smartphone_patterns(useragent, result)
return true if Woothee::Browser.challenge_sleipnir(useragent, result)
return true if Woothee::Misc.challenge_http_library(useragent, result)
return true if Woothee::Misc.challenge_maybe_rss_reader(useragent, result)
return true if Woothee::Crawler.challenge_maybe_crawler(useragent, result)
false
end
FILLED = {
Woothee::ATTRIBUTE_NAME => Woothee::VALUE_UNKNOWN,
Woothee::ATTRIBUTE_CATEGORY => Woothee::VALUE_UNKNOWN,
Woothee::ATTRIBUTE_OS => Woothee::VALUE_UNKNOWN,
Woothee::ATTRIBUTE_OS_VERSION => Woothee::VALUE_UNKNOWN,
Woothee::ATTRIBUTE_VERSION => Woothee::VALUE_UNKNOWN,
Woothee::ATTRIBUTE_VENDOR => Woothee::VALUE_UNKNOWN,
}.freeze
def self.fill_result(result)
FILLED.merge(result)
end
end