Skip to content

Instantly share code, notes, and snippets.

@storborg
Created July 8, 2009 20:53
Show Gist options
  • Save storborg/143184 to your computer and use it in GitHub Desktop.
Save storborg/143184 to your computer and use it in GitHub Desktop.
"""
Quick hack to return a likely User Agent based on a weighted list of strings.
Use like:
>>> import useragent
>>> useragent.pick()
"""
import random
uas = ((0.05, 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.5; en-US; rv:1.9.1) Gecko/20090624 Firefox/3.5'), # FF 3.5 Leopard
(0.2, 'Mozilla/5.0 (Windows; U; Windows NT 6.0; en-GB; rv:1.9.0.11) Gecko/2009060215 Firefox/3.0.11 (.NET CLR 3.5.30729)'), # FF 3.0 Vista
(0.05, 'Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.1) Gecko/20090615 Firefox/3.5'), # FF 3.5 Vista
(0.1, 'Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.9.1) Gecko/20090624 Firefox/3.5 GTB5'), # FF 3.5 XP
(0.2, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.6pre) Gecko/2009011606 Firefox/3.1'), # FF 3.1 XP
(0.02, 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_5_6; en-us) AppleWebKit/525.18.1 (KHTML, like Gecko) Version/3.1.2 Safari/525.20.1'), # Safari 3.1.2 Leopard
(0.3, 'Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.0.4) Gecko/2008102920 Firefox/3.0.4'), # FF 3.0.4 Vista
(0.5, 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; GTB6; SLCC1; .NET CLR 2.0.50727; Media Center PC 5.0; .NET CLR 3.0.04506)'), # IE7 Vista
(0.08, 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_5_7; en-us) AppleWebKit/528.18.1 (KHTML, like Gecko) Version/4.0 Safari/528.17'), # Safari 4.0 Leopard
(0.8, 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)'), # IE6 XP
(0.1, 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; GTB6; Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1) ; .NET CLR 1.1.4322; .NET CLR 2.0.50727; InfoPath.1; .NET CLR 3.0.04506.30; .NET CLR 3.0.04506.648; Windows-Media-Player/10.00.00.3990; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; UXPVP 1.0.7.0)'), # IE6 Crazy long
(0.1, 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; GoogleT5; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30)'), # Different IE6 XP
(0.2, 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; FunWebProducts)'), # IE6 XP with some spyware
(0.2, 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; GTB6; InfoPath.2; .NET CLR 2.0.50727)'), # Different IE6 XP
(0.1, 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; FunWebProducts; Windows-Media-Player/10.00.00.3990; .NET CLR 1.1.4322; .NET CLR 2.0.50727)'), # Different IE6 XP
(0.02, 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; TencentTraveler 4.0; User-agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1;); .NET CLR 2.0.50727)'), # Different Weird IE6 XP
(0.01, 'Mozilla/4.0 (compatible; MSIE 6.0; Windows 98)'), # IE6 Windows 98
(0.7, 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)'), # IE7 Windows XP SP2
(1.0, 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)'), # IE7 Vista
(0.5, 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0)'), # IE8 Vista
(0.2, 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0)'), # IE8 on Windows 7
(0.1, 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; Trident/4.0)'), # IE8 on Vista "Compatibility View"
(0.04, 'Opera 9.7 (Windows NT 5.2; U; en)'), # Opera on WinXP SP2
(0.01, 'Opera/9.61 (Macintosh; Intel Mac OS X; U; de) Presto/2.1.1'), # Opera on OS X
(0.1, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/525.28 (KHTML, like Gecko) Version/3.2.2 Safari/525.28.1'), # Safari 3.2.2
)
# Build a table with a gradually increasing thresholds, so we can just index
# into it with a random from 0 to 1.
interval_table = []
accum = 0.0
total = sum([weight for weight, ss in uas])
for weight, ss in uas:
accum += weight
interval_table.append((accum / total, ss))
def pick():
rnd = random.random()
for threshold, ss in interval_table:
if rnd < threshold:
return ss
else:
return interval_table[-1][1]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment