Skip to content

Commit 7d861ff

Browse files
committed
[#14] WIP - Implement the Google http client
1 parent b590dde commit 7d861ff

3 files changed

Lines changed: 21 additions & 11 deletions

File tree

lib/gscraper/google/http_client.ex

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,38 @@
11
defmodule Gscraper.Google.HttpClient do
2-
@google_search_base_url "https://www.google.com/search?q="
2+
@google_search_base_url "https://www.google.com"
33
@user_agents [
44
'Mozilla/5.0 (Windows NT 5.1; rv:2.0.1) Gecko/20100101 Firefox/4.0.1',
55
'Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.101 Safari/537.36',
66
'Mozilla/5.0 (Windows NT 6.0) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/13.0.782.112 Safari/535.1',
77
'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.85 Safari/537.36',
8+
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.13; rv:61.0) Gecko/20100101 Firefox/98.0',
9+
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0 Safari/605.1.15',
810
]
911

10-
def get(keyword, header) do
11-
client() |> Tesla.post(url, body, headers: headers) |> handle_response()
12+
def get(keyword) do
13+
headers = ["User-Agent": random_user_agent()]
14+
url = "search?q=#{keyword}"
15+
16+
client() |> Tesla.get(url, headers: headers) |> handle_response()
1217
end
1318

1419
defp client() do
1520
middleware = [
16-
{Tesla.Middleware.BaseUrl, @google_search_base_url},
17-
{Tesla.Middleware.Headers, [{"authorization", "token: " <> token }]}
21+
{Tesla.Middleware.BaseUrl, @google_search_base_url}
1822
]
1923

2024
adapter = {Tesla.Adapter.Hackney}
2125

2226
Tesla.client(middleware, adapter)
2327
end
24-
end
28+
29+
defp handle_response({:ok, %{status: status, body: body}}) when status in 200..299,
30+
do: {:ok, body}
31+
32+
defp handle_response({:ok, %{status: status, body: body}}),
33+
do: {:error, %{status: status, body: body}}
34+
35+
defp handle_response({:error, reason}), do: {:error, reason}
36+
37+
defp random_user_agent(), do: @user_agents |> Enum.shuffle() |> Enum.take(1)
38+
end

lib/gscraper/search/scraper.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ defmodule Gscraper.Search.Scraper do
44
def get(keyword) do
55

66
end
7-
end
7+
end

lib/gscraper/search/worker/scraper_worker.ex

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,10 @@ defmodule Gscraper.Search.ScraperWorker do
33

44
alias Gscraper.Search.Searches
55

6-
7-
86
@impl Oban.Worker
97
def perform(%Oban.Job{args: %{"keyword_id" => keyword_id}}) do
108
keyword = Searches.find_keyword_by_id!(keyword_id)
119

1210
keyword
1311
end
14-
15-
1612
end

0 commit comments

Comments
 (0)