fix twitter scraper

This commit is contained in:
byte[] 2020-07-08 00:26:58 -04:00
parent e581bc2d4e
commit 4e70124f36

View file

@ -1,5 +1,5 @@
defmodule Philomena.Scrapers.Twitter do
@gt_regex ~r|gt=(\d+?);|
@gt_regex ~r|document.cookie = decodeURIComponent\("gt=(\d+);|
@url_regex ~r|\Ahttps?://(?:mobile\.)?twitter.com/([A-Za-z\d_]+)/status/([\d]+)/?|
@script_regex ~r|<script type="text/javascript" .*? src="(https://abs.twimg.com/responsive-web/web/main\.[\da-z]+\.js)">|
@bearer_regex ~r|"(AAAAAAAAAAAAA[^"]*)"|
@ -59,11 +59,8 @@ defmodule Philomena.Scrapers.Twitter do
|> Map.put("url", url)
end
defp extract_guest_token_and_bearer(%Tesla.Env{body: page, headers: headers}) do
[{_, gt}] =
Enum.filter(headers, fn {k, v} -> k == "set-cookie" and String.starts_with?(v, "gt=") end)
[gt] = Regex.run(@gt_regex, gt, capture: :all_but_first)
defp extract_guest_token_and_bearer(%Tesla.Env{body: page}) do
[gt] = Regex.run(@gt_regex, page, capture: :all_but_first)
[script] = Regex.run(@script_regex, page, capture: :all_but_first)
%{body: body} = Philomena.Http.get!(script)