mirror of
https://github.com/philomena-dev/philomena.git
synced 2025-02-25 22:44:34 +01:00
* feat(scrapers/twitter): return received tweet text Also: use url and username from received json for the sake of consistent capitalizaton * fix: all fields are under "tweet"
33 lines
916 B
Elixir
33 lines
916 B
Elixir
defmodule Philomena.Scrapers.Twitter do
|
|
@url_regex ~r|\Ahttps?://(?:mobile\.)?(?:twitter\|x).com/([A-Za-z\d_]+)/status/([\d]+)/?|
|
|
|
|
@spec can_handle?(URI.t(), String.t()) :: true | false
|
|
def can_handle?(_uri, url) do
|
|
String.match?(url, @url_regex)
|
|
end
|
|
|
|
def scrape(_uri, url) do
|
|
[user, status_id] = Regex.run(@url_regex, url, capture: :all_but_first)
|
|
|
|
api_url = "https://api.fxtwitter.com/#{user}/status/#{status_id}"
|
|
{:ok, %Tesla.Env{status: 200, body: body}} = Philomena.Http.get(api_url)
|
|
|
|
json = Jason.decode!(body)
|
|
tweet = json["tweet"]
|
|
|
|
images =
|
|
Enum.map(tweet["media"]["photos"], fn p ->
|
|
%{
|
|
url: "#{p["url"]}:orig",
|
|
camo_url: Camo.Image.image_url(p["url"])
|
|
}
|
|
end)
|
|
|
|
%{
|
|
source_url: tweet["url"],
|
|
author_name: tweet["author"]["screen_name"],
|
|
description: tweet["text"],
|
|
images: images
|
|
}
|
|
end
|
|
end
|