-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
61 lines (45 loc) · 1.55 KB
/
main.py
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
import json
import re
# https://github.com/ua-parser/uap-python
from ua_parser import user_agent_parser
from user_agents.user_agent import UserAgent
# -----------------------------------------------------------
# Inspired from intoli/user-agents: https://github.com/intoli/user-agents
# https://github.com/intoli/user-agents
# -----------------------------------------------------------
print("PY3 user-agents")
# Generating a Random User Agent
user_agent = UserAgent()
print(str(user_agent))
print(json.dumps(user_agent.data, indent=2))
# Restricting Device Categories
user_agent = UserAgent({"deviceCategory": "mobile"})
# Generating Multiple User Agents With The Same Filters
user_agent = UserAgent({"platform": "Win32"})
user_agents = [user_agent() for _ in range(10000)]
# Regular Expression Matching
user_agent = UserAgent({"userAgent": re.compile("Safari")})
print(user_agent)
# Custom Filter Functions
user_agent = UserAgent({"custom_filter": lambda agent: agent["screenWidth"] > 1300})
print(user_agent)
def filter_user_agent(data):
user_agent_parsed = user_agent_parser.Parse(data["userAgent"])
return (
user_agent_parsed["os"]["family"] == "iOS"
and int(user_agent_parsed["os"]["major"]) > 11
)
user_agent = UserAgent({"custom_filter": filter_user_agent})
print(user_agent)
# Combining Filter
user_agent = UserAgent(
{
"userAgent": re.compile(r"Safari"),
"connection": {
"effectiveType": "4g",
},
"deviceCategory": "desktop",
"platform": "MacIntel",
}
)
print(user_agent)