-
Notifications
You must be signed in to change notification settings - Fork 107
/
user_agent.ex
34 lines (25 loc) · 899 Bytes
/
user_agent.ex
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
defmodule Crawly.Middlewares.UserAgent do
@moduledoc """
Set/Rotate user agents for crawling. The user agents are read from
:crawly, :user_agents sessions.
The default value for the user agent is: Crawly Bot 1.0
Rotation is determined through `Enum.random/1`.
### Options
- `:user_agents`, optional. A list of user agent strings to rotate. Defaults to "Crawly Bot 1.0".
### Example Declaration
```
middlewares: [
{UserAgent, user_agents: ["My Custom Bot"] }
]
```
"""
def run(request, state, opts \\ []) do
opts = Enum.into(opts, %{user_agents: nil})
new_headers = List.keydelete(request.headers, "User-Agent", 0)
user_agents = Map.get(opts, :user_agents, ["Crawly Bot 1.0"])
useragent = Enum.random(user_agents)
new_request =
Map.put(request, :headers, [{"User-Agent", useragent} | new_headers])
{new_request, state}
end
end